CSS 样式集分析
CSS 样式集分析
[selector]{
[property]:[value];
[<\- Declaration \->]
}
[选择器]{
[属性]:[值];
[<\- 声明 \->]
}
编写 CSS 样式时,我习惯遵守这些规则:
class 名称以连字符(-)连接,除了下文提到的 BEM 命名法;
缩进 4 空格;
声明拆分成多行;
声明以相关性顺序排列,而非字母顺序;
有前缀的声明适当缩进,从而对齐其值;
缩进样式集从而反映 DOM;
保留最后一条声明结尾的分号。例如:
.widget{
padding:10px;
border:1px solid #BADA55;
background\-color:#C0FFEE;
\-webkit\-border\-radius:4px;
\-moz\-border\-radius:4px;
border\-radius:4px;
}
.widget\-heading{
font\-size:1.5rem;
line\-height:1;
font\-weight:bold;
color:#BADA55;
margin\-right:\-10px;
margin\-left: \-10px;
padding:0.25em;
}
我们可以发现,.widget-heading
是 .widget
的子元素,因为前者的样式集比后者多缩进了一级。这样通过缩进就可以让开发者在阅读代码时快速获取这样的重要信息。
我们还可以发现 .widget-heading
的声明是根据其相关性排列的:.widget-heading
是行间元素,所以我们先添加字体相关的样式声明,接下来是其它的。
以下是一个没有拆分成多行的例子:
复制代码
.t10 { width:10% }
.t20 { width:20% }
.t25 { width:25% } /\* 1/4 \*/
.t30 { width:30% }
.t33 { width:33.333% } /\* 1/3 \*/
.t40 { width:40% }
.t50 { width:50% } /\* 1/2 \*/
.t60 { width:60% }
.t66 { width:66.666% } /\* 2/3 \*/
.t70 { width:70% }
.t75 { width:75% } /\* 3/4\*/
.t80 { width:80% }
.t90 { width:90% }
https://www.wanxiangyundang.top/read/CSS-Guidelines/spilt.3.css.md