[Read @for loop] -> [Iterate from 1 to 5] -> [Generate .m-1, .m-2, ... classes] -> [Compile CSS rules] -> [Apply styles to HTML elements]
Generating utility classes with loops in SASS - Browser Rendering Trace
@for $i from 1 through 5 { .m-#{$i} { margin: #{$i}rem; } }[Box 1] [Box 2] [Box 3]
[Box 1] [Box 2] [Box 3]
<div class="m-1">Box 1</div> <div class="m-3">Box 2</div> <div class="m-5">Box 3</div>
@for $i from 1 through 5 { .m-#{$i} { margin: #{$i}rem; } }
| Property | Value Applied | Visual Effect | Common Use |
|---|---|---|---|
| margin | 1rem | Small space around element | Small gaps between elements |
| margin | 3rem | Medium space around element | Moderate gaps for clearer separation |
| margin | 5rem | Large space around element | Big gaps for distinct separation |
Practice
@for loops in Sass when generating utility classes?Solution
Step 1: Understand the purpose of
@forloops in SassThe
@forloop is used to repeat CSS code multiple times with slight variations, such as different class names.Step 2: Identify the benefit in utility class generation
Using loops lets you create many utility classes quickly and ensures consistent naming and styling without writing repetitive code.
Final Answer:
It creates many classes quickly and keeps styles consistent. -> Option BQuick Check:
Loops speed up class creation = B [OK]
- Thinking loops add JavaScript behavior
- Believing loops reduce CSS file size automatically
- Confusing loops with HTML structure changes
.m-1 to .m-5 using a Sass @for loop?Solution
Step 1: Recall Sass
@forloop syntaxThe correct syntax uses
@for $var from start through endortofor exclusive end.Step 2: Identify the correct loop and interpolation
@for $i from 1 through 5 { .m-#{$i} { margin: $i * 1rem; } } uses
from 1 through 5and interpolates#{$i}correctly in the class name.Final Answer:
@for $i from 1 through 5 { .m-#{$i} { margin: $i * 1rem; } } -> Option AQuick Check:
Correct Sass loop syntax = D [OK]
- Using JavaScript-style loops in Sass
- Wrong keywords like 'in' or 'loop'
- Missing interpolation syntax #{}
@for $i from 1 through 3 {
.p-#{$i} {
padding: $i * 0.5rem;
}
}Solution
Step 1: Understand the loop range and multiplication
The loop runs from 1 to 3. Each padding value is
$i * 0.5rem.Step 2: Calculate padding for each class
For
i=1, padding = 0.5rem; fori=2, padding = 1rem; fori=3, padding = 1.5rem.Final Answer:
.p-1 { padding: 0.5rem; } .p-2 { padding: 1rem; } .p-3 { padding: 1.5rem; } -> Option CQuick Check:
Multiply i by 0.5rem = C [OK]
- Confusing rem with px units
- Multiplying by wrong factor
- Misreading loop range
.w-1 to .w-4:@for $i from 1 to 4 {
.w-#{$i} {
width: $i * 10px;
}
}Solution
Step 1: Check the loop range keyword
The keyword 'to' excludes the last number, so loop runs 1 to 3, not 4.
Step 2: Confirm other syntax parts
Interpolation is correct, and multiplying number by px unit is valid in Sass.
Final Answer:
Using 'to' instead of 'through' excludes the last number. -> Option AQuick Check:
'to' excludes end number = A [OK]
- Confusing 'to' and 'through' keywords
- Thinking multiplication by px is invalid
- Starting loop at 0 when not needed
.m-0 to .m-5 but skip .m-3. Which Sass code correctly does this?Solution
Step 1: Understand the goal to skip .m-3
We want classes from 0 to 5 but not for 3.
Step 2: Check conditional logic in options
@for $i from 0 through 5 { @if $i != 3 { .m-#{$i} { margin: $i * 0.25rem; } } } uses
@if $i != 3to skip generating .m-3. @for $i from 0 through 5 { @if $i == 3 { .m-#{$i} { margin: $i * 0.25rem; } } } generates only .m-3. @for $i from 0 through 5 { .m-#{$i} { margin: $i * 0.25rem; } @if $i == 3 { @continue; } } tries to use@continueafter class creation, which is invalid in Sass. @for $i from 0 through 5 { .m-#{$i} { margin: $i * 0.25rem; } } generates all classes including .m-3.Final Answer:
@for $i from 0 through 5 { @if $i != 3 { .m-#{$i} { margin: $i * 0.25rem; } } } -> Option DQuick Check:
Use @if $i != 3 to skip class = A [OK]
- Using @continue incorrectly in Sass
- Generating unwanted classes by missing condition
- Using wrong condition to include only one value
Ready to prove it?
Pass the test and this pattern moves unknown
