css規範,css命名規範

來源:互聯網
上載者:User

css規範,css命名規範

1.檔案
  [建議] CSS 檔案使用無BOM的UTF-8編碼。
  解釋:
    UTF-8 編碼具有更廣泛的適應性。BOM(byte order mark)在使用程式或工具處理檔案時可能造成不必要的幹擾。
  擴充:
     http://www.zhihu.com/question/20167122

 

2.縮排
  [強制] 使用 4 個空格做為一個縮排層級,不允許使用 2 個空格 或 tab 字元。
  樣本:

.selector {    margin: 0;    padding: 0;}

 

3.空格
  [強制] 選取器 與 { 之間必須包含空格。
  樣本:

.selector {}

  [強制] 屬性名稱與之後的:之間不允許包含空格,:與屬性值之間必須包含空格。
  樣本:

margin: 0;

  [強制] 列表型屬性值書寫在單行時,,後必須跟一個空格。
  樣本:

font-family: Arial, sans-serif; 

 

4.行長度
  [強制] 每行不得超過 120 個字元,除非單行不可分割。
  解釋:
    常見不可分割的情境為URL超長。

  [建議] 對於超長的樣式,在樣式值的空格處或,後換行,建議按邏輯分組。
  樣本:

/* 不同屬性值按邏輯分組 */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)));    

 

5.選取器
  [強制] 當一個 rule 包含多個 selector 時,每個選取器聲明必須獨佔一行。
  樣本:

/* 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;}

  [強制] 屬性選取器中的值必須用雙引號包圍。
  解釋:
    不允許使用單引號,不允許不使用引號。
  樣本:

/* good */article[character="juliet"] {    voice-family: "Vivien Leigh", victoria, female}/* bad */article[character='juliet'] {    voice-family: "Vivien Leigh", victoria, female}

 

6.屬性
  [強制] 屬性定義必須另起一行。
  樣本:

/* good */.selector {    margin: 0;    padding: 0;}/* bad */.selector { margin: 0; padding: 0; }

  [強制] 屬性定義後必須以分號結尾。
  樣本:

/* good */.selector {    margin: 0;}/* bad */.selector {    margin: 0}

 

7.選取器
  [強制] 如無必要,不得為 id、class 選取器添加類型選取器進行限定。
  解釋:
    在效能和維護性上,都有一定的影響。
  樣本:

/* good */#error,.danger-message {    font-color: #c00;}/* bad */dialog#error,p.danger-message {    font-color: #c00;}

  [建議] 選取器的嵌套層級應不大於 3 級,位置靠後的限定條件應儘可能精確。
  樣本:

/* good */#username input {}.comment .avatar {}/* bad */.page .header .login #username input {}.comment div * {}

 

8.屬性縮寫
  [建議] 在可以使用縮寫的情況下,盡量使用屬性縮寫。
  樣本:

/* 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 等縮寫時,應注意隱含值對實際數值的影響,確實需要設定多個方向的值時才使用縮寫。
  解釋:
    border / margin / padding 等縮寫會同時設定多個屬性的值,容易覆蓋不需要覆蓋的設定。如某些方向需要繼承其他聲明的值,則應該分開設定。
  樣本:

/* centering <article class="page"> horizontally and highlight featured ones */article {    margin: 5px;    border: 1px solid #999;}/* good */.page {    margin-right: auto;    margin-left: auto;}.featured {    border-color: #69c;}/* bad */.page {    margin: 5px auto; /* introducing redundancy */}.featured {    border: 1px solid #69c; /* introducing redundancy */} 

 

9.屬性書寫順序
  [建議] 同一 rule set 下的屬性在書寫時,應按功能進行分組,並以 Formatting Model(布局方式、位置) > Box Model(尺寸) > Typographic(文本相關) > Visual(視覺效果) 的順序書寫,以提高代碼的可讀性。
  解釋:
    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;}                 

 

10.清楚浮動
  [建議] 當元素需要撐起高度以包含內部的浮動元素時,通過對偽類設定 clear 或觸發 BFC 的方式進行 clearfix。盡量不使用增加空標籤的方式。
  解釋:
    觸發 BFC 的方式很多,常見的有:
      float 非 none
      position 非 static
      overflow 非 visible
    如希望使用更小副作用的清除浮動方法,參見 A new micro clearfix hack 一文。
    另需注意,對已經觸發 BFC 的元素不需要再進行 clearfix。

 

11.!important
  [建議] 盡量不使用 !important 聲明。


  [建議] 當需要強制指定樣式且不允許任何情境覆蓋時,通過標籤內聯和!important定義樣式。
  解釋:
    必須注意的是,僅在設計上確實不允許任何其它情境覆蓋樣式時,才使用內聯的!important樣式。

 

12.z-index
  [建議] 將 z-index 進行分層,對文檔流外絕對位置元素的視覺層級關係進行管理。
  解釋:
    同層的多個元素,如多個由使用者輸入觸發的 Dialog,在該層級內使用相同的 z-index 或遞增 z-index。
    建議每層包含100個 z-index 來容納足夠的元素,如果每層元素較多,可以調整這個數值。

  [建議] 在可控環境下,期望顯示在最上層的元素,z-index 指定為999999。
  解釋:
    可控環境分成兩種,一種是自身產品線環境;還有一種是可能會被其他產品線引用,但是不會被外部第三方的產品引用。
    不建議取值為 2147483647。以便於自身產品線被其他產品線引用時,當遇到層級覆蓋衝突的情況,留出向上調整的空間。

  [建議] 在第三方環境下,期望顯示在最上層的元素,通過標籤內聯和 !important,將z-index指定為2147483647。

/*    Z-index屬性,用於定義使用position: relative; or position: absolute;”.定位的的 HTML 元素的顯示順序"位置:    Z 軸索引的最大值是 2147483647,除了Safari 3最大Z-index為 16777271。*/

  解釋:
    第三方環境對於開發人員來說完全不可控。在第三方環境下的元素,為了保證元素不被其頁面其他樣式定義覆蓋,需要採用此做法。

 

13.文本
  [強制] 常值內容必須用雙引號包圍。
  解釋:
    文本類型的內容可能在選取器、屬性值等內容中。
  樣本:

/* 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: "”";} 

 

14.數值
  [強制] 當數值為 0 - 1 之間的小數時,省略整數部分的 0。
  樣本:

/* good */panel {    opacity: .8}/* bad */panel {    opacity: 0.8}

 

15.url()
  [強制] url() 函數中的路徑不加引號。
  樣本:

body {    background: url(bg.png);} 

  [建議] url() 函數中的絕對路徑可省去協議名。
  樣本:

body {    background: url(//baidu.com/img/bg.png) no-repeat 0 0;}

 

16.長度
  [強制] 長度為 0 時須省略單位。 (也只有長度單位可省)
  樣本:

/* good */body {    padding: 0 5px;}/* bad */body {    padding: 0px 5px;}

 

17.顏色
  [強制] RGB顏色值必須使用十六進位記號形式 #rrggbb。不允許使用 rgb()。
  解釋:
    帶有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 {    background-color: #aca;}/* bad */.success {    background-color: #aaccaa;}

  [強制] 顏色值不允許使用命名色值。
  樣本:

/* good */.success {    color: #90ee90;}/* bad */.success {    color: lightgreen;}

  [建議] 顏色值中的英文字元採用小寫。如不用小寫也需要保證同一項目內保持大小寫一致。
  樣本:

/* good */.success {    background-color: #aca;    color: #90ee90;}/* good */.success {    background-color: #ACA;    color: #90EE90;}/* bad */.success {    background-color: #ACA;    color: #90ee90;}

 

18.2D位置
  [強制] 必須同時給出水平和垂直方向的位置。
  解釋:
    2D 位置初始值為 0% 0%,但在只有一個方向的值時,另一個方向的值會被解析為 center。為避免理解上的困擾,應同時給出兩個方向的值。
  樣本:

/* good */body {    background-position: center top; /* 50% 0% */}/* bad */body {    background-position: top; /* 50% 0% */}

 

19.字型
  [強制] font-family 屬性中的字型族名稱應使用字型的英文 Family Name,其中如有空格,須放置在引號中。
  解釋:
    所謂英文 Family Name,為字型檔的一個中繼資料,常見名稱如下:

字型                作業系統            Family Name宋體 (中易宋體)     Windows            SimSun黑體 (中易黑體)     Windows            SimHei微軟雅黑             Windows            Microsoft YaHei微軟正黑             Windows            Microsoft JhengHei華文黑體             Mac/iOS            STHeiti冬青黑體             Mac/iOS            Hiragino Sans GB文泉驛正黑     Linux            WenQuanYi Zen Hei文泉驛微米黑     Linux            WenQuanYi Micro Hei                        

  樣本:

h1 {    font-family: "Microsoft YaHei";}

  [強制] font-family 按「西文字型在前、中文字型在後」、「效果佳 (品質高/更能滿足需求) 的字型在前、效果一般的字型在後」的順序編寫,最後必須指定一個通用字型族( serif / sans-serif )。
  解釋:
    更詳細說明可參考http://www.zhihu.com/question/19911793/answer/13329819。
  樣本:

/* 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 大小寫必須統一。
  樣本:

/* 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;}

 

20.字型大小
  [強制] 需要在 Windows 平台顯示的中文內容,其字型大小應不小於 12px。
  解釋:
    由於 Windows 的字型渲染機制,小於12px的文字顯示效果極差、難以辨認。

 

21.字型風格
  [建議] 需要在 Windows 平台顯示的中文內容,不要使用除 normal 外的 font-style。其他平台也應慎用。
  解釋:
    由於中文字型沒有 italic 風格的實現,所有瀏覽器下都會 fallback 到 obilique 實現 (自動擬合為斜體),小字型大小下 (特別是 Windows 下會在小字型大小下使用點陣字型的情況下) 顯示效果差,造成閱讀困難。

 

22.字重
  [強制] font-weight 屬性必須使用數值方式描述。
  解釋:
    CSS 的字重分 100 – 900 共九檔,但目前受字型本身品質和瀏覽器的限制,實際上支援400和700兩檔,分別等價於關鍵詞normal和bold。
瀏覽器本身使用一系列啟發學習法規則來進行匹配,在<700 時一般匹配字型的Regular字重,>=700 時匹配Bold字重。
但已有瀏覽器開始支援 =600 時匹配 Semibold 字重 (見http://justineo.github.io/slideshows/font/#/3/15),故使用數值描述增加了靈活性,也更簡短。
  樣本:

/* good */h1 {    font-weight: 700;}/* bad */h1 {    font-weight: bold;}

 

23.行高
  [建議] line-height 在定義文本段落時,應使用數值。
  解釋:
    將line-height設定為數值,瀏覽器會基於當前元素設定的font-size進行再次計算。在不同字型大小的文本段落組合中,能達到較為舒適的行間間隔效果,避免在每個設定了font-size都需要設定line-height。
    當line-height用於控制垂直置中時,還是應該設定成與容器高度一致。
  樣本:

.container {    line-height: 1.5;}

 

24.變換與動畫
  [強制] 使用 transition 時應指定 transition-property。
  樣本:

/* good */.box {    transition: color 1s, border-color 1s;}/* bad */.box {    transition: all 1s;}

  [建議] 儘可能在瀏覽器能高效實現的屬性上添加過渡和動畫。
  解釋:
    詳情見http://www.html5rocks.com/en/tutorials/speed/high-performance-animations/,在可能的情況下應選擇這樣四種變換:
      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 */}

 

25.響應式
  [強制] 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 */}

  [強制] Media Query 如果有多個逗號分隔的條件時,應將每個條件放在單獨一行中。
  樣本:

@media(-webkit-min-device-pixel-ratio: 2), /* Webkit-based browsers */(min--moz-device-pixel-ratio: 2), /* Older Firefox browsers (prior to Firefox 16) */(min-resolution: 2dppx), /* The standard way */(min-resolution: 192dpi) { /* dppx fallback *//* Retina-specific stuff here */}

  [建議] 儘可能給出在高解析度裝置 (Retina) 下效果更佳的樣式。

 

26.屬性首碼
  [強制] 帶私人首碼的屬性由長到短排列,按冒號位置對齊。
  解釋:
    標準屬性放在最後,按冒號對齊方便閱讀,也便於在編輯器內進行多行編輯。
  樣本:

.box {    -webkit-box-sizing: border-box;       -moz-box-sizing: border-box;            box-sizing: border-box;} 

 

27.Hack
  [建議] 需要添加 hack 時應儘可能考慮是否可以採用其他方式解決。
  解釋:
    如果能通過合理的 HTML 結構或使用其他的 CSS 定義達到理想的樣式,則不應該使用 hack 手段解決問題。通常 hack 會導致維護成本的增加。

  [建議] 盡量使用 選取器 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;}

  [建議] 盡量使用簡單的 屬性 hack。
  樣本:

.box {_display: inline; /* fix double margin */    float: left;    margin-left: 20px;}.container {    overflow: hidden;    *zoom: 1; /* triggering hasLayout */}

  

28.Expression
[強制] 禁止使用 Expression。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.