CSS 預處理器技術已經非常的成熟,而且也涌現出了越來越多的 CSS 的預處理器框架。本文向你介紹使用最為普遍的三款 CSS 預處理器框架,分別是 Sass、Less CSS、Stylus。
首先我們來簡單介紹下什么是 CSS 預處理器,CSS 預處理器是一種語言用來為 CSS 增加一些編程的的特性,無需考慮瀏覽器的兼容性問題,例如你可以在 CSS 中使用變量、簡單的程序邏輯、函數等等在編程語言中的一些基本技巧,可以讓你的 CSS 更見簡潔,適應性更強,代碼更直觀等諸多好處。
不要再停留在石器時代了,下面讓我們開始 CSS 預處理器之旅。
我們將會從語法、變量、嵌套、混入(Mixin)、繼承、導入、函數和操作符等方面分別對這三個框架進行比較介紹。
語法
在使用 CSS 預處理器之前最重要的是理解語法,幸運的是基本上大多數預處理器的語法跟 CSS 都差不多。
首先 Sass 和 Less 都使用的是標準的 CSS 語法,因此如果你可以很方便的將已有的 CSS 代碼轉為預處理器代碼,默認 Sass 使用 .sass 擴展名,而 Less 使用 .less 擴展名。
下面是這二者的語法:
|
1
2
3
4
|
/* style.scss or style.less */h1 { color: #0982C1;} |
你注意到了,這是一個再普通不過的,不過 Sass 同時也支持老的語法,就是不包含花括號和分號的方式:
|
1
2
3
|
/* style.sass */h1 color: #0982c1 |
而 Stylus 支持的語法要更多樣性一點,它默認使用 .styl 的文件擴展名,下面是 Stylus 支持的語法:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
/* style.styl */h1 { color: #0982C1;} /* omit brackets */h1 color: #0982C1; /* omit colons and semi-colons */h1 color #0982C1 |
你也可以在同一個樣式單中使用不同的變量,例如下面的寫法也不會報錯:
|
1
2
3
4
5
|
h1 { color #0982c1}h2 font-size: 1.2em |
變量
你可以在 CSS 預處理器中聲明變量,并在整個樣式單中使用,支持任何類型的變量,例如顏色、數值(不管是否包括單位)、文本。然后你可以任意引用該變量。
Sass 的變量必須是 $ 開始,然后變量名和值使用冒號隔開,跟 CSS 的屬性一致:
|
1
2
3
4
5
6
7
8
9
|
$mainColor: #0982c1;$siteWidth: 1024px;$borderStyle: dotted; body { color: $mainColor; border: 1px $borderStyle $mainColor; max-width: $siteWidth;} |
而 Less 的變量名使用 @ 符號開始:
|
1
2
3
4
5
6
7
8
9
|
@mainColor: #0982c1;@siteWidth: 1024px;@borderStyle: dotted; body { color: @mainColor; border: 1px @borderStyle @mainColor; max-width: @siteWidth;} |
Stylus 對變量名沒有任何限定,你可以是 $ 開始,也可以是任意的字符,而且與變量值之間可以用冒號、空格隔開,需要注意的是 Stylus (0.22.4) 將會編譯 @ 開始的變量,但其對應的值并不會賦予該變量,換句話說,在 Stylus 的變量名不要用 @ 開頭。
|
1
2
3
4
5
6
7
8
|
mainColor = #0982c1siteWidth = 1024px$borderStyle = dotted body color mainColor border 1px $borderStyle mainColor max-width siteWidth |
上面的三種不同的 CSS 預處理器的寫法,最終都將產生相同的結果:
|
1
2
3
4
5
|
body { color: #0982c1; border: 1px dotted #0982c1; max-width: 1024px;} |
你可以想象,加入你的 CSS 中使用了某個顏色的地方多達數十次,那么要修改顏色時你必須找到這數十次的地方并一一修改,而有了 CSS 預處理器,修改一個地方就夠了!
嵌套
如果我們需要在CSS中相同的 parent 引用多個元素,這將是非常乏味的,你需要一遍又一遍地寫 parent。例如:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
section { margin: 10px;}section nav { height: 25px;}section nav a { color: #0982C1;}section nav a:hover { text-decoration: underline;} |
而如果用 CSS 預處理器,就可以少些很多單詞,而且父子節點關系一目了然。我們這里提到的三個 CSS 框架都是允許嵌套語法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
section { margin: 10px; nav { height: 25px; a { color: #0982C1; &:hover { text-decoration: underline; } } }} |
最終生成的 CSS 結果是:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
section { margin: 10px;}section nav { height: 25px;}section nav a { color: #0982C1;}section nav a:hover { text-decoration: underline;} |
非常之方便!
Mixins (混入)
Mixins 有點像是函數或者是宏,當你某段 CSS 經常需要在多個元素中使用時,你可以為這些共用的 CSS 定義一個 Mixin,然后你只需要在需要引用這些 CSS 地方調用該 Mixin 即可。
Sass 的混入語法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/* Sass mixin error with (optional) argument $borderWidth which defaults to 2px if not specified */@mixin error($borderWidth: 2px) { border: $borderWidth solid #F00; color: #F00;} .generic-error { padding: 20px; margin: 4px; @ include error(); /* Applies styles from mixin error */}.login-error { left: 12px; position: absolute; top: 20px; @ include error(5px); /* Applies styles from mixin error with argument $borderWidth equal to 5px*/} |
Less CSS 的混入語法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/* LESS mixin error with (optional) argument @borderWidth which defaults to 2px if not specified */.error(@borderWidth: 2px) { border: @borderWidth solid #F00; color: #F00;} .generic-error { padding: 20px; margin: 4px; .error(); /* Applies styles from mixin error */}.login-error { left: 12px; position: absolute; top: 20px; .error(5px); /* Applies styles from mixin error with argument @borderWidth equal to 5px */} |
Stylus 的混入語法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/* Stylus mixin error with (optional) argument borderWidth which defaults to 2px if not specified */error(borderWidth= 2px) { border: borderWidth solid #F00; color: #F00;} .generic-error { padding: 20px; margin: 4px; error(); /* Applies styles from mixin error */}.login-error { left: 12px; position: absolute; top: 20px; error(5px); /* Applies styles from mixin error with argument borderWidth equal to 5px */} |
最終它們都將編譯成如下的 CSS 樣式:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
.generic-error { padding: 20px; margin: 4px; border: 2px solid #f00; color: #f00;}.login-error { left: 12px; position: absolute; top: 20px; border: 5px solid #f00; color: #f00;} |
繼承
當我們需要為多個元素定義相同樣式的時候,我們可以考慮使用繼承的做法。例如我們經常需要:
|
1
2
3
4
5
|
p,ul,ol { /* styles here */} |
在 Sass 和 Stylus 我們可以這樣寫:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
.block { margin: 10px 5px; padding: 2px;} p { @extend .block; /* Inherit styles from '.block' */ border: 1px solid #EEE;}ul, ol { @extend .block; /* Inherit styles from '.block' */ color: #333; text-transform: uppercase;} |
在這里首先定義 .block 塊,然后讓 p 、ul 和 ol 元素繼承 .block ,最終生成的 CSS 如下:
|
1
2
3
4
5
6
7
8
9
10
11
|
.block, p, ul, ol { margin: 10px 5px; padding: 2px;}p { border: 1px solid #EEE;}ul, ol { color: #333; text-transform: uppercase;} |
在這方面 Less 表現的稍微弱一些,更像是混入寫法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
.block { margin: 10px 5px; padding: 2px;} p { .block; /* Inherit styles from '.block' */ border: 1px solid #EEE;}ul, ol { .block; /* Inherit styles from '.block' */ color: #333; text-transform: uppercase;} |
生成的 CSS 如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
.block { margin: 10px 5px; padding: 2px;}p { margin: 10px 5px; padding: 2px; border: 1px solid #EEE;}ul,ol { margin: 10px 5px; padding: 2px; color: #333; text-transform: uppercase;} |
你所看到的上面的代碼中,.block 的樣式將會被插入到相應的你想要繼承的選擇器中,但需要注意的是優先級的問題。
導入 (Import)
很多 CSS 開發者對導入的做法都不太感冒,因為它需要多次的 HTTP 請求。但是在 CSS 預處理器中的導入操作則不同,它只是在語義上包含了不同的文件,但最終結果是一個單一的 CSS 文件,如果你是通過 @ import "file.css"; 導入 CSS 文件,那效果跟普通的 CSS 導入一樣。注意:導入文件中定義的混入、變量等信息也將會被引入到主樣式文件中,因此需要避免它們互相沖突。
reset.css:
|
1
2
3
4
|
/* file.{type} */body { background: #EEE;} |
main.xxx:
|
1
2
3
4
5
6
|
@ import "reset.css";@ import "file.{type}"; p { background: #0982C1;} |
最終生成的 CSS:
|
1
2
3
4
5
6
7
|
@ import "reset.css";body { background: #EEE;}p { background: #0982C1;} |
顏色函數
CSS 預處理器一般都會內置一些顏色處理函數用來對顏色值進行處理,例如加亮、變暗、顏色梯度等。
Sass:
|
1
2
3
4
5
6
7
8
9
10
11
|
lighten($color, 10%); /* returns a color 10% lighter than $color */darken($color, 10%); /* returns a color 10% darker than $color */ saturate($color, 10%); /* returns a color 10% more saturated than $color */desaturate($color, 10%); /* returns a color 10% less saturated than $color */ grayscale($color); /* returns grayscale of $color */complement($color); /* returns complement color of $color */invert($color); /* returns inverted color of $color */ mix($color1, $color2, 50%); /* mix $color1 with $color2 with a weight of 50% */ |
上面只是簡單列了 Sass 的一些基本顏色處理函數,完整的列表請看 Sass Documentation.
下面是一個具體的例子:
|
1
2
3
4
5
6
|
$color: #0982C1; h1 { background: $color; border: 3px solid darken($color, 50%);} |
Less CSS:
|
1
2
3
4
5
6
7
8
9
10
|
lighten(@color, 10%); /* returns a color 10% lighter than @color */darken(@color, 10%); /* returns a color 10% darker than @color */ saturate(@color, 10%); /* returns a color 10% more saturated than @color */desaturate(@color, 10%); /* returns a color 10% less saturated than @color */ spin(@color, 10); /* returns a color with a 10 degree larger in hue than @color */spin(@color, -10); /* returns a color with a 10 degree smaller hue than @color */ mix(@color1, @color2); /* return a mix of @color1 and @color2 */ |
LESS 完整的顏色函數列表請看 LESS Documentation.
LESS 使用顏色函數的例子:
|
1
2
3
4
5
6
|
@color: #0982C1; h1 { background: @color; border: 3px solid darken(@color, 50%);} |
Stylus:
|
1
2
3
4
5
|
lighten(color, 10%); /* returns a color 10% lighter than 'color' */darken(color, 10%); /* returns a color 10% darker than 'color' */ saturate(color, 10%); /* returns a color 10% more saturated than 'color' */desaturate(color, 10%); /* returns a color 10% less saturated than 'color' */ |
完整的顏色函數列表請閱讀 Stylus Documentation.
實例:
|
1
2
3
4
5
|
color = #0982C1 h1 background color border 3px solid darken(color, 50%) |
運算符
你可以直接在 CSS 預處理器中進行樣式的計算,例如:
|
1
2
3
4
5
6
|
body { margin: (14px/2); top: 50px + 100px; right: 100px - 50px; left: 10 * 10;} |
一些跟具體瀏覽器相關的處理
這是宣傳使用預處理的原因之一,并且是一個很好的理由,這樣可以節省的大量的時間和汗水。創建一個mixin來處理不同瀏覽器的CSS寫法是很簡單的,節省了大量的重復工作和痛苦的代碼編輯。
Sass
|
1
2
3
4
5
6
7
8
9
|
@mixin border-radius($values) { -webkit-border-radius: $values; -moz-border-radius: $values; border-radius: $values;} div { @ include border-radius(10px);} |
Less CSS
|
1
2
3
4
5
6
7
8
9
|
.border-radius(@values) { -webkit-border-radius: @values; -moz-border-radius: @values; border-radius: @values;} div { .border-radius(10px);} |
Stylus
|
1
2
3
4
5
6
7
8
9
|
border-radius(values) { -webkit-border-radius: values; -moz-border-radius: values; border-radius: values;} div { border-radius(10px);} |
編譯結果:
|
1
2
3
4
5
|
div { -webkit-border-radius: 10px; -moz-border-radius: 10px; border-radius: 10px;} |
3D文本
要生成具有 3D 效果的文本可以使用 text-shadows ,唯一的問題就是當要修改顏色的時候就非常的麻煩,而通過 mixin 和顏色函數可以很輕松的實現:
Sass
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@mixin text3d($color) { color: $color; text-shadow: 1px 1px 0px darken($color, 5%), 2px 2px 0px darken($color, 10%), 3px 3px 0px darken($color, 15%), 4px 4px 0px darken($color, 20%), 4px 4px 2px #000;} h1 { font-size: 32pt; @ include text3d(#0982c1);} |
Less CSS
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
.text3d(@color) { color: @color; text-shadow: 1px 1px 0px darken(@color, 5%), 2px 2px 0px darken(@color, 10%), 3px 3px 0px darken(@color, 15%), 4px 4px 0px darken(@color, 20%), 4px 4px 2px #000;} span { font-size: 32pt; .text3d(#0982c1);} |
Stylus
|
1
2
3
4
5
6
|
text3d(color) color: color text-shadow: 1px 1px 0px darken(color, 5%), 2px 2px 0px darken(color, 10%), 3px 3px 0px darken(color, 15%), 4px 4px 0px darken(color, 20%), 4px 4px 2px #000span font-size: 32pt text3d(#0982c1) |
生成的 CSS
|
1
2
3
4
5
6
7
8
9
|
span { font-size: 32pt; color: #0982c1; text-shadow: 1px 1px 0px #097bb7, 2px 2px 0px #0875ae, 3px 3px 0px #086fa4, 4px 4px 0px #07689a, 4px 4px 2px #000;} |
效果圖:
列 (Columns)
使用數值操作和變量可以很方便的實現適應屏幕大小的布局處理。
Sass
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
$siteWidth: 1024px;$gutterWidth: 20px;$sidebarWidth: 300px; body { margin: 0 auto; width: $siteWidth;}.content { float: left; width: $siteWidth - ($sidebarWidth+$gutterWidth);}.sidebar { float: left; margin-left: $gutterWidth; width: $sidebarWidth;} |
Less CSS
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
@siteWidth: 1024px;@gutterWidth: 20px;@sidebarWidth: 300px; body { margin: 0 auto; width: @siteWidth;}.content { float: left; width: @siteWidth - (@sidebarWidth+@gutterWidth);}.sidebar { float: left; margin-left: @gutterWidth; width: @sidebarWidth;} |
Stylus
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
siteWidth = 1024px;gutterWidth = 20px;sidebarWidth = 300px; body { margin: 0 auto; width: siteWidth;}.content { float: left; width: siteWidth - (sidebarWidth+gutterWidth);}.sidebar { float: left; margin-left: gutterWidth; width: sidebarWidth;} |
實際效果
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
body { margin: 0 auto; width: 1024px;}.content { float: left; width: 704px;}.sidebar { float: left; margin-left: 20px; width: 300px;} |
錯誤報告
如果你經常 CSS 你會發現很難找到 CSS 中錯誤的地方,這也是預處理框架的好處,它會報告錯誤,你可以從這篇文章中學習如何讓 CSS 框架錯誤報告。
注釋
以上三種框架都支持形如 /* */ 的多行注釋以及 // 的單行注釋。
英文原文,OSCHINA 原創翻譯
文章列表

留言列表
