CSS
文件使用無 BOM
的 UTF-8
編碼。4
個空格作爲一個縮進層級,不容許使用 2
個空格 或 tab
字符。選擇器
與 {
之間必須包含空格。.selector {
}
屬性名
與以後的 :
之間不容許包含空格, :
與 屬性值
之間必須包含空格。margin: 0;
列表型屬性值
書寫在單行時,,
後必須跟一個空格。font-family: Arial, sans-serif;
120
個字符,除非單行不可分割。空格
處或 ,
後換行,建議按邏輯分組。/* 不一樣屬性值按邏輯分組 */ 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; }
>
、+
、~
選擇器的兩邊各保留一個空格。/* 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; }
不容許使用單引號,不容許不使用引號。css
1.6 屬性git
/* good */ .selector { margin: 0; padding: 0; } /* bad */ .selector { margin: 0; padding: 0; }
2.通用github
2.1選擇器web
id
、class
選擇器添加類型選擇器進行限定。在性能和維護性上,都有必定的影響。windows
/* good */ #error, .danger-message { font-color: #c00; } /* bad */ dialog#error, p.danger-message { font-color: #c00; }
3
級,位置靠後的限定條件應儘量精確。
/* good */ .post { font: 12px/1.5 arial, sans-serif; } /* bad */ .post { font-family: arial, sans-serif; font-size: 12px; line-height: 1.5; }
border
/ margin
/ padding
等縮寫時,應注意隱含值對實際數值的影響,確實須要設置多個方向的值時才使用縮寫。
position
/ top
/ right
/ bottom
/ left
/ float
/ display
/ overflow
等border
/ margin
/ padding
/ width
/ height
等font
/ line-height
/ text-align
/ word-wrap
等background
/ color
/ transition
/ list-style
等另外,若是包含 content
屬性,應放在最前面。瀏覽器
clear
或觸發 BFC
的方式進行 clearfix
。儘可能不使用增長空標籤的方式。
觸發 BFC 的方式不少,常見的有:編輯器
另需注意,對已經觸發 BFC 的元素不須要再進行 clearfix。ide
!important
聲明。!important
定義樣式。
2.6 z-index函數
z-index
進行分層,對文檔流外絕對定位元素的視覺層級關係進行管理。 同層的多個元素,如多個由用戶輸入觸發的 Dialog,在該層級內使用相同的 z-index
或遞增 z-index
。佈局
建議每層包含100個 z-index
來容納足夠的元素,若是每層元素較多,能夠調整這個數值。
z-index
指定爲 999999
。!important
,將 z-index
指定爲 2147483647
。第三方環境對於開發者來講徹底不可控。在第三方環境下的元素,爲了保證元素不被其頁面其餘樣式定義覆蓋,須要採用此作法。
文本類型的內容可能在選擇器、屬性值等內容中。
0
。/* good */ panel { opacity: .8; } /* bad */ panel { opacity: 0.8; }
url()
函數中的路徑不加引號。body { background: url(bg.png); }
url()
函數中的絕對路徑可省去協議名。body { background: url(//baidu.com/img/bg.png) no-repeat 0 0; }
0
時須省略單位。 (也只有長度單位可省)#rrggbb
。不容許使用 rgb()
。使用 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 { background-color: #aca; } /* bad */ .success { background-color: #aaccaa; }
/* good */ .success { color: #90ee90; } /* bad */ .success { color: lightgreen; }
2D 位置初始值爲 0% 0%
,但在只有一個方向的值時,另外一個方向的值會被解析爲 center。爲避免理解上的困擾,應同時給出兩個方向的值
/* good */ body { background-position: center top; /* 50% 0% */ } /* bad */ body { background-position: top; /* 50% 0% */ }
font-family
屬性中的字體族名稱應使用字體的英文 Family Name
,其中若有空格,須放置在引號中。font-family
按「西文字體在前、中文字體在後」、「效果佳 (質量高/更能知足需求) 的字體在前、效果通常的字體在後」的順序編寫,最後必須指定一個通用字體族( serif
/ sans-serif
)。/* 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; }
font-family
不區分大小寫,但在同一個項目中,一樣的 Family Name
大小寫必須統一。
12px
。
normal
外的 font-style
。其餘平臺也應慎用。
font-weight
屬性必須使用數值方式描述。CSS 的字重分 100 – 900 共九檔,但目前受字體自己質量和瀏覽器的限制,實際上支持 400
和 700
兩檔,分別等價於關鍵詞 normal
和 bold
。
在 <700
時通常匹配字體的 Regular 字重,>=700
時匹配 Bold 字重。
/* good */ h1 { font-weight: 700; } /* bad */ h1 { font-weight: bold; }
line-height
在定義文本段落時,應使用數值。將 line-height
設置爲數值,瀏覽器會基於當前元素設置的 font-size
進行再次計算。在不一樣字號的文本段落組合中,能達到較爲溫馨的行間間隔效果,避免在每一個設置了 font-size
都須要設置 line-height
。
當 line-height
用於控制垂直居中時,仍是應該設置成與容器高度一致。
transition
時應指定 transition-property
。
Media Query
不得單獨編排,必須與相關的規則一塊兒定義。/* 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 */ }
標準屬性放在最後,按冒號對齊方便閱讀,也便於在編輯器內進行多行編輯。