NEWS LETTER

SASS嵌套

Scroll down

SASS中提供了三种嵌套方式:

  1. 选择器嵌套
  2. 属性嵌套
  3. 伪类嵌套

1、选择器嵌套

我们平时用的最多的就是选择器嵌套了,比如:

1
2
3
4
5
6
7
8
p {
span {
color: red;
div & {
color: blue;
}
}
}

对应的css样式代码:

1
2
3
4
5
6
p span {
color: red;
}
div p span {
color: blue;
}

2、属性嵌套

假如有下面一点css代码:

1
2
3
4
5
div {
font-size: 20px;
font-weight: bold;
font-style: italic;
}

我们可以使用属性嵌套来写成:

1
2
3
4
5
6
7
div {
font: {
size: 20px;
weight: bold;
style: italic;
}
}

3、伪类嵌套

伪类嵌套也是开发中经常用的一种嵌套方式,比如下面这段css代码:

1
2
3
4
5
6
7
8
9
10
11
12
div:before, 
div:after {
content: '';
width: 10px;
height: 10px;
}
div:before {
background: red;
}
div:after {
background: blue;
}

我们可以使用伪类嵌套来写成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
div {
&:before,
&:after {
content: '';
width: 10px;
height: 10px;
}
&:before {
background: red;
}
&:after {
background: blue;
}
}

平时开发中,我们可以合理的应用上述三种嵌套方式,来简化代码书写,提高开发效率。

其他文章
cover
SASS循环控制指令
  • 24/03/13
  • 11:19
  • SASS
cover
使用SASS简化媒体查询
  • 24/02/22
  • 17:23
  • SASS
请输入关键词进行搜索