Discover how a simple loop can save you hours of tedious CSS writing!
Why @while loop usage in SASS? - Purpose & Use Cases
Imagine you want to create a set of buttons with different shades of blue, each slightly darker than the last. You write CSS for each button color by hand, repeating similar code over and over.
This manual method is slow and boring. If you want to change the number of buttons or the shade steps, you must rewrite or copy-paste many lines, risking mistakes and inconsistencies.
The @while loop in Sass lets you write one block of code that repeats automatically. It counts and changes values for you, so you only write the pattern once.
.btn-1 { background: blue; } .btn-2 { background: darkblue; } .btn-3 { background: navy; }
$i: 1; @while $i <= 3 { .btn-#{$i} { background: darken(blue, $i * 10%); } $i: $i + 1; }
You can create many style variations quickly and change them easily by adjusting just a few values.
Designers often need multiple button sizes or colors for a website. Using @while loops, they generate all these styles with less code and fewer errors.
Writing repetitive styles by hand is slow and error-prone.
@while loops automate repetition in Sass.
This saves time and makes style changes easier and safer.
Practice
@while loop do in Sass?Solution
Step 1: Understand the purpose of
The@whileloop@whileloop repeats code as long as a condition remains true.Step 2: Compare options with the loop behavior
Only Repeats styles while a condition is true correctly describes repeating styles while a condition is true.Final Answer:
Repeats styles while a condition is true -> Option DQuick Check:
@whileloops repeat while true [OK]
@while repeats until condition is false [OK]- Thinking it runs only once
- Confusing with static style blocks
- Believing it stops styles immediately
@while loop in Sass?Solution
Step 1: Check the correct
The correct syntax uses@whilesyntax@while condition { ... }without parentheses or semicolons.Step 2: Identify the option matching this syntax
@while $i < 5 { .box-#{$i} { width: 10px * $i; } } uses@while $i < 5 { ... }which is correct Sass syntax.Final Answer:
@while $i < 5 { .box-#{$i} { width: 10px * $i; } } -> Option AQuick Check:
Correct@whilesyntax uses braces, no parentheses [OK]
@while, no parentheses needed [OK]- Adding parentheses around condition
- Using semicolons after condition
- Missing braces for loop body
$i: 1;
@while $i <= 3 {
.item-#{$i} {
width: 10px * $i;
}
$i: $i + 1;
}Solution
Step 1: Understand the loop iterations
The loop runs while $i is 1, 2, and 3, incrementing $i each time.Step 2: Calculate the width for each iteration
Widths are 10px * 1 = 10px, 10px * 2 = 20px, 10px * 3 = 30px, generating three classes.Final Answer:
.item-1 { width: 10px; } .item-2 { width: 20px; } .item-3 { width: 30px; } -> Option CQuick Check:
Loop runs 3 times, width multiplies by $i [OK]
- Forgetting to increment $i
- Confusing units in multiplication
- Assuming syntax error without checking
@while loop:$i: 1;
@while $i < 4 {
.box-#{$i} {
height: 20px * $i;
}
}Solution
Step 1: Check loop variable update
The variable $i is never increased inside the loop, causing an infinite loop.Step 2: Confirm other syntax parts
Loop condition and braces are correct; multiplication with units is valid in Sass.Final Answer:
Missing increment of $i inside the loop -> Option AQuick Check:
Loop variable must change to stop loop [OK]
@while [OK]- Forgetting to increment loop variable
- Assuming syntax error instead of logic error
- Confusing multiplication units
@while. Which code correctly achieves this?$count: 1;
@while $count <= 4 {
.btn-#{$count} {
padding: 5px * $count;
}
$count: $count + 1;
}Solution
Step 1: Analyze loop condition and increment
The loop runs while $count is 1 to 4, incrementing $count each time.Step 2: Check padding calculation and class names
Each button gets padding 5px times $count, producing 5px, 10px, 15px, and 20px for .btn-1 to .btn-4.Final Answer:
Correctly creates .btn-1 to .btn-4 with padding 5px, 10px, 15px, 20px -> Option BQuick Check:
Loop with increment and multiplication works as expected [OK]
- Not incrementing loop variable
- Using parentheses incorrectly in Sass
@while - Forgetting to multiply padding by loop variable
Ready to prove it?
Pass the test and this pattern moves unknown
