scss遍历数组 发表于 2021-12-30 | 分类于 css , scss 使用scss遍历数组 each遍历12345678910111213$colors: ( #00D477, #F57933, #0052F5 ); @each $c in $colors { $i: index($colors, $c); .tag-#{$i} { background-color: $c; } } 生成的结果如下: 123456789101112.tag-1 { background-color: #00D477;}.tag-2 { background-color: #F57933;}.tag-3 { background-color: #0052F5;} for循环1234567891011$colors: ( #00D477, #F57933, #0052F5);@for $i from 1 to 4 { .tag-#{$i} { background-color: nth($colors, $i) }} 生成结果与上方一样,要注意的是to循环不到4另外to换成through,但是后者可以走到4