@for

@for 规则,写作 @for <variable> from <expression> to <expression> { ... }@for <variable> from <expression> through <expression> { ... },从一个数字(第一个 表达 的结果)到另一个数字(第二个 表达 的结果)进行向上或向下计数,并评估中间每个数字的块。沿途的每个数字都分配给给定的变量名称。如果使用 to,则排除最终数字;如果使用 through,则将其包括在内。

SCSS Syntax

$base-color: #036;

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




Sass Syntax

$base-color: #036

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






CSS Output

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

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

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