40
46

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 5 years have passed since last update.

AtraeAdvent Calendar 2015

Day 15

Sassで連想配列が使えるようになってたので使ってみた。

Last updated at Posted at 2015-12-15

Sassで連想配列が使えずに泣いたあの日はもういない。
無駄に配列2つ用意したあの日よグッバイ。

ということで、早速サンプルいってみよう。

style.scss
$member_color:(
  madoka:pink,
  homura:black,
  sayaka:blue,
  kyoko:red,
  kyube:white
);

@each $name, $color in $member_color {
  .color_#{$name} {
     color: $color;
  }
}

コンパイルするとこんな感じで書きだされます。

style.css
.color_madoka {
  color: pink;
}

.color_homura {
  color: black;
}

.color_sayaka {
  color: blue;
}

.color_kyoko {
  color: red;
}

.color_kyube {
  color: white;
}

簡単に解説すると、

style.scss
$member_color:(//変数の中身をを括弧で囲めばおk
  madoka:pink,
  homura:black,
  sayaka:blue,
  kyoko:red,
  kyube:white
);

@each $name, $color in $member_color {//$nameと$colorはkeyとvalueが順番に入る模様。
  .color_#{$name} {
     color: $color;
  }
}

という感じです。

40
46
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
40
46

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?