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
Concept
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.
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.
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.
Compare
⚖️ @each vs @for
Need
Prefer
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
Cheat Sheet
⚡ Quick Reference
Goal
Code
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 … }
Hands-On
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.
The map loop writes two classes per tone. The state list builds :hover, :focus, and :active selectors via interpolation.
Applications
🚀 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.
Watch Out
⚠️ 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.
Pro Tips
💡 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
Summary
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
📝01
@each
per item
Loop
📋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.
@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.