插值语法
约 395 字大约 1 分钟
2025-04-17
注意
- 插值语法只能作用于 CSS 属性和选择器,无法作用于属性值。
- 使用变量和函数作为选择器或者属性值的时候,必须使用插值语法,否则按照字面量含义直接输出。
- 在支持读取变量的场景,不需要额外使用插值语法,可以使用表达式替代。
Stylus 支持使用 {}
字符包围表达式进行插值,该表达式会成为标识符的一部分。例如,-webkit-{'border' + '-radius'}
会被计算为 -webkit-border-radius
。
一、属性插值
一个典型的应用场景是为 CSS 属性添加浏览器厂商前缀。
vendor(prop, args)
-webkit-{prop} args
-moz-{prop} args
{prop} args
border-radius()
vendor('border-radius', arguments)
box-shadow()
vendor('box-shadow', arguments)
button
border-radius 1px 2px / 3px 4px
编译为:
button {
-webkit-border-radius: 1px 2px / 3px 4px;
-moz-border-radius: 1px 2px / 3px 4px;
border-radius: 1px 2px / 3px 4px;
}
二、选择器插值
插值同样适用于选择器。例如,我们可以通过迭代为表格的前 5 行设置高度属性,如下所示:
table
for row in 1 2 3 4 5
tr:nth-child({row})
height: 10px * row
编译为:
table tr:nth-child(1) {
height: 10px;
}
table tr:nth-child(2) {
height: 20px;
}
table tr:nth-child(3) {
height: 30px;
}
table tr:nth-child(4) {
height: 40px;
}
table tr:nth-child(5) {
height: 50px;
}
你也可以通过拼接字符串将多个选择器合并到一个变量中,并在适当位置进行插值:
mySelectors = '#foo,#bar,.baz'
{mySelectors}
background: #000
编译为:
#foo,
#bar,
.baz {
background: #000;
}
更新日志
2025/8/24 08:17
查看所有更新日志
e7112
-1于