TExcelTAD
Home › Sass › Sass @each

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.

scss
@each $item in <list> { … }
@each $key, $value in <map> { … }

Looping over a list

scss · try it yourself
$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:

scss · try it yourself
$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:

scss · try it yourself
$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():

scss · try it yourself
@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:

scss
.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?

  1. @each $key in $map
  2. @each $key, $value in $map
  3. @for $key, $value of $map
  4. @each ($key: $value) in $map

How many rules does @each $c in red, green, blue { .t-#{$c} { color: $c; } } produce?

  1. 1
  2. 2
  3. 3
  4. 0

Why is interpolation needed in .icon-#{$size}?

  1. It isn't needed
  2. Variables aren't evaluated inside selectors without #{}
  3. To quote the value
  4. To convert it to a number