摘要:
作為 CSS 的一種擴展,Less 不僅完全兼容 CSS 語法,而且連新增的特性也是使用 CSS 語法。這樣的設計使得學習 Less 很輕松,而且你可以在任何時候回退到 CSS。less文件是以less作為文件后綴名,HTML引用時可以像css那樣引用,如下:
<link rel="stylesheet/less" type="text/css" href="styles.less">
注意:本文描述的一切都是基於1.4.0版本,除非有特殊標明。
變量:
變量的作用就是把值定義在一個地方,然后在各處使用,這樣能讓代碼更易維護,如下:
// Variables @link-color: #428bca; // sea blue // 用法 a:link { color: @link-color; } .widget { color: #fff; background: @link-color; }
上面代碼將顏色#428bca賦給一個變量@link-color,然后在color屬性中使用變量,對應的css如下:
a:link { color: #428bca; } .widget { color: #fff; background: #428bca; }
變量不僅可以用在屬性值上,也可以用在選擇元素名,屬性名(1.6.0支持),url和import方法。如下:
選擇元素名:
// Variables @mySelector: banner; // Usage .@{mySelector} { font-weight: bold; line-height: 40px; margin: 0 auto; }
編譯后為
.banner { font-weight: bold; line-height: 40px; margin: 0 auto; }
url:
// Variables @images: "../img"; // 用法 body { color: #444; background: url("@{images}/white-sand.png"); }
編譯后
body { color: #444; background: url("../img/white-sand.png"); }
@import:
// Variables @themes: "../../src/themes"; // Usage @import "@{themes}/tidal-wave.less";
編譯后
@import "../../src/themes/tidal-wave.less";
屬性名:
@property: color; .widget { @{property}: #0ee; background-@{property}: #999; }
編譯后
.widget { color: #0ee; background-color: #999; }
變量的變量名也可以是變量,如下:
@fnord: "I am fnord.";
@var: "fnord";
content: @@var;
編譯后
content: "I am fnord.";
延遲加載:
變量支持延遲加載,所以你可以在變量定義之前使用。如下:
.lazy-eval { width: @var; } @var: @a; @a: 9%;
或者
.lazy-eval-scope { width: @var; @a: 9%; } @var: @a; @a: 100%;
上面兩個都會被編譯成如下
.lazy-eval-scope { width: 9%; }
問什么第二個也會被編譯成上面的css,這是因為當一個變量被定義兩次時,最后一次定義的生效。就類似於css中,對同一個元素定義不同的css樣式,最后定義的生效。再比如下面這個
@var: 0; .class1 { @var: 1; .class { @var: 2; three: @var; @var: 3; } one: @var; }
編譯后
.class1 .class { three: 3; } .class { one: 1; }
Extend:
擴展選擇器是less的偽類選擇器,他會復制當前選擇器,定義新的樣式,而原來的不便
nav ul { &:extend(.inline); background: blue; } .inline { color: red; }
編譯后
nav ul { background: blue; } .inline, nav ul { color: red; }
語法:
.a:extend(.b) {} 也可以這樣使用 .a { &:extend(.b); }
.e:extend(.f) {} .e:extend(.g) {} // 上面等價於下面 .e:extend(.f, .g) {}
嵌套選擇器:
.bucket { tr { color: blue; } } .some-class:extend(.bucket tr) {}
編譯后
.bucket tr, .some-class { color: blue; }
精確匹配:
.a.class, .class.a, .class > .a { color: blue; } .test:extend(.class) {} // 不會匹配任何選擇
nth:
:nth-child(1n+3) { color: blue; } .child:extend(n+3) {}
編譯后
:nth-child(1n+3) { color: blue; }
注意:1n+3與n+3在css中是等價的,但是在less中不等價。
屬性選擇器:
[title=identifier] { color: blue; } [title='identifier'] { color: blue; } [title="identifier"] { color: blue; } .noQuote:extend([title=identifier]) {} .singleQuote:extend([title='identifier']) {} .doubleQuote:extend([title="identifier"]) {}
編譯后
[title=identifier], .noQuote, .singleQuote, .doubleQuote { color: blue; } [title='identifier'], .noQuote, .singleQuote, .doubleQuote { color: blue; } [title="identifier"], .noQuote, .singleQuote, .doubleQuote { color: blue; }
注意:less中不區分單引號雙引號
關鍵字all:
.a.b.test, .test.c { color: orange; } .test { &:hover { color: green; } } .replacement:extend(.test all) {}
編譯后
.a.b.test, .test.c, .a.b.replacement, .replacement.c { color: orange; } .test:hover, .replacement:hover { color: green; }
變量選擇器:
@variable: .bucket; @{variable} { // interpolated selector color: blue; } .some-class:extend(.bucket) {}// 不會匹配任何選擇元素
.bucket { color: blue; } .some-class:extend(@{variable}) {} // 不會匹配任何元素 @variable: .bucket;
注意:extend不匹配變量。
@media:
@media print { .screenClass:extend(.selector) {} // extend inside media .selector { color: black; } } .selector { color: red; } @media screen { .selector { color: blue; } }
編譯后
@media print { .selector, .screenClass { color: black; } } .selector { color: red; } @media screen { .selector { color: blue; } }
注意:extend只能匹配@media中前面定義的,在后面定義的將忽略。
使用extend重寫樣式:
在開發中我們會定義一些通用樣式,然后單獨樣式在添加class,使用css的后面覆蓋前面的原理來實現樣式。extend也可以實現這種效果,如下:
.animal { background-color: black; color: white; } .bear { &:extend(.animal); background-color: brown; }
減少css代碼:
.my-inline-block() { display: inline-block; font-size: 0; } .thing1 { .my-inline-block; } .thing2 { .my-inline-block; }
編譯后:
.thing1 { display: inline-block; font-size: 0; } .thing2 { display: inline-block; font-size: 0; }
使用extend
.my-inline-block { display: inline-block; font-size: 0; } .thing1 { &:extend(.my-inline-block); } .thing2 { &:extend(.my-inline-block); }
編譯后
.my-inline-block, .thing1, .thing2 { display: inline-block; font-size: 0; }