在 css 首行設置文件編碼爲 UTF-8。css
@charset "UTF-8";
class 名稱應當儘量短,而且意義明確。使用有意義的名稱,使用有組織的或目的明確的名稱,不要使用表現形式的名稱。html
不推薦git
fw-800 {font-weight:800;} .red {color:red;}
推薦github
.heavy {font-weight:800;} .important {color:red;}
使用中劃線(-)分隔 class 中的單詞。雖然它很不方便的讓你雙擊選擇,可是它能夠加強理解性。另外屬性選擇器 [attribute|=value]
也能識別中劃線(-),因此最好堅持使用中劃線做爲分隔符。web
不推薦瀏覽器
.slide_hd {} .slide_bd {}
推薦sass
.slide-hd {} .slide-bd {}
基於最近的父 class 或基本 class 做爲新 class 的前綴。app
不推薦ide
.header .logo {} .header .banner {}
推薦佈局
.header-logo {} .header-banner {}
使用 .js-*
的 class 來標識行爲(與樣式相對),而且不要將這些 class 寫有任何的樣式。
在寫選擇器時,要儘量的減小嵌套層級,通常 2~3 層,不要超過 4 層。
不推薦
.main ul li a span {}
推薦
.main span {}
當構建選擇器時應該使用清晰,準確和有語義的 class 名。儘可能減小使用標籤選擇器。若是你只關心你的 class 名,而不是你的代碼元素,這樣會更容易維護。
不推薦
div.content > header.content-header > h2.title { font-size: 2em; }
推薦
.content > .content-header > .title { font-size: 2em; }
css 提供了各類簡寫屬性(font
、background
等等),使用簡寫屬性對於代碼效率和可讀性是有頗有用的。
不推薦
border-top-style: none;
font-family: palatino, georgia, serif;
font-size: 100%;
line-height: 1.6;
padding-bottom: 2px;
padding-left: 1px;
padding-right: 1px;
padding-top: 0;
推薦
border-top: none;
font: 100%/1.6 palatino, georgia, serif;
padding: 0 1px 2px;
可是不能濫用簡寫形式,過分使用簡寫形式的屬性聲明會致使代碼混亂,而且會對屬性值帶來沒必要要的覆蓋從而引發意外的反作用。
不推薦
width:100px;
margin:0 auto;
推薦
width:100px;
margin-right:auto;
margin-left:auto;
不要在 0 值後面使用單位。
不推薦
padding-bottom: 0px;
margin: 0em;
推薦
padding-bottom: 0;
margin: 0;
css 中的顏色值能夠使用 16 進制來表示,在可能的狀況下,能夠進行縮寫,好比:#fff
、#000
。
雖然 hacks 可以很方便的解決瀏覽器之間的兼容問題,可是咱們仍是不要使用 hacks,儘可能從根本上解決問題,好比改變結構等等。
爲了保證更好的可讀性,咱們應該遵循如下順序:
position
| z-index
| top
| right
| bottom
| left
| clip
display
| float
| clear
| visibility
| overflow
| overflow-x
| overflow-y
width
| min-width
| max-width
| height
| min-height
| max-height
margin
| margin-top
| margin-right
| margin-bottom
| margin-left
padding
| padding-top
| padding-right
| padding-bottom
| padding-left
border
| border-top
| border-right
| border-bottom
| border-left
| border-radius
| box-shadow
| border-image
background
| background-color
| background-image
| background-repeat
| background-attachment
| background-position
| background-origin
| background-clip
| background-size
color
| opacity
font
| font-style
| font-variant
| font-weight
| font-size
| font-family
text-transform
| white-space
| word-break
| word-wrap
| overflow-wrap
| text-align
| word-spacing
| letter-spacing
| text-indent
| vertical-align
| line-height
text-decoration
| text-shadow
direction
| unicode-bidi
| writing-mode
list-style
| list-style-image
| list-style-position
| list-style-type
table-layout
| border-collapse
| border-spacing
| caption-side
| empty-cells
content
| counter-increment
| counter-reset
| quotes
appearance
| text-overflow
| outline
| outline-width
| outline-color
| outline-style
| outline-offset
| cursor
| zoom
| box-sizing
| resize
| user-select
columns
| column-width
| column-count
| column-gap
| column-rule
| column-rule-width
| column-rule-style
| column-rule-color
| column-span
| column-fill
| column-break-before
| column-break-after
| column-break-inside
flex
transform
| transition
| animation
將媒體查詢放在儘量相關規則的附近。不要將他們打包放在一個單同樣式文件中或者放在文檔底部。若是你把他們分開了,未來只會被你們遺忘。
推薦
.element {} .element-avatar {} .element-selected {} @media (min-width: 480px) { .element {} .element-avatar {} .element-selected {} }
當使用特定廠商的帶有前綴的屬性時,經過縮進的方式,讓每一個屬性的值在垂直方向對齊,這樣便於多行編輯。
.selector { -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15); box-shadow: 0 1px 2px rgba(0,0,0,.15); }
屬性選擇器或屬性值用雙引號(""
),而不是單引號(''
)括起來。url 的值不要使用引號。
不推薦
import url('//cdn.com/foundation.css'); input[type='submit'] { font-family: 'open sans', arial, sans-serif; } body:after { content: 'pause'; }
推薦
@import url(//cdn.com/foundation.css); input[type="submit"] { font-family: "open sans", arial, sans-serif; } body:after { content: "pause"; }
爲了保證一致性和可擴展性,每一個聲明應該用分號結束。
不推薦
.demo { width:100px; height:100px }
推薦
.demo { width:100px; height:100px; }
爲了易讀性和便於快速編輯,統一將語句分爲多行,即便該樣式只包含一條聲明。
不推薦
.demo {width:100px;height:100px;}
推薦
.demo { width:100px; height:100px; }
css 中文字體能夠用 unicode 格式來表示,好比「宋體」能夠用 \5B8B\4F53
來表示。具體參考下表:
中文 | 英文名 | unicode |
---|---|---|
宋體 | SimSun | \5B8B\4F53 |
微軟雅黑 | Microsoft YaHei | \5FAE\8F6F\96C5\9ED1 |
在 sass 中你能夠嵌套選擇器,這能夠使代碼變得更清潔和可讀。嵌套全部的選擇器,但儘可能避免嵌套沒有任何內容的選擇器。
若是你須要指定一些子元素的樣式屬性,而父元素將不什麼樣式屬性,能夠使用常規的 css 選擇器鏈,這將防止您的腳本看起來過於複雜。
不推薦
.content { display: block; } .content > .news-article > .title { font-size: 1.2em; }
推薦
.content { display: block; > .news-article > .title { font-size: 1.2em; } }
當使用 sass 的嵌套功能的時候,重要的是有一個明確的嵌套順序。
@extend
和 @include
:first-letter
,:hover
,:active
等等):before
和 :after
).selected
,.active
等等).test { @extend %clearfix; color:#ccc; &:hover { color:#000; } &:before { border:1px solid #eee; content:""; } &.active { color:#f00; &:hover { color:#ff0; } } @media screen and (max-width: 640px) { display:block; font-size:2em; } > .title { font-size:1.2em; } }