less解析

变量

值变量

以 @ 开头 定义变量,并且使用时 直接 键入 @名称。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@color: #999;
@bgColor: skyblue;//不要添加引号
@width: 50%;
#wrap {
color: @color;
background: @bgColor;
width: @width;
}

/* 生成后的 CSS */
#wrap {
color: #999;
background: skyblue;
width: 50%;
}

选择器变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* Less */
@mySelector: #wrap;
@Wrap: wrap;
@{mySelector}{ //变量名 必须使用大括号包裹
color: #999;
width: 50%;
}
.@{Wrap}{
color:#ccc;
}
#@{Wrap}{
color:#666;
}

/* 生成的 CSS */
#wrap{
color: #999;
width: 50%;
}
.wrap{
color:#ccc;
}
#wrap{
color:#666;
}

属性变量

1
2
3
4
5
6
7
8
9
10
11
/* Less */
@borderStyle: border-style;
@Soild:solid;
#wrap{
@{borderStyle}: @Soild;//变量名 必须使用大括号包裹
}

/* 生成的 CSS */
#wrap{
border-style:solid;
}

url变量

1
2
3
4
5
6
7
8
9
10
/* Less */
@images: "../img";//需要加引号
body {
background: url("@{images}/dog.png");//变量名 必须使用大括号包裹
}

/* 生成的 CSS */
body {
background: url("../img/dog.png");
}

声明方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* Less */
@background: {background:red;};
#main{
@background();
}
@Rules:{
width: 200px;
height: 200px;
border: solid 1px red;
};
#con{
@Rules();
}

/* 生成的 CSS */
#main{
background:red;
}
#con{
width: 200px;
height: 200px;
border: solid 1px red;
}

变量运算

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/* Less */
@width:300px;
@color:#222;
#wrap{
width:@width-20;
height:@width-20*5;
margin:(@width-20)*5;
color:@color*2;
background-color:@color + #111;
}

/* 生成的 CSS */
#wrap{
width:280px;
height:200px;
margin:1400px;
color:#444;
background-color:#333;
}

变量作用域

1
2
3
4
5
6
7
8
9
10
11
12
/* Less */
@var: @a;
@a: 100%;
#wrap {
width: @var;
@a: 9%;
}

/* 生成的 CSS */
#wrap {
width: 9%;
}

用变量去定义变量

1
2
3
4
5
6
7
8
9
10
/* Less */
@fnord: "I am fnord.";
@var: "fnord";
#wrap::after{
content: @@var; //将@var替换为其值 content:@fnord;
}
/* 生成的 CSS */
#wrap::after{
content: "I am fnord.";
}

嵌套

&的用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* Less */
#header{
&:after{
content:"Less is more!";
}
.title{
font-weight:bold;
}
&_content{//理解方式:直接把 & 替换成 #header
margin:20px;
}
}
/* 生成的 CSS */
#header::after{
content:"Less is more!";
}
#header .title{ //嵌套了
font-weight:bold;
}
#header_content{//没有嵌套!
margin:20px;
}

媒体查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* Less */
#main{
//something...

@media screen{
@media (max-width:768px){
width:100px;
}
}
@media tv {
width:2000px;
}
}
/* 生成的 CSS */
@media screen and (maxwidth:768px){
#main{
width:100px;
}
}
@media tv{
#main{
width:2000px;
}
}

混合方法

无参数方法

1
2
3
4
5
6
.card(){
//something...
}
#wrap{
.card();
}
  1. . 与 # 皆可作为 方法前缀。
  2. 方法后写不写 () 看个人习惯。

默认参数方法

  1. Less 可以使用默认参数,如果 没有传参数,那么将使用默认参数。
  2. @arguments 犹如 JS 中的 arguments 指代的是 全部参数。
  3. 传的参数中 必须带着单位。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* Less */
.border(@a:10px,@b:50px,@c:30px,@color:#000){
border:solid 1px @color;
box-shadow: @arguments;//指代的是 全部参数
}
#main{
.border(0px,5px,30px,red);//必须带着单位
}
#wrap{
.border(0px);
}
#content{
.border;//等价于 .border()
}

/* 生成的 CSS */
#main{
border:solid 1px red;
box-shadow:0px,5px,30px,red;
}
#wrap{
border:solid 1px #000;
box-shadow: 0px 50px 30px #000;
}
#content{
border:solid 1px #000;
box-shadow: 10px 50px 30px #000;
}

方法的匹配模式

  1. 第一个参数 left 要会找到方法中匹配程度最高的,如果匹配程度相同,将全部选择,并存在着样式覆盖替换。
  2. 如果匹配的参数 是变量,则将会匹配,如 @_ 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/* Less */
.triangle(top,@width:20px,@color:#000){
border-color:transparent transparent @color transparent ;
}
.triangle(right,@width:20px,@color:#000){
border-color:transparent @color transparent transparent ;
}

.triangle(bottom,@width:20px,@color:#000){
border-color:@color transparent transparent transparent ;
}
.triangle(left,@width:20px,@color:#000){
border-color:transparent transparent transparent @color;
}
.triangle(@_,@width:20px,@color:#000){
border-style: solid;
border-width: @width;
}
#main{
.triangle(left, 50px, #999)
}
/* 生成的 CSS */
#main{
border-color:transparent transparent transparent #999;
border-style: solid;
border-width: 50px;
}

方法的命名空间

  1. 在 CSS 中> 选择器,选择的是 儿子元素,就是 必须与父元素 有直接血源的元素。
  2. 在引入命令空间时,如使用 > 选择器,父元素不能加 括号。
  3. 不得单独使用命名空间的方法 必须先引入命名空间,才能使用 其中方法。
  4. 子方法 可以使用上一层传进来的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* Less */
#card(){
background: #723232;
.d(@w:300px){
width: @w;

#a(@h:300px){
height: @h;//可以使用上一层传进来的方法
}
}
}
#wrap{
#card > .d > #a(100px); // 父元素不能加 括号
}
#main{
#card .d();
}
#con{
//不得单独使用命名空间的方法
//.d() 如果前面没有引入命名空间 #card ,将会报错

#card; // 等价于 #card();
.d(20px); //必须先引入 #card
}
/* 生成的 CSS */
#wrap{
height:100px;
}
#main{
width:300px;
}
#con{
width:20px;
}

方法的条件筛选

  1. 比较运算有: > >= = =< <。
  2. = 代表的是等于
  3. 除去关键字 true 以外的值都被视为 false:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* Less */
#card{

// and 运算符 ,相当于 与运算 &&,必须条件全部符合才会执行
.border(@width,@color,@style) when (@width>100px) and(@color=#999){
border:@style @color @width;
}

// not 运算符,相当于 非运算 !,条件为 不符合才会执行
.background(@color) when not (@color>=#222){
background:@color;
}

// , 逗号分隔符:相当于 或运算 ||,只要有一个符合条件就会执行
.font(@size:20px) when (@size>50px) , (@size<100px){
font-size: @size;
}
}
#main{
#card>.border(200px,#999,solid);
#card .background(#111);
#card > .font(40px);
}
/* 生成后的 CSS */
#main{
border:solid #999 200px;
background:#111;
font-size:40px;
}

数量不定的参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* Less */
.boxShadow(...){
box-shadow: @arguments;
}
.textShadow(@a,...){
text-shadow: @arguments;
}
#main{
.boxShadow(1px,4px,30px,red);
.textShadow(1px,4px,30px,red);
}

/* 生成后的 CSS */
#main{
box-shadow: 1px 4px 30px red;
text-shadow: 1px 4px 30px red;
}

方法使用的important!

1
2
3
4
5
6
7
8
9
10
11
12
13
/* Less */
.border{
border: solid 1px red;
margin: 50px;
}
#main{
.border() !important;
}
/* 生成后的 CSS */
#main {
border: solid 1px red !important;
margin: 50px !important;
}

循环方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/* Less */
.generate-columns(4);

.generate-columns(@n, @i: 1) when (@i =< @n) {
.column-@{i} {
width: (@i * 100% / @n);
}
.generate-columns(@n, (@i + 1));
}
/* 生成后的 CSS */
.column-1 {
width: 25%;
}
.column-2 {
width: 50%;
}
.column-3 {
width: 75%;
}
.column-4 {
width: 100%;
}

属性拼接方法

+_ 代表的是 空格;+ 代表的是 逗号。

1
2
3
4
5
6
7
8
9
10
11
12
/* Less */
.boxShadow() {
box-shadow+: inset 0 0 10px #555;
}
.main {
.boxShadow();
box-shadow+: 0 0 20px black;
}
/* 生成后的 CSS */
.main {
box-shadow: inset 0 0 10px #555, 0 0 20px black;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
/* Less */
.Animation() {
transform+_: scale(2);
}
.main {
.Animation();
transform+_: rotate(15deg);
}

/* 生成的 CSS */
.main {
transform: scale(2) rotate(15deg);
}

继承

extend 关键字使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* Less */
.animation{
transition: all .3s ease-out;
.hide{
transform:scale(0);
}
}
#main{
&:extend(.animation);
}
#con{
&:extend(.animation .hide);
}

/* 生成后的 CSS */
.animation,#main{
transition: all .3s ease-out;
}
.animation .hide , #con{
transform:scale(0);
}

all 全局搜索替换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* Less */
#main{
width: 200px;
}
#main {
&:after {
content:"Less is good!";
}
}
#wrap:extend(#main all) {}

/* 生成的 CSS */
#main,#wrap{
width: 200px;
}
#main:after, #wrap:after {
content: "Less is good!";
}

减少代码的重复性

  1. 选择器和扩展之间 是允许有空格的:pre:hover :extend(div pre).
  2. 可以有多个扩展: pre:hover:extend(div pre):extend(.bucket tr) - 注意这与 pre:hover:extend(div pre, .bucket tr)一样。
  3. 这是不可以的,扩展必须在最后 : pre:hover:extend(div pre).nth-child(odd)。
  4. 如果一个规则集包含多个选择器,所有选择器都可以使用extend关键字。

导入

  1. 导入 less 文件 可省略后缀
  2. @import 的位置可随意放置
  3. 使用@import (reference)导入外部文件,但不会添加 把导入的文件 编译到最终输出中,只引用。
  4. @import语句的默认行为。这表明相同的文件只会被导入一次,而随后的导入文件的重复代码都不会解析。
  5. 使用@import (multiple)允许导入多个同名文件。

函数

判断类型

  1. isnumber: 判断给定的值 是否 是一个数字。
  2. iscolor: 判断给定的值 是否 是一个颜色
  3. isurl:判断给定的值 是否 是一个 url 。

颜色操作

  1. saturate:增加一定数值的颜色饱和度。
  2. lighten:增加一定数值的颜色亮度。
  3. darken:降低一定数值的颜色亮度。
  4. fade:给颜色设定一定数值的透明度。
  5. mix:根据比例混合两种颜色。

数学函数

  1. ceil:向上取整。
  2. floor:向下取整。
  3. percentage: 将浮点数转换为百分比字符串。
  4. round:四舍五入
  5. sqrt:计算一个数的平方根。
  6. abs:计算数字的绝对值,原样保持单位
  7. pow:计算一个数的乘方。