1、变量
@width: 10px;
@height: @width + 10px;
#header {
width: @width;
height: @height;
}
2、混合
.bordered {
border-top: dotted 1px black;
}
#menu a {
color: #111;
.bordered();
}
.post a {
color: red;
.bordered();
}
3、嵌套
#header {
.navigation {
}
.logo {
}
}
.clearfix {
display: block;
&:after {
}
}
4、@规则嵌套和冒泡
.component {
width: 300px;
@media (min-width: 768px) {
width: 600px;
@media (min-resolution: 192dpi) {
background-image: url(/img/retina2x.png);
}
}
@media (min-width: 1280px) {
width: 800px;
}
}
5、运算
// 所有操作数被转换成相同的单位
@conversion-1: 5cm + 10mm; // 结果是 6cm
@conversion-2: 2 - 3cm - 5mm; // 结果是 -1.5cm
// conversion is impossible
@incompatible-units: 2 + 5px - 3cm; // 结果是 4px
// example with variables
@base: 5%;
@filler: @base * 2; // 结果是 10%
@other: @base + @filler; // 结果是 15%
6、转义
@min768: ~"(min-width: 768px)";
.element {
@media @min768 {
font-size: 1.2rem;
}
}
编译为:
@media (min-width: 768px) {
.element {
font-size: 1.2rem;
}
}
7、函数
面这个例子将介绍如何利用 percentage 函数将 0.5 转换为 50%,将颜色饱和度增加 5%,以及颜色亮度降低 25% 并且色相值增加 8 等用法
@base: #f04615;
@width: 0.5;
.class {
width: percentage(@width); // returns `50%`
color: saturate(@base, 5%);
background-color: spin(lighten(@base, 25%), 8);
}
8、命名空间和访问符
#bundle() {
.button {}
.tab {}
.citation {}
}
9、映射
#colors() {
primary: blue;
secondary: green;
}
.button {
color: #colors[primary];
border: 1px solid #colors[secondary];
}
10、作用域
@var: red;
#page {
@var: white;
#header {
color: @var; // white
}
}
11、导入
@import "library"; // library.less
@import "typo.css";