Sass @each
@each repeats a block of styles once for every item in a list or every key/value pair in a map. It's the go-to tool for generating families of similar classes — colour utilities, icon classes, button variants — from a single source of truth.
@each $item in <list> { … }
@each $key, $value in <map> { … }Looping over a list
$sizes: small, medium, large;
@each $size in $sizes {
.icon-#{$size} {
background-image: url("/icons/#{$size}.svg");
}
}.icon-small {
background-image: url("/icons/small.svg");
}
.icon-medium {
background-image: url("/icons/medium.svg");
}
.icon-large {
background-image: url("/icons/large.svg");
}
Each pass sets $size to the next item. Interpolation (#{$size}) puts it into the selector and the URL.
Looping over a map
Maps pair names with values, so they're perfect for theme colours. Destructure each pair into two variables:
$status-colors: (
success: #2e7d32,
warning: #ed6c02,
danger: #d32f2f,
info: #0288d1,
);
@each $name, $color in $status-colors {
.text-#{$name} {
color: $color;
}
.bg-#{$name} {
background-color: $color;
color: white;
}
}.text-success {
color: #2e7d32;
}
.bg-success {
background-color: #2e7d32;
color: white;
}
.text-warning {
color: #ed6c02;
}
.bg-warning {
background-color: #ed6c02;
color: white;
}
.text-danger {
color: #d32f2f;
}
.bg-danger {
background-color: #d32f2f;
color: white;
}
.text-info {
color: #0288d1;
}
.bg-info {
background-color: #0288d1;
color: white;
}
Add one entry to $status-colors and both classes for it appear automatically.
Destructuring a list of lists
If each item is itself a list, you can unpack it into several variables at once:
$headings: (h1 2.5rem 700) (h2 2rem 700) (h3 1.5rem 600);
@each $tag, $size, $weight in $headings {
#{$tag} {
font-size: $size;
font-weight: $weight;
}
}h1 {
font-size: 2.5rem;
font-weight: 700;
}
h2 {
font-size: 2rem;
font-weight: 700;
}
h3 {
font-size: 1.5rem;
font-weight: 600;
}
Using the index
@each doesn't give you an index directly. When you need one, combine @for with list.nth(), or use list.index():
@use "sass:list";
$steps: plan, build, ship;
@each $step in $steps {
$i: list.index($steps, $step);
.step-#{$step}::before {
content: "#{$i}";
}
}.step-plan::before {
content: "1";
}
.step-build::before {
content: "2";
}
.step-ship::before {
content: "3";
}
@each inside a rule
Loops can live inside a selector, too, producing nested rules or declarations:
.social {
@each $network, $color in (github: #24292f, mastodon: #6364ff) {
&--#{$network} { background: $color; }
}
}
// .social--github { … } .social--mastodon { … }Tip: Keep generated utility sets small. A loop over 10 colours × 10 shades × 5 properties produces 500 classes — easy to write, heavy to ship.
Test yourself
Which syntax loops over a map's keys and values?
- @each $key in $map
- @each $key, $value in $map
- @for $key, $value of $map
- @each ($key: $value) in $map
How many rules does @each $c in red, green, blue { .t-#{$c} { color: $c; } } produce?
- 1
- 2
- 3
- 0
Why is interpolation needed in .icon-#{$size}?
- It isn't needed
- Variables aren't evaluated inside selectors without #{}
- To quote the value
- To convert it to a number