0
0
SASSmarkup~15 mins

@for loop (through vs to) in SASS - Trade-offs & Expert Analysis

Choose your learning style9 modes available
Overview - @for loop (through vs to)
What is it?
The @for loop in Sass is a way to repeat styles multiple times by counting numbers. It helps you write less code by automating repetitive CSS rules. There are two ways to use it: 'to' and 'through', which control how many times the loop runs. Understanding the difference helps you control your styles precisely.
Why it matters
Without the @for loop, you would have to write many similar CSS rules by hand, which is slow and error-prone. The difference between 'to' and 'through' lets you decide if the loop stops before or includes the last number, preventing off-by-one mistakes. This saves time and keeps your styles clean and consistent.
Where it fits
Before learning @for loops, you should know basic Sass syntax and variables. After mastering @for loops, you can explore other Sass loops like @each and @while, and learn how to combine loops with mixins for powerful style automation.
Mental Model
Core Idea
The @for loop repeats styles by counting numbers, and 'to' stops before the last number while 'through' includes it.
Think of it like...
Imagine you are counting steps while climbing stairs: 'to' means you stop just before the last step, 'through' means you step on every stair including the last one.
Loop range visualization:

Start  ──▶ 1 ──▶ 2 ──▶ 3 ──▶ ... ──▶ N

@for $i from 1 through N: includes N
@for $i from 1 to N: stops before N (up to N-1)
Build-Up - 6 Steps
1
FoundationBasic @for loop syntax
🤔
Concept: Learn how to write a simple @for loop in Sass to repeat styles.
Use @for $i from 1 through 3 { .item-#{$i} { width: 10px * $i; } } This creates three classes with widths 10px, 20px, and 30px.
Result
CSS output: .item-1 { width: 10px; } .item-2 { width: 20px; } .item-3 { width: 30px; }
Understanding the basic syntax is key to automating repetitive CSS and saves you from writing similar code multiple times.
2
FoundationCounting with 'through' keyword
🤔
Concept: 'through' includes the last number in the loop count.
When you write @for $i from 1 through 5, the loop runs for $i = 1, 2, 3, 4, and 5. Example: @for $i from 1 through 5 { .box-#{$i} { height: 5px * $i; } }
Result
CSS output: .box-1 { height: 5px; } .box-2 { height: 10px; } .box-3 { height: 15px; } .box-4 { height: 20px; } .box-5 { height: 25px; }
Knowing 'through' includes the last number helps you create loops that cover the full intended range without missing the last item.
3
IntermediateCounting with 'to' keyword
🤔
Concept: 'to' stops the loop before the last number, excluding it.
When you write @for $i from 1 to 5, the loop runs for $i = 1, 2, 3, 4 but NOT 5. Example: @for $i from 1 to 5 { .circle-#{$i} { border-radius: 2px * $i; } }
Result
CSS output: .circle-1 { border-radius: 2px; } .circle-2 { border-radius: 4px; } .circle-3 { border-radius: 6px; } .circle-4 { border-radius: 8px; }
Understanding 'to' helps avoid off-by-one errors when you want to exclude the last number from your loop.
4
IntermediateChoosing between 'to' and 'through'
🤔Before reading on: Do you think 'to' includes the last number or stops before it? Commit to your answer.
Concept: Learn when to use 'to' or 'through' based on whether you want to include the last number in your loop.
Use 'through' when you want to include the last number, like counting all items. Use 'to' when you want to stop before the last number, useful for zero-based or exclusive ranges. Example: @for $i from 1 through 3 { /* runs 1,2,3 */ } @for $i from 1 to 3 { /* runs 1,2 only */ }
Result
Loops behave differently depending on keyword, affecting how many CSS rules are generated.
Knowing the difference prevents common mistakes where styles are missing or extra, improving control over your CSS output.
5
AdvancedUsing @for loops with dynamic selectors
🤔Before reading on: Can you predict how interpolation works inside @for loops for class names? Commit to your answer.
Concept: Combine @for loops with string interpolation to create dynamic class names or IDs.
Inside the loop, use #{$i} to insert the current number into selectors or property values. Example: @for $i from 1 through 4 { .item-#{$i} { margin-left: 5px * $i; } }
Result
CSS output: .item-1 { margin-left: 5px; } .item-2 { margin-left: 10px; } .item-3 { margin-left: 15px; } .item-4 { margin-left: 20px; }
Understanding interpolation inside loops unlocks powerful ways to generate many related styles automatically.
6
ExpertPerformance and pitfalls of @for loops
🤔Before reading on: Do you think very large @for loops slow down Sass compilation significantly? Commit to your answer.
Concept: Explore how very large loops affect Sass performance and how to avoid common mistakes like infinite loops or off-by-one errors.
Large loops generate many CSS rules, which can slow down compilation and increase CSS size. Avoid loops with incorrect ranges that cause infinite loops or no output. Example of a common mistake: @for $i from 1 to 1 { /* no output because 'to' excludes 1 */ } Use 'through' if you want to include 1. Also, avoid nested loops with large ranges to keep performance manageable.
Result
Efficient loops produce clean CSS quickly; inefficient loops cause slow builds or bloated CSS.
Knowing performance impacts helps you write loops that balance automation with maintainability and speed.
Under the Hood
Sass processes @for loops during compilation by counting from the start number to the end number, generating CSS rules for each iteration. The 'to' keyword sets the loop to stop before the end number, while 'through' includes it. This counting happens in the Sass compiler, not in the browser, so the output CSS is static and ready to use.
Why designed this way?
The 'to' and 'through' keywords were designed to give developers precise control over loop ranges, reflecting common programming patterns where ranges can be inclusive or exclusive. This avoids confusion and off-by-one errors common in loops. Sass chose to do this at compile time to keep CSS fast and simple.
Sass @for loop flow:

┌───────────────┐
│ Start number  │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Loop counter  │
│ increments   │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Check condition│
│ (to or through)│
└──────┬────────┘
       │
   Yes │ No
       ▼    
┌───────────────┐
│ Generate CSS  │
│ for $i value  │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does '@for $i from 1 to 5' include 5 in the loop? Commit to yes or no.
Common Belief:Many think '@for $i from 1 to 5' runs for $i = 1 to 5, including 5.
Tap to reveal reality
Reality:'to' excludes the last number, so it runs only for $i = 1 to 4.
Why it matters:This causes missing styles for the last item, leading to bugs where expected CSS rules are not generated.
Quick: Can '@for $i from 1 through 1' run zero times? Commit to yes or no.
Common Belief:Some believe '@for $i from 1 through 1' runs zero times because start equals end.
Tap to reveal reality
Reality:It runs exactly once, for $i = 1.
Why it matters:Misunderstanding this leads to skipping necessary styles or writing extra code to handle single-item cases.
Quick: Does using '@for' loops slow down the browser? Commit to yes or no.
Common Belief:People often think @for loops slow down the browser because they generate many styles.
Tap to reveal reality
Reality:@for loops run at compile time in Sass, so the browser only sees the final CSS, not the loop.
Why it matters:This misconception can cause unnecessary avoidance of loops, missing out on their productivity benefits.
Quick: Can you use negative numbers with '@for' loops? Commit to yes or no.
Common Belief:Many think '@for' loops only work with positive numbers.
Tap to reveal reality
Reality:Sass @for loops require the start number to be less than or equal to the end number and both must be positive integers; negative ranges are not supported.
Why it matters:Trying to use negative numbers causes errors or no output, confusing beginners.
Expert Zone
1
The difference between 'to' and 'through' mirrors common programming range conventions, but Sass chose explicit keywords to avoid ambiguity.
2
Interpolation inside loops can be combined with nested loops to generate complex, multi-dimensional CSS grids or patterns efficiently.
3
Large loops can bloat CSS size; experts often combine loops with conditionals or mixins to generate only necessary styles.
When NOT to use
Avoid @for loops when you need to iterate over non-numeric lists or maps; use @each instead. Also, for very large or dynamic data sets, consider generating CSS with JavaScript or server-side tools to keep build times manageable.
Production Patterns
In production, @for loops are commonly used for generating grid systems, spacing scales, or theme variations. Experts combine them with variables and mixins to create scalable, maintainable design systems.
Connections
Programming for loops
Same pattern of counting and repeating actions based on numbers.
Understanding how loops work in programming languages helps grasp Sass @for loops since they share the concept of counting iterations.
CSS Grid and Flexbox
Builds-on by automating repetitive CSS rules for layout items.
Knowing @for loops helps generate consistent grid or flex item styles quickly, improving layout control.
Manufacturing assembly lines
Similar pattern of repeating a process step-by-step for each item.
Seeing loops as assembly lines clarifies how each iteration produces a new style, just like each station adds a part to a product.
Common Pitfalls
#1Off-by-one error using 'to' when 'through' is needed
Wrong approach:@for $i from 1 to 5 { .box-#{$i} { width: 10px * $i; } }
Correct approach:@for $i from 1 through 5 { .box-#{$i} { width: 10px * $i; } }
Root cause:Confusing 'to' as inclusive causes the last iteration to be skipped, missing styles.
#2Using negative or zero ranges in @for loops
Wrong approach:@for $i from -3 through 3 { .neg-#{$i} { margin: 5px * $i; } }
Correct approach:// Sass does not support negative ranges; use positive ranges or @each for lists.
Root cause:Misunderstanding that @for loops only accept positive integers and increasing ranges.
#3Expecting @for loops to run in the browser
Wrong approach:Trying to use @for loops inside CSS files without Sass preprocessing.
Correct approach:Write @for loops in Sass files and compile them to CSS before using in the browser.
Root cause:Not knowing that @for loops are a Sass feature processed at build time, not a CSS feature.
Key Takeaways
The @for loop in Sass automates repeating CSS rules by counting numbers.
'through' includes the last number in the loop, while 'to' stops before it.
Using the correct keyword prevents common off-by-one errors in your styles.
Interpolation inside loops lets you create dynamic selectors and property values.
@for loops run at compile time, so they do not affect browser performance.