:
后應(yīng)該插入一個(gè)空格。box-shadow
)。rgb()
、rgba()
、hsl()
、hsla()
或 rect()
值的內(nèi)部的逗號(hào)后面插入空格。這樣利于從多個(gè)屬性值(既加逗號(hào)也加空格)中區(qū)分多個(gè)顏色值(只加逗號(hào),不加空格)。.5
代替 0.5
;-.5px
代替 -0.5px
)。#fff
。在掃描文檔時(shí),小寫字符易于分辨,因?yàn)樗麄兊男问礁子趨^(qū)分。#fff
代替 #ffffff
。input[type="text"]
。只有在某些情況下是可選的,但是,為了代碼的一致性,建議都加上雙引號(hào)。margin: 0;
代替 margin: 0px;
。對(duì)于這里用到的術(shù)語有疑問嗎?請(qǐng)參考 Wikipedia 上的 層疊樣式表 - 語法。
/* 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; }
相關(guān)的屬性聲明應(yīng)當(dāng)歸為一組,并按照下面的順序排列:
由于定位(positioning)可以從正常的文檔流中移除元素,并且還能覆蓋盒模型(box model)相關(guān)的樣式,因此排在首位。盒模型排在第二位,因?yàn)樗鼪Q定了組件的尺寸和位置。
其他屬性只是影響組件的內(nèi)部(inside)或者是不影響前兩組屬性,因此排在后面。
完整的屬性列表及其排列順序請(qǐng)參考 Recess。
.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; }
@import
與 <link>
標(biāo)簽相比,@import
指令要慢很多,不光增加了額外的請(qǐng)求次數(shù),還會(huì)導(dǎo)致不可預(yù)料的問題。替代辦法有以下幾種:
<link>
元素請(qǐng)參考 Steve Souders 的文章了解更多知識(shí)。
<!-- Use link elements --> <link rel="stylesheet" href="core.css"> <!-- Avoid @imports --> <style> @import url("more.css"); </style>
將媒體查詢放在盡可能相關(guān)規(guī)則的附近。不要將他們打包放在一個(gè)單一樣式文件中或者放在文檔底部。如果你把他們分開了,將來只會(huì)被大家遺忘。下面給出一個(gè)典型的示例。
.element { ... } .element-avatar { ... } .element-selected { ... } @media (min-width: 480px) { .element { ...} .element-avatar { ... } .element-selected { ... } }
當(dāng)使用特定廠商的帶有前綴的屬性時(shí),通過縮進(jìn)的方式,讓每個(gè)屬性的值在垂直方向?qū)R,這樣便于多行編輯。
在 Textmate 中,使用 Text → Edit Each Line in Selection (??A)。在 Sublime Text 2 中,使用 Selection → Add Previous Line (??↑) 和 Selection → Add Next Line (??↓)。
/* Prefixed properties */ .selector { -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15); box-shadow: 0 1px 2px rgba(0,0,0,.15); }
對(duì)于只包含一條聲明的樣式,為了易讀性和便于快速編輯,建議將語句放在同一行。對(duì)于帶有多條聲明的樣式,還是應(yīng)當(dāng)將聲明分為多行。
這樣做的關(guān)鍵因素是為了錯(cuò)誤檢測(cè) -- 例如,CSS 校驗(yàn)器指出在 183 行有語法錯(cuò)誤。如果是單行單條聲明,你就不會(huì)忽略這個(gè)錯(cuò)誤;如果是單行多條聲明的話,你就要仔細(xì)分析避免漏掉錯(cuò)誤了。
/* 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; }
在需要顯示地設(shè)置所有值的情況下,應(yīng)當(dāng)盡量限制使用簡(jiǎn)寫形式的屬性聲明。常見的濫用簡(jiǎn)寫屬性聲明的情況如下:
padding
margin
font
background
border
border-radius
大部分情況下,我們不需要為簡(jiǎn)寫形式的屬性聲明指定所有值。例如,HTML 的 heading 元素只需要設(shè)置上、下邊距(margin)的值,因此,在必要的時(shí)候,只需覆蓋這兩個(gè)值就可以。過度使用簡(jiǎn)寫形式的屬性聲明會(huì)導(dǎo)致代碼混亂,并且會(huì)對(duì)屬性值帶來不必要的覆蓋從而引起意外的副作用。
MDN(Mozilla Developer Network)上一篇非常好的關(guān)于shorthand properties 的文章,對(duì)于不太熟悉簡(jiǎn)寫屬性聲明及其行為的用戶很有用。
/* 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; }
避免非必要的嵌套。這是因?yàn)殡m然你可以使用嵌套,但是并不意味著應(yīng)該使用嵌套。只有在必須將樣式限制在父元素內(nèi)(也就是后代選擇器),并且存在多個(gè)需要嵌套的元素時(shí)才使用嵌套。
// Without nesting .table > thead > tr > th { … } .table > thead > tr > td { … } // With nesting .table > thead > tr { > th { … } > td { … } }
代碼是由人編寫并維護(hù)的。請(qǐng)確保你的代碼能夠自描述、注釋良好并且易于他人理解。好的代碼注釋能夠傳達(dá)上下文關(guān)系和代碼目的。不要簡(jiǎn)單地重申組件或 class 名稱。
對(duì)于較長(zhǎng)的注釋,務(wù)必書寫完整的句子;對(duì)于一般性注解,可以書寫簡(jiǎn)潔的短語。
/* Bad example */ /* Modal header */ .modal-header { ... } /* Good example */ /* Wrapping element for .modal-title and .modal-close */ .modal-header { ... }
.btn
和 .btn-danger
)。.btn
代表 button,但是 .s
不能表達(dá)任何意思。.js-*
class 來標(biāo)識(shí)行為(與樣式相對(duì)),并且不要將這些 class 包含到 CSS 文件中。在為 Sass 和 Less 變量命名是也可以參考上面列出的各項(xiàng)規(guī)范。
/* Bad example */ .t { ... } .red { ... } .header { ... } /* Good example */ .tweet { ... } .important { ... } .tweet-header { ... }
[class^="..."]
)。瀏覽器的性能會(huì)受到這些因素的影響。擴(kuò)展閱讀:
/* 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 { ... }
將你的編輯器按照下面的配置進(jìn)行設(shè)置,以避免常見的代碼不一致和差異:
參照文檔并將這些配置信息添加到項(xiàng)目的 .editorconfig
文件中。例如:Bootstrap 中的 .editorconfig 示例。更多信息請(qǐng)參考 about EditorConfig。