/* Bad CSS */ .selector, .selector-secondary, .selector[type=text] { padding:15px; margin:0px 0px 15px; background-color:rgba(0, 0, 0, 0.5); box-shadow:0px 1px 2px #CCC,inset 0 1px 0 #FFFFFF } /* Good CSS */ .selector, .selector-secondary, .selector[type="text"] { padding: 15px; margin-bottom: 15px; background-color: rgba(0,0,0,.5); box-shadow: 0 1px 2px #ccc, inset 0 1px 0 #fff; }
因为定位(positioning)能够从正常的文档流中移除元素,而且还能覆盖盒模型(box model)相关的样式,所以排在首位。盒模型排在第二位,由于它决定了组件的尺寸和位置。css
其余属性只是影响组件的内部(inside)或者是不影响前两组属性,所以排在后面。web
.declaration-order { /* Positioning */ position: absolute; top: 0; right: 0; bottom: 0; left: 0; z-index: 100; /* Box-model */ display: block; float: right; width: 100px; height: 100px; /* Typography */ font: normal 13px "Helvetica Neue", sans-serif; line-height: 1.5; color: #333; text-align: center; /* Visual */ background-color: #f5f5f5; border: 1px solid #e5e5e5; border-radius: 3px; /* Misc */ opacity: 1; }
与 <link> 标签相比,@import 指令要慢不少,不光增长了额外的请求次数,还会致使不可预料的问题。替代办法有如下几种:浏览器
<!-- Use link elements --> <link rel="stylesheet" href="core.css"> <!-- Avoid @imports --> <style> @import url("more.css"); </style>
将媒体查询放在尽量相关规则的附近。不要将他们打包放在一个单同样式文件中或者放在文档底部。若是你把他们分开了,未来只会被你们遗忘。下面给出一个典型的实例。app
.element { ... } .element-avatar { ... } .element-selected { ... } @media (min-width: 480px) { .element { ...} .element-avatar { ... } .element-selected { ... } }
当使用特定厂商的带有前缀的属性时,经过缩进的方式,让每一个属性的值在垂直方向对齐,这样便于多行编辑。编辑器
在 Textmate 中,使用 Text → Edit Each Line in Selection (⌃⌘A)。在 Sublime Text 2 中,使用 Selection → Add Previous Line (⌃⇧↑) 和 Selection → Add Next Line (⌃⇧↓)。ide
/* Prefixed properties */ .selector { -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15); box-shadow: 0 1px 2px rgba(0,0,0,.15); }
对于只包含一条声明的样式,为了易读性和便于快速编辑,建议将语句放在同一行。对于带有多条声明的样式,仍是应当将声明分为多行。性能
这样作的关键因素是为了错误检测 -- 例如,CSS 校验器指出在 183 行有语法错误。若是是单行单条声明,你就不会忽略这个错误;若是是单行多条声明的话,你就要仔细分析避免漏掉错误了。优化
/* Single declarations on one line */ .span1 { width: 60px; } .span2 { width: 140px; } .span3 { width: 220px; } /* Multiple declarations, one per line */ .sprite { display: inline-block; width: 16px; height: 15px; background-image: url(../img/sprite.png); } .icon { background-position: 0 0; } .icon-home { background-position: 0 -20px; } .icon-account { background-position: 0 -40px; }
在须要显示地设置全部值的状况下,应当尽可能限制使用简写形式的属性声明。常见的滥用简写属性声明的状况以下:编码
大部分状况下,咱们不须要为简写形式的属性声明指定全部值。例如,HTML 的 heading 元素只须要设置上、下边距(margin)的值,所以,在必要的时候,只需覆盖这两个值就能够。过分使用简写形式的属性声明会致使代码混乱,而且会对属性值带来没必要要的覆盖从而引发意外的反作用。url
MDN(Mozilla Developer Network)上一片很是好的关于shorthand properties 的文章,对于不太熟悉简写属性声明及其行为的用户颇有用。
/* Bad example */ .element { margin: 0 0 10px; background: red; background: url("image.jpg"); border-radius: 3px 3px 0 0; } /* Good example */ .element { margin-bottom: 10px; background-color: red; background-image: url("image.jpg"); border-top-left-radius: 3px; border-top-right-radius: 3px; }
避免非必要的嵌套。这是由于虽然你可使用嵌套,可是并不意味着应该使用嵌套。只有在必须将样式限制在父元素内(也就是后代选择器),而且存在多个须要嵌套的元素时才使用嵌套。
// Without nesting .table > thead > tr > th { … } .table > thead > tr > td { … } // With nesting .table > thead > tr { > th { … } > td { … } }
代码是由人编写并维护的。请确保你的代码可以自描述、注释良好而且易于他人理解。好的代码注释可以传达上下文关系和代码目的。不要简单地重申组件或 class 名称。
对于较长的注释,务必书写完整的句子;对于通常性注解,能够书写简洁的短语。
/* Bad example */ /* Modal header */ .modal-heade1`r { ... } /* Good example */ /* Wrapping element for .modal-title and .modal-close */ .modal-header { ...
/* Bad example */ .t { ... } .red { ... } .header { ... } /* Good example */ .tweet { ... } .important { ... } .tweet-header { ... }
/* Bad example */ span { ... } .page-container #stream .stream-item .tweet .tweet-header .username { ... } .avatar { ... } /* Good example */ .avatar { ... } .tweet-header .username { ... } .tweet .avatar { ... }
/* * Component section heading */ .element { ... } /* * Component section heading * * Sometimes you need to include optional context for the entire component. Do that up here if it's important enough. */ .element { ... } /* Contextual sub-component or modifer */ .element-heading { ... }
将你的编辑器按照下面的配置进行设置,以免常见的代码不一致和差别: