CSS的一些编程规范总结

2020-05-07 06:00:50易采站长站整理

    -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 是行间元素,所以我们先添加字体相关的样式声明,接下来是其它的。

  以下是一个没有拆分成多行的例子:

CSS Code复制内容到剪贴板

.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% }   

  在这个例子(来自inuit.css’s table grid system)中,将 CSS 放在一行内可以使得代码更紧凑。
 命名规范

  一般情况下我都是以连字符(-)连接 class 的名字(例如 .foo-bar 而非 .foo_bar 或 .fooBar),不过在某些特定的时候我会用 BEM(Block, Element, Modifier)命名法。