CSS 做爲網頁樣式的描述語言。本文檔的目標是使 CSS 代碼風格保持一致,容易被理解和被維護。css
解釋:
UTF-8 編碼具備更普遍的適應性。html
示例:前端
.selector { margin: 0; padding: 0; }
示例:html5
.selector { }
示例:git
margin: 0;
示例:github
font-family: Arial, sans-serif;
解釋:
常見不可分割的場景爲URL超長。web
示例:瀏覽器
/* 不一樣屬性值按邏輯分組 */ background: transparent url(aVeryVeryVeryLongUrlIsPlacedHere) no-repeat 0 0; /* 可重複屢次的屬性,每次重複一行 */ background-image: url(aVeryVeryVeryLongUrlIsPlacedHere) url(anotherVeryVeryVeryLongUrlIsPlacedHere); /* 相似函數的屬性值能夠根據函數調用的縮進進行 */ background-image: -webkit-gradient( linear, left bottom, left top, color-stop(0.04, rgb(88,94,124)), color-stop(0.52, rgb(115,123,162)) );
示例:編輯器
/* good */ .post, .page, .comment { line-height: 1.5; } /* bad */ .post, .page, .comment { line-height: 1.5; }
示例:ide
/* good */ main > nav { padding: 10px; } label + input { margin-left: 5px; } input:checked ~ button { background-color: #69C; } /* bad */ main>nav { padding: 10px; } label+input { margin-left: 5px; } input:checked~button { background-color: #69C; }
示例:
/* good */ article[character="juliet"] { voice-family: "Vivien Leigh", victoria, female; } /* bad */ article[character='juliet'] { voice-family: "Vivien Leigh", victoria, female; }
示例:
/* good */ .selector { margin: 0; padding: 0; } /* bad */ .selector { margin: 0; padding: 0; }
示例:
/* good */ .selector { margin: 0; } /* bad */ .selector { margin: 0 }
解釋:
在性能和維護性上,都有必定的影響。
示例:
/* good */ #error, .danger-message { font-color: #c00; } /* bad */ dialog#error, p.danger-message { font-color: #c00; }
解釋:
BEM是一種命名方法,可以幫助你在前端開發中實現可複用的組件和代碼共享。
BEM的命名規矩很容易記:block-name__element-name--modifier-name,也就是模塊名 + 元素名 + 修飾器名。
示例:
<div class="page-btn"> <button type="button" class="page-btn__prev">上一頁</button> <!-- ... --> <button type="button" class="page-btn__next">下一頁</button> </div>
示例:
/* good */ #username input {} .comment .avatar {} /* bad */ .page .header .login #username input {} .comment div * {}
示例:
/* good */ .post { font: 12px/1.5 arial, sans-serif; } /* bad */ .post { font-family: arial, sans-serif; font-size: 12px; line-height: 1.5; }
解釋:
堅持限制屬性取值簡寫的使用,屬性簡寫須要你必須顯式設置全部取值。常見的屬性簡寫濫用包括:
padding
margin
font
background
border
border-radius
大多數狀況下,咱們並不須要設置屬性簡寫中包含的全部值。例如,HTML 頭部只設置上下的 margin,因此若是須要,只設置這兩個值。過分使用屬性簡寫每每會致使更混亂的代碼,其中包含沒必要要的重寫和意想不到的反作用。
Mozilla Developer Network 有一篇對不熟悉屬性簡寫及其行爲的人來講很棒的關於shorthand properties 的文章。
示例:
/* good */ .element { margin-bottom: 10px; background-color: red; background-image: url(image.jpg); border-top-left-radius: 3px; border-top-right-radius: 3px; } /* bad */ .element { margin: 0 0 10px; background: red; background: url(image.jpg); border-radius: 3px 3px 0 0; }
解釋:
Formatting Model 相關屬性包括:position / top / right / bottom / left / float / display / overflow 等
Box Model 相關屬性包括:border / margin / padding / width / height 等
Typographic 相關屬性包括:font / line-height / text-align / word-wrap 等
Visual 相關屬性包括:background / color / transition / list-style 等
另外,若是包含 content 屬性,應放在最前面。
示例:
.sidebar { /* formatting model: positioning schemes / offsets / z-indexes / display / ... */ position: absolute; top: 50px; left: 0; overflow-x: hidden; /* box model: sizes / margins / paddings / borders / ... */ width: 200px; padding: 5px; border: 1px solid #ddd; /* typographic: font / aligns / text styles / ... */ font-size: 14px; line-height: 20px; /* visual: colors / shadows / gradients / ... */ background: #f5f5f5; color: #333; -webkit-transition: color 1s; -moz-transition: color 1s; transition: color 1s; }
解釋:
觸發 BFC 的方式不少,常見的有:
float 非 none
position 非 static
overflow 非 visible
如但願使用更小反作用的清除浮動方法,參見 A new micro clearfix hack一文。
另需注意,對已經觸發 BFC 的元素不須要再進行 clearfix。
解釋:
必須注意的是,僅在設計上 確實不容許任何其它場景覆蓋樣式 時,才使用內聯的 !important 樣式。一般在第三方環境的應用中使用這種方案。下面的 z-index 章節是其中一個特殊場景的典型樣例。
解釋:
本身寫的z-index的值不能超過 100 (通用組的除外);
頁面中的元素內容的z-index不能超過10(popup poptip除外),須要按照內容定義1 2 3 4不容許直接使用如1000,9999;
popup poptip的z-index須要按照內容使用 99如下,10以上的值(11,12,13,也能夠小於10),不容許直接使用1000,9999之類大值;
解釋:
可控環境分紅兩種,一種是自身產品線環境;還有一種是可能會被其餘產品線引用,可是不會被外部第三方的產品引用。
不建議取值爲 2147483647。以便於自身產品線被其餘產品線引用時,當遇到層級覆蓋衝突的狀況,留出向上調整的空間。
解釋:
文本類型的內容可能在選擇器、屬性值等內容中。
示例:
/* good */ html[lang|="zh"] q:before { font-family: "Microsoft YaHei", sans-serif; content: "「"; } html[lang|="zh"] q:after { font-family: "Microsoft YaHei", sans-serif; content: "」"; } /* bad */ html[lang|=zh] q:before { font-family: 'Microsoft YaHei', sans-serif; content: '「'; } html[lang|=zh] q:after { font-family: "Microsoft YaHei", sans-serif; content: "」"; }
示例:
/* good */ panel { opacity: .8; } /* bad */ panel { opacity: 0.8; }
示例:
body { background: url(//baidu.com/img/bg.png) no-repeat 0 0; }
示例:
/* good */ body { padding: 0 5px; } /* bad */ body { padding: 0px 5px; }
解釋:
帶有alpha的顏色信息可使用 rgba()。使用 rgba() 時每一個逗號後必須保留一個空格。
示例:
/* good */ .success { box-shadow: 0 0 2px rgba(0, 128, 0, .3); border-color: #008000; } /* bad */ .success { box-shadow: 0 0 2px rgba(0,128,0,.3); border-color: rgb(0, 128, 0); }
示例:
/* good */ .success { color: #90ee90; } /* bad */ .success { color: lightgreen; }
示例:
/* good */ .success { background-color: #aca; } /* bad */ .success { background-color: #aaccaa; }
示例:
/* good */ .success { background-color: #aca; color: #90ee90; } /* good */ .success { background-color: #ACA; color: #90EE90; } /* bad */ .success { background-color: #ACA; color: #90ee90; }
解釋:
2D 位置初始值爲 0% 0%,但在只有一個方向的值時,另外一個方向的值會被解析爲 center。爲避免理解上的困擾,應同時給出兩個方向的值。background-position屬性值的定義
示例:
/* good */ body { background-position: center top; /* 50% 0% */ } /* bad */ body { background-position: top; /* 50% 0% */ }
解釋:
所謂英文 Family Name,爲字體文件的一個元數據,常見名稱以下:
示例:
h1 { font-family: "Microsoft YaHei"; }
解釋:
更詳細說明可參考如何保證網頁的字體在各平臺都儘可能顯示爲最高質量的黑體?
示例:
/* Display according to platform */ .article { font-family: Arial, sans-serif; } /* Specific for most platforms */ h1 { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", "WenQuanYi Micro Hei", "Microsoft YaHei", sans-serif; }
示例:
/* good */ body { font-family: Arial, sans-serif; } h1 { font-family: Arial, "Microsoft YaHei", sans-serif; } /* bad */ body { font-family: arial, sans-serif; } h1 { font-family: Arial, "Microsoft YaHei", sans-serif; }
解釋:
因爲 Windows 的字體渲染機制,小於 12px 的文字顯示效果極差、難以辨認。
解釋:
CSS 的字重分 100 – 900 共九檔,但目前受字體自己質量和瀏覽器的限制,實際上支持 400 和 700 兩檔,分別等價於關鍵詞normal 和 bold。
瀏覽器自己使用一系列啓發式規則來進行匹配,在 <700 時通常匹配字體的 Regular 字重,>=700 時匹配 Bold 字重。
但已有瀏覽器開始支持 =600 時匹配 Semibold 字重 (見此表),故使用數值描述增長了靈活性,也更簡短。
示例:
/* good */ h1 { font-weight: 700; } /* bad */ h1 { font-weight: bold; }
將 line-height
設置爲數值,瀏覽器會基於當前元素設置的 font-size
進行再次計算。在不一樣字號的文本段落組合中,能達到較爲溫馨的行間間隔效果,避免在每一個設置了 font-size
都須要設置 line-height
。
當 line-height
用於控制垂直居中時,仍是應該設置成與容器高度一致。
示例
.container { line-height: 1.5; }
示例:
/* good */ .box { transition: color 1s, border-color 1s; } /* bad */ .box { transition: all 1s; }
解釋:
見本文,在可能的狀況下應選擇這樣四種變換:
transform: translate(npx, npx);
transform: scale(n);
transform: rotate(ndeg);
opacity: 0..1;
典型的,可使用 translate 來代替 left 做爲動畫屬性。
示例:
/* good */ .box { transition: transform 1s; } .box:hover { transform: translate(20px); /* move right for 20px */ } /* bad */ .box { left: 0; transition: left 1s; } .box:hover { left: 20px; /* move right for 20px */ }
示例:
/* Good */ /* header styles */ @media (...) { /* header styles */ } /* main styles */ @media (...) { /* main styles */ } /* footer styles */ @media (...) { /* footer styles */ } /* Bad */ /* header styles */ /* main styles */ /* footer styles */ @media (...) { /* header styles */ /* main styles */ /* footer styles */ }
解釋:
儘可能將媒體查詢的位置靠近他們相關的規則。不要將他們一塊兒放到一個獨立的樣式文件中,或者丟在文檔的最底部。這樣作只會讓你們之後更容易忘記他們。
示例:
element { ... } .element-avatar { ... } .element-selected { ... } @media (min-width: 480px) { .element { ...} .element-avatar { ... } .element-selected { ... } }
解釋:
標準屬性放在最後,按冒號對齊方便閱讀,也便於在編輯器內進行多行編輯。
示例:
.box { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; }
解釋:
若是能經過合理的 HTML 結構或使用其餘的 CSS 定義達到理想的樣式,則不該該使用 hack 手段解決問題。一般 hack 會致使維護成本的增長。
解釋:
儘可能使用符合 CSS 語法的 selector hack,能夠避免一些第三方庫沒法識別 hack 語法的問題。
示例:
/* IE 7 */ *:first-child + html #header { margin-top: 3px; padding: 5px; } /* IE 6 */ * html #header { margin-top: 5px; padding: 4px; }
示例:
.box { _display: inline; /* fix double margin */ float: left; margin-left: 20px; } .container { overflow: hidden; *zoom: 1; /* triggering hasLayout */ }