Loops help create many grid column styles quickly without writing each one by hand.
Grid column generator with loops in SASS
Start learning this pattern below
Jump into concepts and practice - no test required
@for $i from 1 through $total-columns { .col-#{$i} { grid-column: span $i; } }
The @for loop runs from a start number to an end number.
Use #{$i} to insert the loop number into class names or values.
.col-1 to .col-4 that span 1 to 4 columns.@for $i from 1 through 4 { .col-#{$i} { grid-column: span $i; } }
$total-columns makes it easy to change how many columns you generate.$total-columns: 6; @for $i from 1 through $total-columns { .col-#{$i} { grid-column: span $i; } }
This code creates a grid container with 4 equal columns. It uses a loop to make classes .col-1 to .col-4. Each class spans that many columns. The background color changes for each column size to help see the difference.
@use 'sass:math'; $total-columns: 4; .container { display: grid; grid-template-columns: repeat(#{$total-columns}, 1fr); gap: 1rem; max-width: 600px; margin: 2rem auto; } @for $i from 1 through $total-columns { .col-#{$i} { grid-column: span $i; background-color: hsl(math.div(360, $total-columns) * $i, 70%, 80%); padding: 1rem; border: 1px solid hsl(math.div(360, $total-columns) * $i, 70%, 50%); text-align: center; font-weight: bold; border-radius: 0.5rem; } }
Use @use 'sass:math' for math functions like division to avoid warnings.
Loop variables can be used inside strings with #{$variable} to create dynamic class names.
Changing $total-columns updates the grid and all column classes automatically.
Loops in Sass help generate many grid column classes quickly and consistently.
Use @for with variables to make your grid flexible and easy to update.
Dynamic class names and properties make your CSS cleaner and easier to maintain.
Practice
@for loop in SASS help you do when creating grid columns?Solution
Step 1: Understand the purpose of
The@forin SASS@forloop repeats code blocks a set number of times, useful for generating CSS classes.Step 2: Connect
Using@forwith grid columns@for, you can create many classes like.col-1,.col-2, etc., each with different widths.Final Answer:
Generate multiple CSS classes for different column widths automatically -> Option DQuick Check:
@forloop = Generate CSS classes [OK]
@for as a shortcut to write many classes fast [OK]- Confusing SASS loops with JavaScript loops
- Thinking
@forwrites HTML elements - Assuming it adds images or content
Solution
Step 1: Recall correct
The correct syntax uses@forsyntax in SASS@for $var from start through endto include the end number.Step 2: Check each option
@for $i from 1 through 4 { .col-#{$i} { width: 25% * $i; } } uses@for $i from 1 through 4, which is correct. Options A, B, and C use invalid keywords or syntax.Final Answer:
@for $i from 1 through 4 { .col-#{$i} { width: 25% * $i; } } -> Option CQuick Check:
Correct@forsyntax = @for $i from 1 through 4 { .col-#{$i} { width: 25% * $i; } } [OK]
- Using 'in' instead of 'from' and 'through'
- Using '@loop' which is not valid SASS
- Confusing '@each' with '@for'
@for $i from 1 through 3 {
.col-#{$i} {
grid-column: span $i;
}
}What CSS will be generated?
Solution
Step 1: Understand the loop iterations
The loop runs for $i = 1, 2, 3, creating classes .col-1, .col-2, .col-3.Step 2: Check the property values
Each class setsgrid-column: span $i;, so .col-1 spans 1 column, .col-2 spans 2, and .col-3 spans 3.Final Answer:
.col-1 { grid-column: span 1; } .col-2 { grid-column: span 2; } .col-3 { grid-column: span 3; } -> Option BQuick Check:
Loop variable $i matches span value [OK]
- Confusing 'span $i' with just '$i'
- Reversing the span values
- Assuming syntax error due to interpolation
@for $i from 1 to 4 {
.col-#{$i} {
width: 100% / $i;
}
}Solution
Step 1: Check the loop syntax
The loop uses@for $i from 1 to 4, which excludes the end number 4. Usethroughto include it.Step 2: Verify other parts
Class name uses interpolation correctly. Division in width is valid in SASS. Loop variable is declared.Final Answer:
Using 'to' instead of 'through' in the loop range -> Option AQuick Check:
Loop range must use 'through' for inclusive end [OK]
- Using 'to' which excludes the last number
- Forgetting interpolation in class names
- Thinking division in width is invalid
.col-2, .col-4, and .col-6 with widths as fractions of 6 columns?Solution
Step 1: Understand how to loop only even numbers
SASS@forloops do not support 'step' directly, so use@if $i % 2 == 0to filter even numbers.Step 2: Check each option
@for $i from 2 through 6 { @if $i % 2 == 0 { .col-#{$i} { width: (100% / 6) * $i; } } } uses@if $i % 2 == 0correctly. @for $i from 1 through 6 { @if $i / 2 == 0 { .col-#{$i} { width: (100% / 6) * $i; } } } uses division instead of modulo, which is wrong. @each $i from (2, 4, 6) { .col-#{$i} { width: (100% / 6) * $i; } } uses invalid syntax ('from' instead of 'in' for @each). @for $i from 2 to 6 step 2 { .col-#{$i} { width: (100% / 6) * $i; } } uses invalid 'step' syntax.Final Answer:
@for $i from 2 through 6 { @if $i % 2 == 0 { .col-#{$i} { width: (100% / 6) * $i; } } } -> Option AQuick Check:
Use modulo (%) to filter even numbers in loops [OK]
- Using division (/) instead of modulo (%) for condition
- Trying to use 'step' in @for which is invalid
- Confusing @each with @for loops
