Sass(Syntactically Awesome Stylesheets)是一套動態樣式語言。它的寫法與 CSS 幾乎一致,卻多了許多 CSS 本身不具備的能力,因此比手寫 CSS 更省力,也更容易閱讀與維護。Sass 早期的語法風格近似 Haml,走的是縮排路線,最初的目的就是為了讓 HTML 與 CSS 的撰寫速度更快。
Less 同樣屬於動態樣式語言,它替 CSS 補上了變數、繼承、運算、函式等程式化特性。Less 的執行位置很靈活:既可以在瀏覽器端透過 less.js 即時編譯(支援 IE6+、Webkit、Firefox 等),也可以在伺服器端搭配 Node.js 預先編譯成 CSS。

Sass 最初的語法與 Haml 一樣採用縮排,對習慣大括號思維的前端工程師來說並不直覺,而且既有的 CSS 程式碼無法直接搬進來。於是 Sass 在第三版調整了語法規則,衍生出 SCSS(Sassy CSS):它與原本的 Sass 語法相容,只是把縮排換成了大括號與分號。
寫法對照如下。
Sass(縮排語法):
.content
margin: 10px auto
h1
font-size: 24px
SCSS(大括號語法):
.content {
margin: 10px auto;
h1 { font-size: 24px; }
}
@,SCSS 使用 $;兩者的作用域規則也有差別。@if / @else、@for;Less 不支援這類語法。/* Sass 的條件判斷 */
@if lightness($color) > 30% {
background-color: #000;
} @else {
background-color: #fff;
}
/* Sass 的 for 迴圈 */
@for $i from 1 to 10 {
.border-#{$i} {
border: #{$i}px solid blue;
}
}
可以把重複使用的樣式值定義成變數,之後隨處引用。
/* Less */
@color: #4d926f;
#header { color: @color; }
h2 { color: @color; }
/* SCSS */
$color: #4d926f;
#header { color: $color; }
h2 { color: $color; }
/* 編譯後的 CSS */
#header { color: #4d926f; }
h2 { color: #4d926f; }
混合能把一段定義好的樣式整批帶入另一個選擇器,達到類似繼承的效果,還可以傳入參數,用法接近函式。
/* Less */
.rounded-corners(@radius: 5px) {
border-radius: @radius;
-webkit-border-radius: @radius;
-moz-border-radius: @radius;
}
#header { .rounded-corners; }
#footer { .rounded-corners(11px); }
/* SCSS */
@mixin rounded-corners($radius: 5px) {
border-radius: $radius;
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
}
#header { @include rounded-corners; }
#footer { @include rounded-corners(11px); }
/* 編譯後的 CSS */
#header { border-radius: 5px; -webkit-border-radius: 5px; -moz-border-radius: 5px; }
#footer { border-radius: 11px; -webkit-border-radius: 11px; -moz-border-radius: 11px; }
在選擇器內部再嵌套選擇器,可以大幅減少重複的前綴,結構也更清楚。
#header {
h1 { font-size: 26px; font-weight: bold; }
p {
font-size: 12px;
a {
text-decoration: none;
&:hover { border-width: 1px; }
}
}
}
/* 編譯後的 CSS */
#header h1 { font-size: 26px; font-weight: bold; }
#header p { font-size: 12px; }
#header p a { text-decoration: none; }
#header p a:hover { border-width: 1px; }
支援加、減、乘、除四則運算,數值與顏色都能參與計算,方便表達屬性之間的複雜關係。
/* Less */
@the-border: 1px;
@base-color: #111;
@red: #842210;
#header {
color: @base-color * 3;
border-left: @the-border;
border-right: @the-border * 2;
}
#footer {
color: @base-color + #003300;
border-color: desaturate(@red, 10%);
}
/* SCSS 只需把 @ 換成 $ */
$the-border: 1px;
$base-color: #111;
$red: #842210;
/* 編譯後的 CSS */
#header { color: #333; border-left: 1px; border-right: 2px; }
#footer { color: #114411; border-color: #7d2717; }
兩者都提供一系列顏色運算函式。運算時顏色會先轉成 HSL 色彩空間,再針對各通道調整:
lighten(@color, 10%); /* 亮度提高 10% */ darken(@color, 10%); /* 亮度降低 10% */ saturate(@color, 10%); /* 飽和度提高 10% */ desaturate(@color, 10%); /* 飽和度降低 10% */ fadein(@color, 10%); /* 不透明度提高 10% */ fadeout(@color, 10%); /* 不透明度降低 10% */ spin(@color, 10); /* 色相增加 10 度 */ spin(@color, -10); /* 色相減少 10 度 */
SCSS 完全支援這些函式,只要把 @ 改成 $ 即可。
兩者的變數都會隨作用域改變,行為卻有一個關鍵差異。
/* Less:#header 內重新定義 @color,#footer 不受影響 */
@color: #00c; /* 藍色 */
#header {
@color: #c00; /* 紅色 */
border: 1px solid @color; /* 紅色邊框 */
}
#footer {
border: 1px solid @color; /* 仍然是藍色邊框 */
}
/* SCSS:在 #header 內重新定義後,該變數之後的值會被全域改寫 */
$color: #00c;
#header {
$color: #c00;
border: 1px solid $color; /* 紅色邊框 */
}
#footer {
border: 1px solid $color; /* 也變成紅色 */
}
也就是說,Less 的變數只在定義它的區塊內有效,離開該區塊就會回到原值;而 Sass 在區塊內重新賦值後,此處之後的同一變數都會被改寫。這一點在維護大型樣式表時要特別留意。
/* Less */ @import "lib.less"; @import "lib"; /* SCSS:被引入的檔案建議以 _ 開頭命名,例如 _rounded.scss */ @import "rounded";
SCSS 需要 Ruby 環境,安裝指令如下:
$ gem install sass
完成後即可開始編譯,最常用的是 watch 模式:
$ sass --watch style.scss:style.css
此外還 can 搭配 Compass 這個成熟的框架,能省下大量重複工作。
引入 .less 檔時,要把 rel 屬性設為 stylesheet/less:
<link rel="stylesheet/less" type="text/css" href="styles.less">
接著到 Less 官網下載 less.js 並引入:
<script src="less.js" type="text/javascript"></script>
要注意順序:.less 樣式檔必須放在 less.js 之前引入,否則編譯會失敗。
最簡單的方式是透過 npm(Node 的套件管理器)安裝:
$ npm install less $ npm install less@latest # 安裝最新穩定版
安裝後可在命令列呼叫編譯器:
$ lessc styles.less # 輸出到 stdout $ lessc styles.less > styles.css # 存成檔案 $ lessc -x styles.less > styles.css # 加上 -x 可壓縮輸出
當然,前面提到的 Less.app、CodeKit.app 這類圖形化工具會更直覺省事。