The @each
rule makes it easy to emit styles or evaluate code for each element of a list or each pair in a map. It’s great for repetitive styles that only have a few variations between them. It’s usually written @each <variable> in <expression> { ... }
, where the expression returns a list. The block is evaluated for each element of the list in turn, which is assigned to the given variable name.
$sizes: 40px, 50px, 80px; @each $size in $sizes { .icon-#{$size} { font-size: $size; height: $size; width: $size; } }
$sizes: 40px, 50px, 80px @each $size in $sizes .icon-#{$size} font-size: $size height: $size width: $size
.icon-40px { font-size: 40px; height: 40px; width: 40px; } .icon-50px { font-size: 50px; height: 50px; width: 50px; } .icon-80px { font-size: 80px; height: 80px; width: 80px; }
You can also use @each
to iterate over every key/value pair in a map by writing it @each <variable>, <variable> in <expression> { ... }
. The key is assigned to the first variable name, and the element is assigned to the second.
$icons: ("eye": "\f112", "start": "\f12e", "stop": "\f12f"); @each $name, $glyph in $icons { .icon-#{$name}:before { display: inline-block; font-family: "Icon Font"; content: $glyph; } }
$icons: ("eye": "\f112", "start": "\f12e", "stop": "\f12f") @each $name, $glyph in $icons .icon-#{$name}:before display: inline-block font-family: "Icon Font" content: $glyph
@charset "UTF-8"; .icon-eye:before { display: inline-block; font-family: "Icon Font"; content: ""; } .icon-start:before { display: inline-block; font-family: "Icon Font"; content: ""; } .icon-stop:before { display: inline-block; font-family: "Icon Font"; content: ""; }
If you have a list of lists, you can use @each
to automatically assign variables to each of the values from the inner lists by writing it @each <variable...> in <expression> { ... }
. This is known as destructuring, since the variables match the structure of the inner lists. Each variable name is assigned to the value at the corresponding position in the list, or null
if the list doesn’t have enough values.
$icons: "eye" "\f112" 12px, "start" "\f12e" 16px, "stop" "\f12f" 10px; @each $name, $glyph, $size in $icons { .icon-#{$name}:before { display: inline-block; font-family: "Icon Font"; content: $glyph; font-size: $size; } }
$icons: "eye" "\f112" 12px, "start" "\f12e" 16px, "stop" "\f12f" 10px @each $name, $glyph, $size in $icons .icon-#{$name}:before display: inline-block font-family: "Icon Font" content: $glyph font-size: $size
@charset "UTF-8"; .icon-eye:before { display: inline-block; font-family: "Icon Font"; content: ""; font-size: 12px; } .icon-start:before { display: inline-block; font-family: "Icon Font"; content: ""; font-size: 16px; } .icon-stop:before { display: inline-block; font-family: "Icon Font"; content: ""; font-size: 10px; }
Because @each
supports destructuring and maps count as lists of lists, @each
‘s map support works without needing special support for maps in particular.
© 2006–2022 the Sass team, and numerous contributors
Licensed under the MIT License.
https://sass-lang.com/documentation/at-rules/control/each