Sass @for counts from one number to another and runs a block for each step. This page covers through vs to, counting up or down, utility scales, grids, and five compiled examples.
01
Loop
@for
02
Through
End included
03
To
End excluded
04
Direction
Up or down
05
Generate
Scales & grids
06
Practice
5 examples
Concept
What Is @for?
Official docs: the @for rule counts up or down from one number to another and evaluates a block for each number in between. Each number along the way is assigned to the variable you name.
Write it as @for <variable> from <start> through <end> { … } or @for <variable> from <start> to <end> { … }.
Compile-time only—browsers never see @for.
Ideal when you need a numeric series (1…n columns, spacing steps, z-index layers).
through includes the end; to excludes it.
Use interpolation (#{$i}) to build selectors and values.
💡
Beginner tip
Think of @for as “count from A to B and write these styles each time.” Prefer @each when you already have named tokens in a list or map.
Foundation
📝 Syntax
styles.scss
@for $i from 1 through 3 {
.step-#{$i} {
order: $i;
}
}
Inclusive vs Exclusive
⚖️ through vs to
Official docs: if to is used, the final number is excluded; if through is used, it is included.
Form
Range for 1 … 3
from 1 through 3
1, 2, 3 (end included)
from 1 to 3
1, 2 (end excluded)
💡
Fun fact
from 0 to $count is a handy zero-based pattern: you get 0 … $count - 1—perfect for stacking $count items.
Direction
🔄 Counting Up or Down
If the start is smaller than the end, Sass counts upward. If the start is larger, it counts downward. The same through / to rules still apply.
styles.scss
@for $i from 5 through 1 {
.layer-#{$i} {
z-index: $i;
}
}
Mixin parameters — generate N items from a $count argument.
🧠 How Compilation Works
1
Resolve the range
Evaluate start and end numbers; pick up or down.
Bounds
2
Assign the counter
Set your variable (for example $i) to the current number.
Bind
3
Evaluate the block
Emit CSS for this step, then move to the next number.
Repeat
4
✓
CSS ships
All generated rules appear; @for itself does not.
Watch Out
⚠️ Common Pitfalls
Confusing to with through — off-by-one bugs are the classic mistake.
Huge CSS output — @for $i from 1 through 1000 can explode file size.
Using @for for named tokens — prefer @each for lists and maps.
Forgetting interpolation — selectors need #{$i}, not bare $i.
Non-integer ranges — keep ends as whole numbers unless you intentionally need fractional steps.
Pro Tips
💡 Best Practices
✅ Do
Choose through or to deliberately for inclusive vs exclusive ends
Name the counter clearly ($i, $col, $step)
Drive ranges from variables ($columns, $steps)
Combine with mixins when the recipe is reusable
Generate only the steps your design system needs
❌ Don’t
Hand-copy nearly identical numbered rules when a loop would do
Loop unbounded ranges into production CSS
Use @for when you already have a token list—use @each
Forget that to excludes the end value
Nest enormous loops without checking output size
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about Sass @for
Count numeric ranges at compile time to generate repetitive CSS cleanly.
5
Core concepts
📝01
@for
per number
Loop
✅02
through
end included
Inclusive
🚫03
to
end excluded
Exclusive
🔄04
Direction
up or down
Order
✓05
Compile
many CSS rules
Output
❓ Frequently Asked Questions
It counts from one number to another and evaluates a block once per number, assigning that number to a variable you name.
through includes the end number. to excludes it. So from 1 through 3 runs 1, 2, 3; from 1 to 3 runs 1, 2.
Yes. If the start is greater than the end (for example from 5 through 1), Sass counts down.
No. @for is compile-time only. Browsers see the styles generated for each iteration.
@for walks a numeric range. @each walks values in a list or map. Use @for when you need to count; use @each when you already have the items.
Yes. Both ends can be any Sass expressions that resolve to numbers (including variables and math).
Did you know?
Official Sass docs show @for with :nth-child selectors and color steps—a classic pattern for striped or staggered list styling without hand-writing each rule.
@for is the go-to control rule when you need to count through a numeric range. Use through to include the end, to to exclude it, and count up or down as needed for scales, grids, and stacked layouts.