Less
什么是CSS预处理器
Less中文网: https://less.bootcss.com/
常见的CSS预处理器
- Less(Leaner Style Sheets) 、Sass 、Stylus
为什么需要less
什么是less
- Less 是一门 CSS 预处理语言,为 CSS 赋予了动态语言的特征。
- 它扩展了 CSS 语言,增加了变量、Mixin(混合)、嵌套、函数和运算等特性,使 CSS 更易维护和扩展
- 一句话:用类似 JS 的语法去写 CSS
less基本使用
- 在浏览器中直接运行
- 编写 .less 文件–>引入 .less 文件–> 引入less.js
- 运行注意点:
- 一定要先引入 less.css , 再引入 less.js
- 如果 less 代码是写到单独的文件中,一定要在服务端环境运行才能生效
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| .box { font-size: 20px; } .center { text-align: center; } .father { width: 100px; height: 100px; .box; .son { color: red; .center; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| .box { font-size: 20px; } .center { text-align: center; } .father { width: 100px; height: 100px; font-size: 20px; } .father .son { color: red; text-align: center; }
|
提前预编译
Less的注释
- 单行注释和多行注释:
- 单行注释,不会被编译到 .css文件中
- 多行注释,会编译到 .css 文件中( 在 css 中仅支持 /**/ 注释)
Less的变量
1 2 3 4 5 6 7 8
| @width:200px; @height:200px; .box{ width:@width; height:@height; }
|
1 2 3 4 5 6
| .box { width: 200px; height: 200px; }
|
- 变量的赋值 (和 js 一样可以将一个变量赋值给另外一个变量)
1 2 3 4 5 6 7 8
| @width:200px; @height:@width; .box{ width:@width; height:@height; }
|
1 2 3 4 5 6
| .box { width: 200px; height: 200px; }
|
- 局部变量和全局变量:
- 和 js 一样, less 中的变量也有全局变量和局部变量, 定义在 { } 外面的就是全局的变量,什么地方都可以使用, 定义在 { } 里面的就是局部变量,只能在 { } 中使用
1 2 3 4 5 6 7 8 9
| @width:100px; .box{ @width:200px; width:@width; height:100px; }
|
1 2 3 4 5 6 7
| .box { width: 200px; height: 100px; }
|
- 注意定: less 中的变量是延迟加载的,写到后面也能在前面使用
1 2 3 4 5 6 7
| .box{ width:@width; height:100px; }
@width:200px;
|
1 2 3 4 5 6
| .box { width: 200px; height: 100px; }
|
1 2 3 4 5 6 7 8
| @width:100px; .box{ width:@width; height:100px; }
@width:200px;
|
1 2 3 4 5 6
| .box { width: 200px; height: 100px; }
|
Less变量的插值
- 什么是变量插值?
- 在 less 中如果属性的取值可以直接使用变量,但是如果是属性名称或者选择器名称并不能直接使用变量, 如果属性名称或者选择器名称想使用变量中保存的值,那么必须使用变量插值的格式
- 变量作为样式的属性名:
1 2 3 4 5 6 7 8
| @width:200px; @height:height; .box{ width:@width; @{height}:100px; }
|
1 2 3 4 5 6
| .box { width: 200px; height: 100px; }
|
1 2 3 4 5 6 7 8
| @width:200px; @div:div; @{div}{ width:@width; height:100px; }
|
1 2 3 4 5 6
| div { width: 200px; height: 100px; }
|
Less的运算
1 2 3 4
| .box{ width:(200px * 0.5); height:(200px / 2); }
|
1 2 3 4
| .box { width: 100px; height: 100px; }
|
Less的混合
- 什么是 less 中的混合 ( Mix in )
- 将需要重复使用的代码封装到一个类中,在需要使用的地方调用封装好的类即可, 在预处理的时候 less 会自动将用到的封装好的类中的代码拷贝过来
- 本质就是 ctrl+c –> ctrl +v
- less 中混合的注意点:
- 如果混合名称的后面没有 (),那么在预处理的时候,会保留混合的代码
- 如果混合名称的后面加上 (),那么在预处理的时候,不会保留混合的代码
- 使用混合类时,也可以在后面添加 (),也可以不加 (),效果同样
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| .box { color: red; } .center() { text-align: center; } .father { width: 100px; height: 100px; .box; .center; }
|
1 2 3 4 5 6 7 8 9 10 11 12
| .box { color: red; } .father { width: 100px; height: 100px; color: red; text-align: center; }
|
Less带参数混合
1 2 3 4 5 6 7 8 9 10 11
| .snail(@w,@h,@c){ width:@w; height:@h; color:@c; } .box1{ .snail(200px,200px,green) } .box2{ .snail(100px,100px,yellow) }
|
1 2 3 4 5 6 7 8 9 10
| .box1 { width: 200px; height: 200px; color: green; } .box2 { width: 100px; height: 100px; color: yellow; }
|
- 带参数以及默认值的混合:
- 若传递了对应的参数,就用传递的参数
- 若不传参则使用默认值
1 2 3 4 5 6 7 8 9 10 11 12
| .snail(@w:100px,@h:100px,@c:red){ width:@w; height:@h; color:@c; } .box1{ .snail(200px,200px,green) } .box2{ .snail(@c:yellow) }
|
1 2 3 4 5 6 7 8 9 10 11
| .box1 { width: 200px; height: 200px; color: green; } .box2 { width: 100px; height: 100px; color: yellow; }
|
混合的可变参数
1 2 3 4 5 6 7 8
| .animate(...){ transition:@arguments; } div{ width:100px; height:100px; .animate(all,4s,linear,0s); }
|
1 2 3 4 5
| div { width: 100px; height: 100px; transition: all 4s linear 0s; }
|
- 指定必须最少传递的参数个数
- 若传递的参数个数,少于必须传递的参数个数,就会报错
- 若使用了
...
,则必须写到形参的最后面
1 2 3 4 5 6 7 8
| .animate(@name,@time,...){ transition:@arguments; } div{ width:100px; height:100px; .animate(all,4s,linear,0s); }
|
1 2 3 4 5
| div { width: 100px; height: 100px; transition: all 4s linear 0s; }
|
Less的匹配模式
混合的按需匹配模式:
- 就是通过混合的第一个字符串形参,来确定具体要执行哪一个同名混合
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| .triangle(Down,@width,@color){ width:0; height:0; border-width:@width; border-style:solid solid solid solid; border-color:@color transparent transparent transparent; } .triangle(Top,@width,@color){ width:0; height:0; border-width:@width; border-style:solid solid solid solid; border-color:transparent transparent @color transparent; } .div{ .triangle(Top,80px,red) }
|
1 2 3 4 5 6 7 8 9
| .div { width: 0; height: 0; border-width: 80px; border-style: solid solid solid solid; border-color: transparent transparent red transparent; }
|
混合的通用匹配模式
- 无论同名的哪一个混合被匹配了,都会先执行通用匹配模式中的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
.triangle(@_,@width,@color){ width:0; height:0; border-style:solid solid solid solid; } .triangle(Down,@width,@color){ width:0; height:0; border-width:@width; border-style:solid solid solid solid; border-color:@color transparent transparent transparent; } .triangle(Top,@width,@color){ width:0; height:0; border-width:@width; border-style:solid solid solid solid; border-color:transparent transparent @color transparent; } .div{ .triangle(Top,80px,red) }
|
1 2 3 4 5 6 7 8 9
|
.div { width: 0; height: 0; border-width: 80px; border-style: solid solid solid solid; border-color: transparent transparent red transparent; }
|
在less中导入less
- 在 less 文件中导入其他 less 文件
- 导入文件时,后缀名
.less
可以省略
1 2 3 4
| @import "triangle.less"; .div{ .triangle(Top,80px,red) }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
.div { width: 0; height: 0; border-width: 80px; border-style: solid solid solid solid; border-color: transparent transparent red transparent; }
.div { width: 0; height: 0; border-width: 80px; border-style: solid solid solid solid; border-color: transparent transparent red transparent; }
|
Less中的内置函数
isnumber(56px); // =>true是否含数字
isstring(“string”); // =>true
iscolor(#ffO); // =>true
iscolor(blue); // =>true
iskeyword (keyword) ; // =>true
isurl(url(…)); // =>true
ispixe1 (56px); // =true
isem(7.8em); // =>true
ispercentage(7.8%); // =>true
isunit(4rem,rem); // =>true是否为指定单位
isruleset (@rules); // =>true是否为变量
简单的变量使用
1 2 3 4 5 6
| @str:"./images/1.jpg"; .box{ width:200px; height:200px; background:url(@str); }
|
1 2 3 4 5
| .box { width: 200px; height: 200px; background: url("images/1.jpg"); }
|
replace函数
1 2 3 4 5 6 7 8
| @str:"./images/1.jpg";
@str2:replace(@str,"1","2"); .box{ width:200px; height:200px; background:url(@str2); }
|
1 2 3 4 5 6
| .box { width: 200px; height: 200px; background: url("images/2.jpg"); }
|
颜色饱和度
1 2 3 4 5 6 7 8 9 10
| .box{ width:200px; height:200px; background:desaturate(yellow,50%); &:hover{ background:saturate(yellow,50%); } }
|
1 2 3 4 5 6 7 8 9 10
| .box { width: 200px; height: 200px; background: #bfbf40; } .box:hover { background: #ffff00; }
|
Less的层级结构
后代选择器
- 如果在某一个选择器的 { } 中直接写上了其它的选择器,会自动转换成后代选择器
1 2 3 4 5 6 7 8
| .father{ width:100px; height:100px; .son{ font-size:20px; color:red; } }
|
1 2 3 4 5 6 7 8
| .father { width: 100px; height: 100px; } .father .son { font-size: 20px; color: red; }
|
:hover
- 这里的 & 的作用,是告诉 less 在转换的时候不用后代来转换,直接拼接在当前选择器的后面即可
1 2 3 4 5 6 7 8 9 10
| .father{ width:100px; height:100px; .son{ font-size:20px; &:hover{ color:red; } } }
|
1 2 3 4 5 6 7 8 9 10
| .father { width: 100px; height: 100px; } .father .son { font-size: 20px; } .father .son:hover { color: red; }
|
::before
1 2 3 4 5 6 7 8 9 10 11
| .father{ width:100px; height:100px; &::before{ content:"子元素"; display:block; background:red; width:100px; height:100px; } }
|
1 2 3 4 5 6 7 8 9 10 11
| .father { width: 100px; height: 100px; } .father::before { content: "子元素"; display: block; background: red; width: 100px; height: 100px; }
|
Less中的继承
- less 中的继承和 less 中混合的区别
- 使用时的语法格式不同
- 转换之后的结果不同 (混合是直接拷贝,继承是并集选择器)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| .center{ position:absolute; left:50%; top:50%; transform:translate(-50%,-50%); } .father:extend(.center){ width:300px; height:300px; background:red; .son:extend(.center){ width:200px; height:200px; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| .center, .father, .father .son { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); } .father { width: 300px; height: 300px; background: red; } .father .son { width: 200px; height: 200px; }
|
Less中的条件判断
比较运算符
1 2 3 4 5 6 7 8 9
| .size(@width,@height) when (@width = 100px){ width:@width; height:@height; } .box{ .size(100px,50px); background:red; }
|
1 2 3 4 5 6
| .box { width: 100px; height: 50px; background: red; }
|
逻辑或
1 2 3 4 5 6 7 8 9
| .size(@width,@height) when (@width = 100px),(@height = 100px){ width:@width; height:@height; } .box{ .size(100px,100px); background:red; }
|
1 2 3 4 5 6
| .box { width: 100px; height: 100px; background: red; }
|
逻辑与
1 2 3 4 5 6 7 8 9
| .size(@width,@height) when (@width = 100px)and(@height = 100px){ width:@width; height:@height; } .box{ .size(100px,100px); background:red; }
|
1 2 3 4 5 6
| .box { width: 100px; height: 100px; background: red; }
|
使用检验函数
1 2 3 4 5 6 7 8 9
| .size(@width,@height) when (ispixel(@width)){ width:@width; height:@height; } .box{ .size(100px,100px); background:red; }
|
1 2 3 4 5 6
| .box { width: 100px; height: 100px; background: red; }
|
完结