/* Bad CSS */.selector,.selector-secondary,.selector[type=text]{padding:15px;margin:0px0px15px;background-color:rgba(0,0,0,0.5);box-shadow:0px1px2px#CCC,inset01px0#FFFFFF}/* Good CSS */.selector,.selector-secondary,.selector[type="text"]{padding:15px;margin-bottom:15px;background-color:rgba(0,0,0,.5);box-shadow:01px2px#ccc,inset01px0#fff;}
/* 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:00;}.icon-home{background-position:0-20px;}.icon-account{background-position:0-40px;}
/* Bad example */.element{margin:0010px;background:red;background:url("image.jpg");border-radius:3px3px00;}/* Good example */.element{margin-bottom:10px;background-color:red;background-image:url("image.jpg");border-top-left-radius:3px;border-top-right-radius:3px;}
/* Bad example */span{...}.page-container#stream.stream-item.tweet.tweet-header.username{...}.avatar{...}/* Good example */.avatar{...}.tweet-header.username{...}.tweet.avatar{...}
代码组织
以组件为单位组织代码段。
制定一致的注释规范。
使用一致的空白符将代码分隔成块,这样利于扫描较大的文档。
如果使用了多个 CSS 文件,将其按照组件而非页面的形式分拆,因为页面会被重组,而组件只会被移动。
/* * 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{...}