【发布时间】:2013-12-05 21:20:43
【问题描述】:
有没有办法将nth-child 值用作 SASS 变量?
使用示例:
div:nth-child(n) {
content: '#{$n}'
}
div:nth-child(n) {
background: rgb(#{$n}, #{$n}, #{$n});
}
【问题讨论】:
标签: css sass css-selectors
有没有办法将nth-child 值用作 SASS 变量?
使用示例:
div:nth-child(n) {
content: '#{$n}'
}
div:nth-child(n) {
background: rgb(#{$n}, #{$n}, #{$n});
}
【问题讨论】:
标签: css sass css-selectors
我认为没有办法做到这一点。但是你可以使用@for 指令来遍历已知数量的元素:
$elements: 15;
@for $i from 0 to $elements {
div:nth-child(#{$i + 1}) {
background: rgb($i, $i, $i);
}
}
【讨论】:
$elements?
你可以像这样使用 mixin:
@mixin child($n) {
&:nth-child(#{$n}){
background-color:rgb($n,$n,$n);
}
}
div{
@include child(2);
}
编译后的css如下:
div:nth-child(2) {
background-color: #020202;
}
查看示例here
【讨论】: