color: red;
}
.nav {
background: #ccc;
color: red;
}
Css 中的 class, id 或者元素属性集都可以用同样的方式引入。
3、带参数混合
在 less 中,你可以把 class 当做是函数,而函数是可以带参数的。
.border-radius (@radius) {
border-radius: @radius;
-moz-border-radius: @radius;
-webkit-border-radius: @radius;
}
#header {
.border-radius(4px);
}
.button {
.border-radius(6px);
}
最后输出:
#header {
border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
}
.button {
border-radius: 6px;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
}
我们还可以给参数设置默认值:
.border-radius (@radius: 5px) {
border-radius: @radius;
-moz-border-radius: @radius;
-webkit-border-radius: @radius;
}
#header {
.border-radius;
}
最后输出:
#header {
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-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 包含了所有传递进来的参数,当你不想处理个别的参数时,这个将很有用。
.border(@width:0,@style:solid,@color:red){
border:@arguments;
}
.demo{
.border(2px);
}
输出:
.demo {
border: 2px solid #ff0000;
}
混合还有许多高级的应用,如模式匹配等。在这里就不介绍了,只讲些基础的东西。
4、嵌套规则
Less 可以让我们用嵌套的方式来写 css。下面是我们平时写的 css:
#header h1 {
font-size: 26px;
font-weight: bold;
}
#header p {
font-size: 12px;
}
#header p a {
text-decoration: none;
}
#header p a:hover {
border-width: 1px;
}
用 less 我们就可以这样写:
#header {
h1 {
font-size: 26px;
font-weight: bold;
}
p {
font-size: 12px;
a {
text-decoration: none;
&:hover { border-width: 1px }
}
}
}
注意 & 符号的使用—如果你想写串联选择器,而不是写后代选择器,就可以用到 & 了。这点对伪类尤其有用如 :hover。
5、运算










