Sass @each Rule

Beginner
10 min read
Updated: Nov 2026
5 examples
Compiled CSS

Definition and Usage

The Sass @each rule runs a block once for every item in a list or every pair in a map. It is ideal for repetitive styles that differ by a small set of values (sizes, names, tokens).

Write @each $item in $list { … }. Each element is assigned to $item in turn. Use interpolation (#{$item}) to build selectors and property values. The loop is compile-time only — browsers never see @each.

Official pattern.

Per the Sass docs: emit styles for each list element or each map key/value pair when variations are few and known up front.

@each vs @for.

Prefer @each when you already have the values. Prefer @for when you need to count a numeric range.

Syntax

SCSS
$sizes: 40px, 50px, 80px;

@each $size in $sizes {
  .icon-#{$size} {
    font-size: $size;
    height: $size;
    width: $size;
  }
}

Pattern: collection → @each $var in … → emit CSS per item.

Loop Forms

Three common ways to write @each:

FormMeaning
@each $item in $listOne variable per list element
@each $key, $value in $mapKey then value for every map pair
@each $a, $b, $c in $rowsDestructure each inner list (missing slots → null)
Fun fact.

Maps count as lists of key/value lists, so map looping works through the same destructuring idea — no special map-only keyword beyond naming two variables.

SCSS Examples

Short, copy-ready snippets. Open View Compiled CSS for verified output.

Example 1 — Loop a List of Sizes

Generate matching icon utilities from one size list.

SCSS
$sizes: 40px, 50px, 80px;

@each $size in $sizes {
  .icon-#{$size} {
    font-size: $size;
    height: $size;
    width: $size;
  }
}

How It Works

  1. Store sizes in a comma-separated list.
  2. Loop with @each $size in $sizes.
  3. Interpolate #{$size} into the class name.
  4. Reuse $size for font-size, height, and width.

Example 2 — Loop a Map of Icons

Keys become class names; values become glyph content.

SCSS
$icons: ("eye": "\f112", "start": "\f12e", "stop": "\f12f");

@each $name, $glyph in $icons {
  .icon-#{$name}:before {
    display: inline-block;
    font-family: "Icon Font";
    content: $glyph;
  }
}

How It Works

  1. Define a map of name → glyph.
  2. Destructure with @each $name, $glyph in $icons.
  3. Build .icon-#{$name}:before from the key.
  4. Set content from the value.

Example 3 — Destructure a List of Lists

Pull name, glyph, and size from each inner row in one step.

SCSS
$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;
  }
}

How It Works

  1. Each comma-separated row is an inner list.
  2. Name variables in order: name, glyph, size.
  3. Missing positions become null if a row is short.
  4. Emit one rule per row with all three fields.

Example 4 — Breakpoint Map → Media Queries

Generate container max-widths from named breakpoints.

SCSS
$breakpoints: (
  sm: 480px,
  md: 768px,
  lg: 1024px,
);

@each $name, $width in $breakpoints {
  @media (min-width: $width) {
    .container-#{$name} {
      max-width: $width;
    }
  }
}

How It Works

  1. Keep breakpoints in one map.
  2. Loop key and width with @each.
  3. Wrap styles in @media (min-width: $width).
  4. Add a new entry to grow the set without copy-paste.

Example 5 — Theme Tokens + State List

Emit color utilities from a map and outline styles from a state list.

SCSS
$tones: (
  success: #2f9e64,
  warning: #e6a700,
  danger: #d64550,
);

@each $name, $color in $tones {
  .badge-#{$name} {
    background: $color;
    color: #fff;
  }

  .text-#{$name} {
    color: $color;
  }
}

$states: hover, focus, active;

@each $state in $states {
  .btn:#{$state} {
    outline: 2px solid currentColor;
  }
}

How It Works

  1. Map loop writes two classes per tone.
  2. State list holds pseudo-class names.
  3. Interpolate .btn:#{$state} into :hover, :focus, :active.
  4. One source of truth keeps theme and states in sync.

Edge Cases & Pitfalls

Most @each bugs are interpolation, output size, or choosing the wrong loop.

selectors

Build selectors with interpolation

Use #{$name} in class names. Plain $name inside a selector will not expand the way you expect.

output

Large maps grow CSS fast

Every iteration emits real CSS. Keep token sets intentional for production builds.

null

Short rows become null

Destructuring with more variables than values fills the extras with null. Guard with @if when needed.

range

Counting? Use @for

If you only need 1…n, prefer @for. Reserve @each for known lists and maps.

Sass Support

@each is a core Dart Sass control rule. It is not a browser CSS feature — support means your Sass compiler understands it.

ImplementationStatus
Dart SassSupported (recommended)
LibSass / node-sassSupported (deprecated toolchain)
Ruby SassSupported (end-of-life)
BrowsersN/A — only see compiled CSS

Prefer Dart Sass for new projects. SCSS and indented syntax both support @each.

Key Takeaways

  • Lists: @each $item in $list.
  • Maps: @each $key, $value in $map.
  • Rows: destructure with multiple variables.
  • Output: interpolate selectors; ship only the CSS you need.

One line: walk a list or map once, emit CSS per item at compile time.

Compile Notes

@each never reaches the browser. Conceptual flow:

flow.txt
1. Evaluate the list or map after `in`
2. Bind $item (or $key/$value / destructured fields)
3. Evaluate the block → emit CSS for this pass
4. Repeat until the collection ends
5. Final stylesheet contains only the generated rules

Frequently Asked Questions

It evaluates a block once for each element of a list, or each key/value pair in a map, assigning values to variables you name. The loop runs at compile time.
Write @each $size in $sizes { … }. Each list item is assigned to $size in turn.
Write @each $name, $value in $map { … }. The key goes to the first variable and the value to the second.
If you have a list of lists, @each $a, $b, $c in $rows assigns each inner list’s values to those variables (or null if a value is missing).
No. @each is compile-time only. Browsers see the styles generated for each iteration.
@each walks values in a list or map. @for counts through a numeric range (from/through/to). Use @each when you already have the items to style.

Next: Sass @for Rule

Count numeric ranges with from/through and from/to.

Sass @for Rule →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful