/* 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 { ... }
將你的編輯器按照下面的配置進行設置,以免常見的代碼不一致和差別: