Discover how a tiny change in your loop can save hours of repetitive work!
@for loop (through vs to) in SASS - When to Use Which
Imagine you want to create 10 colored boxes on a webpage. You write the CSS for each box one by one, changing colors and sizes manually.
If you decide to add more boxes or change the number, you must rewrite or copy-paste many lines. This wastes time and can cause mistakes like missing a box or repeating styles.
The @for loop in Sass lets you write one block of code that repeats automatically. Using through or to, you control if the loop includes the last number or stops before it, making your code cleaner and easier to update.
.box1 { color: red; } .box2 { color: orange; } .box3 { color: yellow; }@for $i from 1 through 3 { .box#{$i} { color: nth((red, orange, yellow), $i); } }
You can quickly create many similar styles with fewer lines and change the number of items by editing just one place.
When designing a navigation menu with multiple items, you can style each item with a loop instead of writing separate styles for each link.
@for loops automate repetitive CSS tasks.
through includes the last number; to stops before it.
Loops save time and reduce errors when scaling styles.
Practice
@for $i from 1 through 5 and @for $i from 1 to 5 in Sass?Solution
Step 1: Understand
Thethroughkeywordthroughkeyword means the loop will include the last number in the range.Step 2: Understand
Thetokeywordtokeyword means the loop stops before the last number, excluding it.Final Answer:
throughincludes 5,tostops before 5 -> Option AQuick Check:
throughincludes last number,toexcludes last number [OK]
- Confusing which keyword includes the last number
- Thinking both keywords behave the same
- Assuming
toincludes the last number
Solution
Step 1: Identify correct
The correct syntax uses@forsyntax@for $i from start to endorthrough.Step 2: Choose syntax for excluding last number
To exclude the last number, useto, so@for $i from 1 to 5 { }is correct.Final Answer:
@for $i from 1 to 5 { } -> Option CQuick Check:
Usetoto exclude last number [OK]
- Using 'through' when wanting to exclude last number
- Using 'in' instead of 'from'
- Using 'until' which is invalid in Sass
@for $i from 1 through 3 {
.item-#{$i} {
width: 10px * $i;
}
}Solution
Step 1: Understand loop range with
The loop runs from 1 to 3 including 3, so $i = 1, 2, 3.throughStep 2: Calculate width for each iteration
Widths are 10px * 1 = 10px, 10px * 2 = 20px, 10px * 3 = 30px.Final Answer:
.item-1 { width: 10px; } .item-2 { width: 20px; } .item-3 { width: 30px; } -> Option DQuick Check:
Loop includes 3, widths multiply by $i [OK]
- Thinking loop stops before 3
- Multiplying width incorrectly
- Missing last class .item-3
@for $i from 1 to 4 {
.box-#{$i} {
height: 5px * $i;
}
}
@for $j from 1 through 4
.circle-#{$j} {
width: 5px * $j;
}Solution
Step 1: Check syntax of first loop
The first loop uses correct syntax with curly braces around the block.Step 2: Check syntax of second loop
The second loop is missing curly braces after the loop declaration, which is required.Final Answer:
Missing curly braces in second loop -> Option AQuick Check:
Loops need curly braces for their blocks [OK]
- Omitting curly braces after @for
- Confusing 'to' and 'through' keywords
- Incorrect multiplication syntax
.btn-1 to .btn-4 using a Sass @for loop. Which loop should you use to include the last button class .btn-4?Solution
Step 1: Understand loop range needed
You want classes from 1 to 4 inclusive, so the loop must include 4.Step 2: Choose correct keyword to include last number
Usingthroughincludes the last number, so@for $i from 1 through 4is correct.Final Answer:
@for $i from 1 through 4 { ... } -> Option BQuick Check:
Usethroughto include last number [OK]
- Using 'to' which excludes last number
- Using invalid keyword 'until'
- Using range that goes beyond needed number
Ready to prove it?
Pass the test and this pattern moves unknown
