使用第 n 个子值作为 SASS 变量

2022-01-10 00:00:00 css-selectors css sass

有没有办法将 nth-child 值用作 SASS 变量?

Is there any way to use the nth-child value as a SASS variable ?

使用示例:

div:nth-child(n) {
    content: '#{$n}'
}

div:nth-child(n) {
    background: rgb(#{$n}, #{$n}, #{$n});
}

推荐答案

我不认为有办法做到这一点.但是你可以使用 @for 指令来循环已知数量的元素:

I don't think there's a way to do exactly that. But you can use @for directive to loop through a known number of elements:

$elements: 15;
@for $i from 0 to $elements {
  div:nth-child(#{$i + 1}) {
     background: rgb($i, $i, $i);
  }
}

相关文章