LESS 的安装和基本配置。
展示 LESS 的安装和配置过程。
# 使用 npm 安装
npm install -g less
# 使用 yarn 安装
yarn global add less
# 编译单个文件
lessc styles.less styles.css
# 压缩输出
lessc --clean-css styles.less styles.min.css
# 监听文件变化
less-watch-compiler less css
LESS 的变量使用。
展示变量的使用。
// 定义变量
@primary-color: #333;
@secondary-color: #666;
@font-stack: Helvetica, sans-serif;
// 使用变量
body {
font: 100% @font-stack;
color: @primary-color;
}
// 嵌套使用
.container {
background-color: @secondary-color;
.header {
color: @primary-color;
}
}
LESS 的嵌套规则。
展示嵌套规则的使用。
// 嵌套规则
nav {
ul {
margin: 0;
padding: 0;
list-style: none;
}
li {
display: inline-block;
}
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}
// 父选择器 &
.button {
&-primary {
background-color: blue;
}
&-secondary {
background-color: gray;
}
&:hover {
opacity: 0.8;
}
}
LESS 的混合器使用。
展示混合器的使用。
// 定义混合器
.border-radius(@radius) {
-webkit-border-radius: @radius;
-moz-border-radius: @radius;
-ms-border-radius: @radius;
border-radius: @radius;
}
// 使用混合器
.box {
.border-radius(10px);
}
// 带默认值的混合器
.box-shadow(@x: 0, @y: 0, @blur: 0, @color: #000) {
-webkit-box-shadow: @x @y @blur @color;
-moz-box-shadow: @x @y @blur @color;
box-shadow: @x @y @blur @color;
}
.card {
.box-shadow(2px, 2px, 5px, rgba(0,0,0,0.3));
}
LESS 的运算功能。
展示运算的使用。
// 基本运算
@base: 5%;
@filler: @base * 2;
@other: @base + @filler;
// 颜色运算
@color: #888 / 4;
@background: @color + #111;
// 单位运算
@width: 100px;
@height: @width * 2;
.element {
width: @width;
height: @height;
color: @color;
background: @background;
}
LESS 的函数使用。
展示函数的使用。
// 颜色函数
@color: #428bca;
.element {
color: lighten(@color, 20%);
background-color: darken(@color, 10%);
border-color: fade(@color, 50%);
}
// 数学函数
@width: 100px;
@height: 200px;
.box {
width: percentage(@width / @height);
height: round(@height / 2);
}
// 字符串函数
@str: "Hello, World!";
.text {
content: replace(@str, "World", "LESS");
}
LESS 的命名空间。
展示命名空间的使用。
// 定义命名空间
#bundle {
.button {
display: block;
border: 1px solid black;
background-color: grey;
&:hover {
background-color: white;
}
}
.tab { ... }
.citation { ... }
}
// 使用命名空间
#header a {
color: orange;
#bundle > .button;
}
LESS 的导入功能。
展示导入的使用。
// 导入文件
@import "variables.less";
@import "mixins.less";
@import "components.less";
// 导入选项
@import (reference) "library.less";
@import (inline) "not-less-compatible.css";
@import (less) "less-compatible.css";
@import (css) "css-compatible.css";
@import (once) "library.less";
@import (multiple) "library.less";
LESS 项目的最佳结构。
展示项目结构的最佳实践。
styles/
├── variables/
│ ├── _colors.less
│ ├── _typography.less
│ └── _spacing.less
├── mixins/
│ ├── _grid.less
│ ├── _buttons.less
│ └── _forms.less
├── components/
│ ├── _buttons.less
│ ├── _cards.less
│ └── _forms.less
├── layout/
│ ├── _header.less
│ ├── _footer.less
│ └── _grid.less
├── pages/
│ ├── _home.less
│ └── _about.less
└── main.less
LESS 的性能优化技巧。
展示性能优化的最佳实践。
// 使用变量存储重复值
@primary-color: #333;
@secondary-color: #666;
// 使用混合器减少代码重复
.flex-center() {
display: flex;
justify-content: center;
align-items: center;
}
// 使用命名空间组织代码
#utilities {
.clearfix() {
&::after {
content: "";
display: table;
clear: both;
}
}
.text-truncate() {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
// 使用条件语句优化代码
.responsive(@breakpoint) {
& when (@breakpoint = mobile) {
@media (max-width: 600px) { @content(); }
}
& when (@breakpoint = tablet) {
@media (max-width: 900px) { @content(); }
}
& when (@breakpoint = desktop) {
@media (min-width: 901px) { @content(); }
}
}
建议按照章节顺序学习,每完成一个练习后再进行下一个。可以通过创建实际项目来实践所学知识。