Sass @each Rule

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
Lists & maps

What You’ll Learn

Sass @each emits styles or runs code for every item in a list or every pair in a map. This page covers list loops, map loops, destructuring, practical token generation, and five compiled examples.

01

Loop

@each

02

Lists

in $list

03

Maps

key, value

04

Destructure

Inner lists

05

Generate

Classes & MQ

06

Practice

5 examples

What Is @each?

Official docs: 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 is great for repetitive styles that only have a few variations between them.

It is usually written @each <variable> in <expression> { … }, where the expression returns a list. The block runs once per element, and that element is assigned to the variable name.

  • Compile-time only—browsers never see @each.
  • Ideal when you already have the values (sizes, names, tokens).
  • Works with lists, maps, and nested lists (destructuring).
  • Use interpolation (#{$name}) to build selectors and property values.
💡
Beginner tip

Think of @each as “for each item in this collection, write these styles.” Keep the collection in one place so adding a size or tone is a one-line change.

📝 Syntax

styles.scss
$sizes: 40px, 50px, 80px;

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

🗺️ With Maps

Official docs: iterate every key/value pair with @each $key, $value in $map { … }. The key is assigned to the first variable and the value to the second.

styles.scss
$icons: ("eye": "\f112", "start": "\f12e", "stop": "\f12f");

@each $name, $glyph in $icons {
  .icon-#{$name}:before {
    content: $glyph;
  }
}

🧩 Destructuring

Official docs: if you have a list of lists, write @each $a, $b, $c in $rows so each inner list’s values are assigned to those variables. Missing positions become null.

💡
Fun fact

Maps count as lists of lists, so map support works through the same destructuring mechanism—no special map-only syntax beyond naming two variables.

⚖️ @each vs @for

NeedPrefer
Walk known values / tokens@each
Count 1…n or a numeric range@for (separate control rule)
Key/value design tokens@each $k, $v in $map

⚡ Quick Reference

GoalCode
Loop a list@each $item in $list { … }
Loop a map@each $key, $val in $map { … }
Destructure rows@each $a, $b, $c in $rows { … }
Build a class.icon-#{$name} { … }
Emit media queries@each $n, $w in $bp { @media … }

Examples Gallery

Each example generates multiple CSS rules from one loop. Open View Compiled CSS for verified output.

📚 Getting Started

Loop lists and maps to avoid copy-paste CSS.

Example 1 — Loop a List of Sizes

Official-style icon size utilities from a simple list.

styles.scss
$sizes: 40px, 50px, 80px;

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

How It Works

Each length in $sizes becomes $size once. Interpolation builds class names like .icon-40px.

Example 2 — Loop a Map of Icons

Keys become class names; values become glyph content.

styles.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

Adding a new icon is one map entry—the loop writes the matching ::before rule automatically.

📈 Destructuring, Breakpoints & Tokens

Richer rows and design-system generation.

Example 3 — Destructure a List of Lists

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

styles.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

Each comma-separated row is an inner list. Variables line up by position: name, glyph, then size.

Example 4 — Breakpoint Map → Media Queries

Generate container max-widths from a named breakpoint map.

styles.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

One map drives every query. Adding xl: 1280px automatically creates another media block when you recompile.

Example 5 — Theme Tokens + State List

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

styles.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

The map loop writes two classes per tone. The state list builds :hover, :focus, and :active selectors via interpolation.

🚀 Real-World Use Cases

  • Utility classes — spacing, icon sizes, color badges.
  • Icon fonts — name → glyph maps.
  • Breakpoints — generate media queries from one map.
  • Theme scales — tones, z-index layers, font steps.
  • State selectors — hover / focus / active lists.

🧠 How Compilation Works

1

Read the collection

Evaluate the list or map after in.

Source
2

Assign variables

Set item (or key/value / destructured fields) for this pass.

Bind
3

Evaluate the block

Emit CSS for this iteration, then move to the next item.

Repeat
4

CSS ships

All generated rules appear; @each itself does not.

⚠️ Common Pitfalls

  • Invalid selectors from values.icon-40px is fine; odd units or spaces may need careful interpolation.
  • Huge CSS output — looping big maps can explode file size.
  • Wrong variable count — destructuring with too many names fills extras with null.
  • Mutating during the loop — keep the source list/map stable and simple.
  • Using @each for numeric ranges — prefer @for when you only need to count.

💡 Best Practices

✅ Do

  • Keep token lists/maps as the single source of truth
  • Name loop variables clearly ($name, $width)
  • Prefer maps for key/value design tokens
  • Use destructuring for multi-field rows
  • Generate only the utilities your project actually uses

❌ Don’t

  • Hand-copy nearly identical rules when a loop would do
  • Loop unbounded or experimental token dumps into production CSS
  • Forget interpolation when building selectors
  • Assume empty lists are falsey inside conditions elsewhere
  • Nest enormous loops without checking output size

Key Takeaways

Knowledge Unlocked

Five things to remember about Sass @each

Walk lists and maps at compile time to generate repetitive CSS cleanly.

5
Core concepts
📋 02

Lists

$item in $list

Values
🗺️ 03

Maps

key, value

Tokens
🧩 04

Destructure

row fields

Rows
05

Compile

many CSS rules

Output

❓ 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.
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.
Did you know?

Official Sass docs note that map iteration works because maps are lists of key/value lists—so destructuring and map looping share the same underlying idea.

Conclusion

@each is the go-to control rule when you already have a list or map of values to turn into CSS. Loop lists for simple series, maps for tokens, and destructure rows when each item carries several fields.

Continue with @for or the Sass introduction.

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.

5 people found this page helpful