The @for loop in Sass helps you repeat styles easily. Using through or to changes how many times the loop runs.
When you want to create multiple similar CSS classes quickly.
When you need to apply styles to a series of numbered elements.
When you want to generate a range of styles with small differences.
When you want to control if the loop includes the last number or stops before it.
Syntax
SASS
@for $var from <start> through <end> {
// styles
}
@for $var from <start> to <end> {
// styles
}
through includes the end number in the loop.
to stops before the end number, so it excludes it.
Examples
This creates classes .item-1, .item-2, and .item-3.
SASS
@for $i from 1 through 3 {
.item-#{$i} {
width: 10px * $i;
}
}
This creates classes .box-1, .box-2, and .box-3. It stops before 4.
SASS
@for $i from 1 to 4 {
.box-#{$i} {
height: 5px * $i;
}
}
Sample Program
This Sass code creates circles with sizes doubling from 2ยน to 2ยณ rem and squares with sizes from 1.5rem to 4.5rem but stops before 4, so squares 1 to 3.
Hint: Remember 'through' includes last number, so 3 iterations [OK]
Common Mistakes:
Thinking loop stops before 3
Multiplying width incorrectly
Missing last class .item-3
4. Identify the error in this Sass code:
@for $i from 1 to 4 {
.box-#{$i} {
height: 5px * $i;
}
}
@for $j from 1 through 4
.circle-#{$j} {
width: 5px * $j;
}
medium
A. Missing curly braces in second loop
B. Using through instead of to in second loop
C. Multiplication syntax is wrong
D. Variable names must be the same in both loops
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 A
Quick Check:
Loops need curly braces for their blocks [OK]
Hint: Always use curly braces {} after @for loop declaration [OK]
Common Mistakes:
Omitting curly braces after @for
Confusing 'to' and 'through' keywords
Incorrect multiplication syntax
5. You want to create 4 buttons with classes .btn-1 to .btn-4 using a Sass @for loop. Which loop should you use to include the last button class .btn-4?
hard
A. @for $i from 1 to 4 { ... }
B. @for $i from 1 through 4 { ... }
C. @for $i from 1 until 4 { ... }
D. @for $i from 1 to 5 { ... }
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
Using through includes the last number, so @for $i from 1 through 4 is correct.
Final Answer:
@for $i from 1 through 4 { ... } -> Option B
Quick Check:
Use through to include last number [OK]
Hint: Use 'through' to include last number in loop [OK]
Common Mistakes:
Using 'to' which excludes last number
Using invalid keyword 'until'
Using range that goes beyond needed number
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.