1. 安装 Less
你可以通过 npm 安装 Less:
npm install -g less
2. 变量
Less 允许你使用变量来存储值,以便在整个样式表中重复使用。变量以 @ 开头。
@primary-color: #3498db;
.header {
color: @primary-color;
}
3. 嵌套规则
Less 允许你在样式规则中嵌套其他规则,以提高样式表的可读性。
.nav {
background-color: #333;
a {
color: #fff;
text-decoration: none;
&:hover {
text-decoration: underline;
}
}
}
4. 混合器(Mixins)
混合器允许你定义可重用的样式块。
.border-radius(@radius) {
border-radius: @radius;
-webkit-border-radius: @radius;
-moz-border-radius: @radius;
}
.button {
.border-radius(5px);
}
5. 导入
Less 允许你将样式表拆分为多个文件,并通过 @import 导入它们。
// variables.less
@primary-color: #3498db;
// styles.less
@import "variables";
.header {
color: @primary-color;
}
6. 运算
Less 支持基本的算术运算。
@base: 5px;
@padding: @base * 2;
.element {
padding: @padding;
}
7. 条件语句
Less 允许你使用条件语句根据不同的条件应用不同的样式。
@width: 10px;
.element {
width: @width;
@if @width > 5 {
color: red;
} @else {
color: blue;
}
}
8. 循环
Less 支持循环,可以用来生成一系列样式。
.loop(@index) when (@index > 0) {
.element-@{index} {
width: @index * 10px;
}
.loop(@index - 1);
}
.loop(5);
这只是 Less 的一些基本概念和语法,它还有更多的功能和特性。通过使用 Less,你可以更灵活地组织和管理你的样式表,提高代码的可维护性。
转载请注明出处:http://www.zyzy.cn/article/detail/4277/Less