做爲 CSS 的一種擴展,Less 不只徹底兼容 CSS 語法,並且連新增的特性也是使用 CSS 語法。這樣的設計使得學習 Less 很輕鬆,並且你能夠在任什麼時候候回退到 CSS。less文件是以less做爲文件後綴名,HTML引用時能夠像css那樣引用,以下:css
<link rel="stylesheet/less" type="text/css" href="styles.less">less
注意:本文描述的一切都是基於1.4.0版本,除非有特殊標明。ide
變量的做用就是把值定義在一個地方,而後在各處使用,這樣能讓代碼更易維護,以下:學習
// Variables @link-color: #428bca; // sea blue // 用法 a:link { color: @link-color; } .widget { color: #fff; background: @link-color; }
上面代碼將顏色#428bca賦給一個變量@link-color,而後在color屬性中使用變量,對應的css以下:url
a:link { color: #428bca; } .widget { color: #fff; background: #428bca; }
變量不只能夠用在屬性值上,也能夠用在選擇元素名,屬性名(1.6.0支持),url和import方法。以下:spa
選擇元素名:設計
// Variables @mySelector: banner; // Usage .@{mySelector} { font-weight: bold; line-height: 40px; margin: 0 auto; }
編譯後爲code
.banner { font-weight: bold; line-height: 40px; margin: 0 auto; }
url:blog
// Variables @images: "../img"; // 用法 body { color: #444; background: url("@{images}/white-sand.png"); }
編譯後three
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; }
擴展選擇器是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-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中不區分單引號雙引號
.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 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中前面定義的,在後面定義的將忽略。
在開發中咱們會定義一些通用樣式,而後單獨樣式在添加class,使用css的後面覆蓋前面的原理來實現樣式。extend也能夠實現這種效果,以下:
.animal { background-color: black; color: white; } .bear { &:extend(.animal); background-color: brown; }
.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; }