0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【SCSS】@eachで、キーを使った配列

Posted at

@eachとは?

配列の数の分だけ、繰り返し処理を行います。

どんな時に使うか?

sample.scss
.block {
  .element1 {
    border: solid 1px black;
    color: black;
  }

  .element2 {
    border: solid 1px red;
    color: red;
  }

  .element3 {
    border: solid 1px green;
    color: green;
  }
}

今回の場合は、要素ごとに色が異なります。
これをまとめてループ処理にします。

【書き方】

@each $変数名 in $リスト名{
  処理
}
sample.scss
$colors: (1:black, 2:red, 3:green);

@each $key, $color in $colors {
  .block {
    .element#{key} {
      border: solid 1px $color;
      color: $color;
    }
  }
}

参考

3-2. ループ処理(each・for・while)で繰り返し記述する手間を省く

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?