LESS 做爲 CSS 的一種形式的擴展,它並沒有閹割 CSS 的功能,而是在現有的 CSS 語法上,
添加了很多額外的功能,所以學習 LESS 是一件輕而易舉的事情,果斷學習之!
變量
很容易理解:
@nice-blue: #5B83AD;
@light-blue: @nice-blue + #111;
#header { color: @light-blue; }
輸出:
#header { color: #6c94be; }
甚至可以用變量名定義爲變量:
@var: 'I am fnord.';
content: @var;
解析後:
content: "I am fnord.";
請注意 LESS 中的變量爲完全的 ‘常量’ ,所以只能定義一次.
混合
在 LESS 中我們可以定義一些通用的屬性集爲一個class,然後在另一個class中去調用這些屬性. 下面有這樣一個class:
.bordered {
border-top: dotted 1px black;
border-bottom: solid 2px black;
}
那如果我們現在需要在其他class中引入那些通用的屬性集,那麼我們只需要在任何class中像下面這樣調用就可以了:
#menua {
color: #111;
.bordered;
}
.posta {
color: red;
.bordered;
}
.bordered class裏面的屬性樣式都會在 #menu a 和 .post a中體現出來:
#menua {
color: #111;
border-top: dotted 1px black;
border-bottom: solid 2px black;
}
.posta {
color: red;
border-top: dotted 1px black;
border-bottom: solid 2px black;
}
任何 CSS class, id 或者 元素 屬性集都可以以同樣的方式引入.
帶參數混合
在 LESS 中,你還可以像函數一樣定義一個帶參數的屬性集合:
.border-radius (@radius) {
border-radius: @radius;
-moz-border-radius: @radius;
-webkit-border-radius: @radius;
}
然後在其他class中像這樣調用它:
#header {
.border-radius(4px);
}
.button {
.border-radius(6px);
}
我們還可以像這樣給參數設置默認值:
.border-radius (@radius: 5px) {
border-radius: @radius;
-moz-border-radius: @radius;
-webkit-border-radius: @radius;
}
所以現在如果我們像這樣調用它的話:
#header {
.border-radius;
}
radius的值就會是5px.
你也可以定義不帶參數屬性集合,如果你想隱藏這個屬性集合,不讓它暴露到CSS中去,但是你還想在其他的屬性集合中引用,你會發現這個方法非常的好用:
.wrap () {
text-wrap: wrap;
white-space: pre-wrap;
white-space: -moz-pre-wrap;
word-wrap: break-word;
}
pre { .wrap }
輸出:
pre {
text-wrap: wrap;
white-space: pre-wrap;
white-space: -moz-pre-wrap;
word-wrap: break-word;
}
@arguments 變量
@arguments包含了所有傳遞進來的參數. 如果你不想單獨處理每一個參數的話就可以像這樣寫:
.box-shadow (@x: 0, @y: 0, @blur: 1px, @color: #000) {
box-shadow: @arguments;
-moz-box-shadow: @arguments;
-webkit-box-shadow: @arguments;
}
.box-shadow(2px, 5px);
將會輸出:
box-shadow: 2px 5px 1px #000;
-moz-box-shadow: 2px 5px 1px #000;
-webkit-box-shadow: 2px 5px 1px #000;
模式匹配和導引表達式
有些情況下,我們想根據傳入的參數來改變混合的默認呈現,比如下面這個例子:
.mixin (@s, @color) { ... }
.class {
.mixin(@switch, #888);
}
如果想讓.mixin根據不同的@switch值而表現各異,如下這般設置:
.mixin (dark, @color) {
color: darken(@color, 10%);
}
.mixin (light, @color) {
color: lighten(@color, 10%);
}
.mixin (@_, @color) {
display: block;
}
現在,如果運行:
@switch: light;
.class {
.mixin(@switch, #888);
}
就會得到下列CSS:
.class {
color: #a2a2a2;
display: block;
}
如上,.mixin就會得到傳入顏色的淺色。如果@switch設爲dark,就會得到深色。
具體實現如下:
第一個混合定義並未被匹配,因爲它只接受dark做爲首參
第二個混合定義被成功匹配,因爲它只接受light
第三個混合定義被成功匹配,因爲它接受任意值
只有被匹配的混合纔會被使用。變量可以匹配任意的傳入值,而變量以外的固定值就僅僅匹配與其相等的傳入值。
我們也可以匹配多個參數:
.mixin (@a) {
color: @a;
}
.mixin (@a, @b) {
color: fade(@a, @b);
}
Now if we call .mixin with a single argument, we will get the output of the first definition, but if we call it with two arguments, we will get the second definition, namely @a faded to @b.
引導
當我們想根據表達式進行匹配,而非根據值和參數匹配時,導引就顯得非常有用。如果你對函數式編程非常熟悉,那麼你很可能已經使用過導引。
爲了儘可能地保留CSS的可聲明性,LESS通過導引混合而非if/else語句來實現條件判斷,因爲前者已在@media query特性中被定義。
以此例做爲開始:
.mixin (@a) when (lightness(@a) >= 50%) {
background-color: black;
}
.mixin (@a) when (lightness(@a) < 50%) {
background-color: white;
}
.mixin (@a) {
color: @a;
}
when關鍵字用以定義一個導引序列(此例只有一個導引)。接下來我們運行下列代碼:
.class1 { .mixin(#ddd) }
.class2 { .mixin(#555) }
就會得到:
.class1 {
background-color: black;
color: #ddd;
}
.class2 {
background-color: white;
color: #555;
}
導引中可用的全部比較運算有: > >= = =< <。此外,關鍵字true只表示布爾真值,下面兩個混合是相同的:
.truth (@a) when (@a) { ... }
.truth (@a) when (@a = true) { ... }
除去關鍵字true以外的值都被視示布爾假:
.class {
.truth(40); // Will not match any of the above definitions.
}
導引序列使用逗號‘,’—分割,當且僅當所有條件都符合時,纔會被視爲匹配成功。
.mixin (@a) when (@a > 10), (@a < -10) { ... }
導引可以無參數,也可以對參數進行比較運算:
@media: mobile;
.mixin (@a) when (@media = mobile) { ... }
.mixin (@a) when (@media = desktop) { ... }
.max (@a, @b) when (@a > @b) { width: @a }
.max (@a, @b) when (@a < @b) { width: @b }
最後,如果想基於值的類型進行匹配,我們就可以使用is*函式:
.mixin (@a, @b: 0) when (isnumber(@b)) { ... }
.mixin (@a, @b: black) when (iscolor(@b)) { ... }
下面就是常見的檢測函式:
iscolor
isnumber
isstring
iskeyword
isurl
如果你想判斷一個值是純數字,還是某個單位量,可以使用下列函式:
ispixel
ispercentage
isem
最後再補充一點,在導引序列中可以使用and關鍵字實現與條件:
.mixin (@a) when (isnumber(@a)) and (@a > 0) { ... }
使用not關鍵字實現或條件
.mixin (@b) when not (@b > 0) { ... }
嵌套規則
LESS 可以讓我們以嵌套的方式編寫層疊樣式. 讓我們先看下下面這段 CSS:
#header { color: black; }
#header.navigation {
font-size: 12px;
}
#header.logo {
width: 300px;
}
#header.logo:hover {
text-decoration: none;
}
在 LESS 中, 我們就可以這樣寫:
#header {
color: black;
.navigation {
font-size: 12px;
}
.logo {
width: 300px;
&:hover { text-decoration: none }
}
}
或者這樣寫:
#header { color: black;
.navigation { font-size: 12px }
.logo { width: 300px;
&:hover { text-decoration: none }
}
}
代碼更簡潔了,而且感覺跟DOM結構格式有點像.
注意 & 符號的使用—如果你想寫串聯選擇器,而不是寫後代選擇器,就可以用到&了. 這點對僞類尤其有用如:hover 和 :focus.
例如:
.bordered {
&.float {
float: left;
}
.top {
margin: 5px;
}
}
會輸出
.bordered.float {
float: left;
}
.bordered.top {
margin: 5px;
}
運算
任何數字、顏色或者變量都可以參與運算. 來看一組例子:
@base: 5%;
@filler: @base * 2;
@other: @base + @filler;
color: #888 / 4;
background-color: @base-color + #111;
height: 100% / 2 + @filler;
LESS 的運算已經超出了我們的期望,它能夠分辨出顏色和單位。如果像下面這樣單位運算的話:
@var: 1px + 5;
LESS 會輸出 6px.
括號也同樣允許使用:
width: (@var + 5) * 2;
並且可以在複合屬性中進行運算:
border: (@width * 2) solid black;
Color 函數
LESS 提供了一系列的顏色運算函數. 顏色會先被轉化成 HSL 色彩空間, 然後在通道級別操作:
lighten(@color, 10%); // return a color which is 10% *lighter* than @color
darken(@color, 10%); // return a color which is 10% *darker* than @color
saturate(@color, 10%); // return a color 10% *more* saturated than @color
desaturate(@color, 10%); // return a color 10% *less* saturated than @color
fadein(@color, 10%); // return a color 10% *less* transparent than @color
fadeout(@color, 10%); // return a color 10% *more* transparent than @color
fade(@color, 50%); // return @color with 50% transparency
spin(@color, 10); // return a color with a 10 degree larger in hue than @color
spin(@color, -10); // return a color with a 10 degree smaller hue than @color
mix(@color1, @color2); // return a mix of @color1 and @color2
使用起來相當簡單:
@base: #f04615;
.class {
color: saturate(@base, 5%);
background-color: lighten(spin(@base, 8), 25%);
}
你還可以提取顏色信息:
hue(@color); // returns the `hue` channel of @color
saturation(@color); // returns the `saturation` channel of @color
lightness(@color); // returns the 'lightness' channel of @color
如果你想在一種顏色的通道上創建另一種顏色,這些函數就顯得那麼的好用,例如:
@new: hsl(hue(@old), 45%, 90%);
@new 將會保持 @old的 色調, 但是具有不同的飽和度和亮度.
Math 函數
LESS提供了一組方便的數學函數,你可以使用它們處理一些數字類型的值:
round(1.67); // returns `2`
ceil(2.4); // returns `3`
floor(2.6); // returns `2`
如果你想將一個值轉化爲百分比,你可以使用percentage 函數:
percentage(0.5); // returns `50%`
命名空間
有時候,你可能爲了更好組織CSS或者單純是爲了更好的封裝,將一些變量或者混合模塊打包起來, 你可以像下面這樣在#bundle中定義一些屬性集之後可以重複使用:
#bundle {
.button () {
display: block;
border: 1px solid black;
background-color: grey;
&:hover { background-color: white }
}
.tab { ... }
.citation { ... }
}
你只需要在 #header a中像這樣引入 .button:
#headera {
color: orange;
#bundle > .button;
}
作用域
LESS 中的作用域跟其他編程語言非常類似,首先會從本地查找變量或者混合模塊,如果沒找到的話會去父級作用域中查找,直到找到爲止.
@var: red;
#page {
@var: white;
#header {
color: @var; // white
}
}
#footer {
color: @var; // red
}
註釋
CSS 形式的註釋在 LESS 中是依然保留的:
/* Hello, I’m a CSS-style comment */.class { color: black }
LESS 同樣也支持雙斜線的註釋, 但是編譯成 CSS 的時候自動過濾掉:
// Hi, I’m a silent comment, I won’t show up in your CSS.class { color: white }
Importing
你可以在main文件中通過下面的形勢引入 .less 文件, .less 後綴可帶可不帶:
@import "lib.less";
@import "lib";
如果你想導入一個CSS文件而且不想LESS對它進行處理,只需要使用.css後綴就可以:
@import "lib.css";
這樣LESS就會跳過它不去處理它.
字符串插值
變量可以用類似ruby和php的方式嵌入到字符串中,像@{name}這樣的結構:
@base-url: "http://assets.fnord.com";
background-image: url("@{base-url}/images/bg.png");
避免編譯
有時候我們需要輸出一些不正確的CSS語法或者使用一些 LESS不認識的專有語法.
要輸出這樣的值我們可以在字符串前加上一個 ~, 例如:
.class {
filter: ~"ms:alwaysHasItsOwnSyntax.For.Stuff()";
}
我們可以將要避免編譯的值用 “”包含起來,輸出結果爲:
.class {
filter: ms:alwaysHasItsOwnSyntax.For.Stuff();
}
其他的詳見less中國官網: