Bird
Raised Fist0
SASSmarkup~20 mins

Generating utility classes with loops in SASS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Sass Loop Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
What is the output CSS of this Sass loop?
Given the Sass code below, what CSS does it generate?
$sizes: 1, 2, 3;
@each $size in $sizes {
  .m-#{$size} {
    margin: #{$size}rem;
  }
}
SASS
$sizes: 1, 2, 3;
@each $size in $sizes {
  .m-#{$size} {
    margin: #{$size}rem;
  }
}
A
.m-1 { margin: 1em; }
.m-2 { margin: 2em; }
.m-3 { margin: 3em; }
B
.m-1 { margin: 1px; }
.m-2 { margin: 2px; }
.m-3 { margin: 3px; }
C
.m-1 { margin: 1rem; }
.m-2 { margin: 2rem; }
.m-3 { margin: 3rem; }
D
.m-1 { padding: 1rem; }
.m-2 { padding: 2rem; }
.m-3 { padding: 3rem; }
Attempts:
2 left
💡 Hint
Look at how the variable $size is used inside the loop and the unit applied.
🧠 Conceptual
intermediate
1:30remaining
How many classes are generated by this Sass loop?
Consider the Sass code below:
@for $i from 1 through 4 {
  .p-#{$i} {
    padding: #{$i}rem;
  }
}

How many CSS classes will this generate?
SASS
@for $i from 1 through 4 {
  .p-#{$i} {
    padding: #{$i}rem;
  }
}
A1
B4
C3
D5
Attempts:
2 left
💡 Hint
The @for loop includes both the start and end numbers.
❓ selector
advanced
2:30remaining
Which option produces correct hover utility classes?
You want to generate hover background color utility classes for colors red, green, and blue using Sass loops. Which option produces the correct CSS?
$colors: red, green, blue;
@each $color in $colors {
  .bg-#{$color}:hover {
    background-color: $color;
  }
}
SASS
$colors: red, green, blue;
@each $color in $colors {
  .bg-#{$color}:hover {
    background-color: $color;
  }
}
A
.bg-red:hover { background-color: $color; }
.bg-green:hover { background-color: $color; }
.bg-blue:hover { background-color: $color; }
B
.bg-red { background-color: red; }
.bg-green { background-color: green; }
.bg-blue { background-color: blue; }
C
.bg-red:hover { background-color: #red; }
.bg-green:hover { background-color: #green; }
.bg-blue:hover { background-color: #blue; }
D
.bg-red:hover { background-color: red; }
.bg-green:hover { background-color: green; }
.bg-blue:hover { background-color: blue; }
Attempts:
2 left
💡 Hint
Check how the variable $color is used inside the CSS property and selector.
❓ layout
advanced
2:30remaining
What visual result does this Sass grid utility generate?
Given this Sass code:
@for $i from 1 through 3 {
  .grid-cols-#{$i} {
    display: grid;
    grid-template-columns: repeat(#{$i}, 1fr);
  }
}

What will you see when applying grid-cols-2 to a container with 4 items?
SASS
@for $i from 1 through 3 {
  .grid-cols-#{$i} {
    display: grid;
    grid-template-columns: repeat(#{$i}, 1fr);
  }
}
AThe container shows 2 equal columns with 4 items distributed across 2 rows.
BThe container shows 1 column with 4 items stacked vertically.
CThe container shows 3 columns with 4 items in a single row.
DThe container shows 4 columns with 4 items in a single row.
Attempts:
2 left
💡 Hint
The class sets grid-template-columns to repeat the number of columns specified.
❓ accessibility
expert
3:00remaining
Which Sass loop generates utility classes with proper ARIA roles for buttons?
You want to create button utility classes with different colors and ensure each button has an ARIA role="button" attribute in HTML. Which Sass code snippet helps generate the CSS classes correctly?
$btn-colors: primary, secondary;
@each $color in $btn-colors {
  .btn-#{$color} {
    background-color: map-get($colors, $color);
    color: white;
  }
}

Note: The ARIA role is added in HTML, but you want the CSS classes to reflect the correct styling for accessibility focus states.
SASS
$btn-colors: primary, secondary;
@each $color in $btn-colors {
  .btn-#{$color} {
    background-color: map-get($colors, $color);
    color: white;
  }
}
A
@each $color in $btn-colors {
  .btn-#{$color} {
    background-color: map-get($colors, $color);
    color: white;
  }
  .btn-#{$color}:focus-visible {
    outline: 3px solid #000;
    outline-offset: 2px;
  }
}
B
@each $color in $btn-colors {
  .btn-#{$color} {
    background-color: map-get($colors, $color);
    color: white;
    role: button;
  }
}
C
@each $color in $btn-colors {
  .btn-#{$color} {
    background-color: map-get($colors, $color);
    color: white;
    aria-role: button;
  }
}
D
@each $color in $btn-colors {
  .btn-#{$color} {
    background-color: map-get($colors, $color);
    color: white;
    &:hover {
      cursor: pointer;
    }
  }
}
Attempts:
2 left
💡 Hint
ARIA roles are added in HTML, but CSS can improve accessibility by styling focus states.

Practice

(1/5)
1. What is the main benefit of using @for loops in Sass when generating utility classes?
easy
A. It automatically adds JavaScript functionality to classes.
B. It creates many classes quickly and keeps styles consistent.
C. It reduces the file size by removing unused CSS.
D. It changes the HTML structure dynamically.

Solution

  1. Step 1: Understand the purpose of @for loops in Sass

    The @for loop is used to repeat CSS code multiple times with slight variations, such as different class names.

  2. 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.

  3. Final Answer:

    It creates many classes quickly and keeps styles consistent. -> Option B
  4. Quick Check:

    Loops speed up class creation = B [OK]
Hint: Loops generate many classes fast and consistent [OK]
Common Mistakes:
  • Thinking loops add JavaScript behavior
  • Believing loops reduce CSS file size automatically
  • Confusing loops with HTML structure changes
2. Which of the following is the correct syntax to create utility classes .m-1 to .m-5 using a Sass @for loop?
easy
A. @for $i from 1 through 5 { .m-#{$i} { margin: $i * 1rem; } }
B. @for $i in 1 to 5 { .m-#{$i} { margin: $i * 1rem; } }
C. @for $i = 1; $i <= 5; $i++ { .m-#{$i} { margin: $i * 1rem; } }
D. @loop $i from 1 to 5 { .m-#{$i} { margin: $i * 1rem; } }

Solution

  1. Step 1: Recall Sass @for loop syntax

    The correct syntax uses @for $var from start through end or to for exclusive end.

  2. Step 2: Identify the correct loop and interpolation

    @for $i from 1 through 5 { .m-#{$i} { margin: $i * 1rem; } } uses from 1 through 5 and interpolates #{$i} correctly in the class name.

  3. Final Answer:

    @for $i from 1 through 5 { .m-#{$i} { margin: $i * 1rem; } } -> Option A
  4. Quick Check:

    Correct Sass loop syntax = D [OK]
Hint: Use '@for $i from 1 through 5' with #{$i} interpolation [OK]
Common Mistakes:
  • Using JavaScript-style loops in Sass
  • Wrong keywords like 'in' or 'loop'
  • Missing interpolation syntax #{}
3. What CSS classes will be generated by this Sass code?
@for $i from 1 through 3 {
  .p-#{$i} {
    padding: $i * 0.5rem;
  }
}
medium
A. .p-1 { padding: 0.05rem; } .p-2 { padding: 0.1rem; } .p-3 { padding: 0.15rem; }
B. .p-1 { padding: 1rem; } .p-2 { padding: 2rem; } .p-3 { padding: 3rem; }
C. .p-1 { padding: 0.5rem; } .p-2 { padding: 1rem; } .p-3 { padding: 1.5rem; }
D. .p-1 { padding: 0.5px; } .p-2 { padding: 1px; } .p-3 { padding: 1.5px; }

Solution

  1. Step 1: Understand the loop range and multiplication

    The loop runs from 1 to 3. Each padding value is $i * 0.5rem.

  2. Step 2: Calculate padding for each class

    For i=1, padding = 0.5rem; for i=2, padding = 1rem; for i=3, padding = 1.5rem.

  3. Final Answer:

    .p-1 { padding: 0.5rem; } .p-2 { padding: 1rem; } .p-3 { padding: 1.5rem; } -> Option C
  4. Quick Check:

    Multiply i by 0.5rem = C [OK]
Hint: Multiply loop index by 0.5rem for padding values [OK]
Common Mistakes:
  • Confusing rem with px units
  • Multiplying by wrong factor
  • Misreading loop range
4. Identify the error in this Sass code that tries to generate width classes .w-1 to .w-4:
@for $i from 1 to 4 {
  .w-#{$i} {
    width: $i * 10px;
  }
}
medium
A. Using 'to' instead of 'through' excludes the last number.
B. Missing interpolation #{} in class name.
C. Multiplying number by px unit directly causes error.
D. Loop variable $i should start from 0.

Solution

  1. Step 1: Check the loop range keyword

    The keyword 'to' excludes the last number, so loop runs 1 to 3, not 4.

  2. Step 2: Confirm other syntax parts

    Interpolation is correct, and multiplying number by px unit is valid in Sass.

  3. Final Answer:

    Using 'to' instead of 'through' excludes the last number. -> Option A
  4. Quick Check:

    'to' excludes end number = A [OK]
Hint: Use 'through' to include last number in @for loops [OK]
Common Mistakes:
  • Confusing 'to' and 'through' keywords
  • Thinking multiplication by px is invalid
  • Starting loop at 0 when not needed
5. You want to generate margin utility classes .m-0 to .m-5 but skip .m-3. Which Sass code correctly does this?
hard
A. @for $i from 0 through 5 { .m-#{$i} { margin: $i * 0.25rem; } }
B. @for $i from 0 through 5 { @if $i == 3 { .m-#{$i} { margin: $i * 0.25rem; } } }
C. @for $i from 0 through 5 { .m-#{$i} { margin: $i * 0.25rem; } @if $i == 3 { @continue; } }
D. @for $i from 0 through 5 { @if $i != 3 { .m-#{$i} { margin: $i * 0.25rem; } } }

Solution

  1. Step 1: Understand the goal to skip .m-3

    We want classes from 0 to 5 but not for 3.

  2. 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 != 3 to 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 @continue after 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.

  3. Final Answer:

    @for $i from 0 through 5 { @if $i != 3 { .m-#{$i} { margin: $i * 0.25rem; } } } -> Option D
  4. Quick Check:

    Use @if $i != 3 to skip class = A [OK]
Hint: Use @if to exclude unwanted loop values [OK]
Common Mistakes:
  • Using @continue incorrectly in Sass
  • Generating unwanted classes by missing condition
  • Using wrong condition to include only one value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard