@for

@for ルールは @for <variable> from <expression> to <expression> { ... } または @for <variable> from <expression> through <expression> { ... } と書き、ある数値(最初の expression の結果)から別数値(2 番目の結果)まで昇順または降順で数を数え、間の各数値についてブロックを評価します。実行のたびに各数値は与えられた変数名に割り当てられます。to が使用された場合、最後の数値は除外され、through が使用された場合、最後の数値は 含まれます。

遊び場

SCSS 構文

$base-color: #036;

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




遊び場

Sass 構文

$base-color: #036

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






CSS 出力

ul:nth-child(3n+1) {
  background-color: rgb(0, 63.75, 127.5);
}

ul:nth-child(3n+2) {
  background-color: rgb(0, 76.5, 153);
}

ul:nth-child(3n+3) {
  background-color: rgb(0, 89.25, 178.5);
}