Sass @for

本文最后更新于:2021年2月19日 晚上

Sass @for

The @for rule, written @for <variable> from <expression> to <expression> { ... } or @for <variable> from <expression> through <expression> { ... }, counts up or down from one number (the result of the first expression) to another (the result of the second) and evaluates a block for each number in between. Each number along the way is assigned to the given variable name. If to is used, the final number is excluded; if through is used, it’s included.

SCSS SYNTAX

1
2
3
4
5
6
7
$base-color: #036;

@for $i from 1 through 3 {
ul:nth-child(3n + #{$i}) {
background-color: lighten($base-color, $i * 5%);
}
}

CSS OUTPUT

1
2
3
4
5
6
7
8
9
10
11
ul:nth-child(3n + 1) {
background-color: #004080;
}

ul:nth-child(3n + 2) {
background-color: #004d99;
}

ul:nth-child(3n + 3) {
background-color: #0059b3;
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!