在前一篇
install-less 安裝好Less後,接下來就來基本介紹Less的功能用法吧。
1. Variables
Variables的意思是變數,我們可以使用一個變數去設定為某個值後,在需要的地方指定改變數,則Less會把該變數的值代入。
Less
1
2
3
4
5
| @color: #4D926F;
.box{
background: @color;
} |
Compiled css
1
2
3
| .box{
background: #4D926F;
} |
2. Mixins
Mixins的意思是funciton,我們可以先定義好function後,在需要的地方直接寫上該function後,Less則會把該function裡面的css代入。
Less
1
2
3
4
5
6
7
| .width(@width:100%){
width: @width;
}
.box{
.width(50%);
} |
Compiled css
3. Nested Rules
Mixins的意思是巢狀規則,以往在css寫父類裡面的子類或更子類的class話,則必須寫好長一排css,而且也不直覺不容易瞭解,現在使用Less的巢狀規則後,在寫的時候就很清楚目前寫到哪了,而且也很容易瞭解。
Less
1
2
3
4
5
6
7
8
| .box{
p{
font-size: 12px;
a{
text-decoration: none;
}
}
} |
Compiled css
1
2
3
4
5
6
| .box p{
font-size: 12px;
}
.box p a{
text-decoration: none;
} |
4. Operations
Operations的意思是運算,在Less裡指定的一個變數值後,如果想在使用時再對這變數改變值的話,可以在該變數後面加上運算進去,Less則會運算後將結果代入。
Less
1
2
3
4
5
6
7
8
| @border: 1px;
@color: #111;
.box{
border-color:@color + #222;
border-width:@border * 3;
border-style:solid;
} |
Compiled css
1
2
3
4
5
| .box{
border-color:#333;
border-width:3px;
border-style:solid;
} |
結論
以上就是Less基本的介紹,Less這類套件真的是改變寫css方式,尤其是
Nested Rules更是,讓在寫css的開發者可以很清楚的知道現在在寫哪一層,而且也容易可以從寫的less知道目前在寫的html結構,真的很推薦使用這類的套件。

相關文章