The @each loop helps you repeat styles for each item in a list. It saves time and keeps your code neat.
@each loop over lists in SASS
@each $item in $list {
// styles using $item
}The $item is a variable that holds each value from the list one by one.
The $list is the list of values you want to loop over.
.text-red, .text-green, and .text-blue, each with the matching text color.$colors: red, green, blue; @each $color in $colors { .text-#{$color} { color: $color; } }
$sizes: 1rem, 2rem, 3rem; @each $size in $sizes { .margin-#{$size} { margin: $size; } }
$empty-list: (); @each $item in $empty-list { .class-#{$item} { color: $item; } } // No output because the list is empty.
$single-item: purple; @each $color in $single-item { .text-#{$color} { color: $color; } }
This code creates three button styles with different background colors from the list. The text color is a lighter shade of the background for good contrast. Each button has padding, rounded corners, and some margin.
@use "sass:color"; $button-colors: #ff0000, #00ff00, #0000ff; @each $button-color in $button-colors { .btn-#{$button-color} { background-color: $button-color; color: color.scale($button-color, $lightness: 80%); padding: 1rem 2rem; border-radius: 0.5rem; font-weight: bold; display: inline-block; margin: 0.5rem; } }
The @each loop runs once for every item in the list.
It is faster and cleaner than writing repetitive CSS for each item.
Remember to use interpolation #{$variable} when using variables inside class names or selectors.
@each loops over each item in a list to create repeated styles.
Use it to save time and avoid writing the same code many times.
Works well with colors, sizes, or any list of values.
Practice
@each loop do in Sass when used with a list?Solution
Step 1: Understand the purpose of
The@each@eachloop is designed to run a block of code for every item in a list.Step 2: Identify what happens with each item
For each item, it repeats the styles or code inside the loop, creating multiple style blocks.Final Answer:
It repeats a block of styles for each item in the list. -> Option AQuick Check:
@eachloops = repeat styles [OK]
@each repeats code for every list item [OK]- Thinking
@eachmodifies the list itself - Confusing
@eachwith list functions like map - Assuming it deletes or filters items
(red, green, blue) using @each in Sass?Solution
Step 1: Identify correct
The correct syntax uses@eachsyntax@each $variable in (list)to loop over items.Step 2: Check each option
@each $color in (red, green, blue) { color: $color; } uses@each $color in (red, green, blue)which is correct. Options A, B, and D use wrong keywords or miss the $ sign.Final Answer:
@each $color in (red, green, blue) { color: $color; } -> Option AQuick Check:
Use@each $var in (list)syntax [OK]
- Omitting the $ before the variable
- Using wrong keywords like 'from' or 'on'
- Not wrapping list in parentheses
@each $size in (small, medium, large) {
.btn-#{$size} {
font-size: if($size == small, 1rem, if($size == medium, 1.5rem, 2rem));
}
}Solution
Step 1: Understand the loop and interpolation
The loop runs over sizes: small, medium, large. The class name uses interpolation to create .btn-small, .btn-medium, .btn-large.Step 2: Evaluate the font-size condition
Using nestedif: small -> 1rem, medium -> 1.5rem, large -> 2rem.Final Answer:
.btn-small { font-size: 1rem; } .btn-medium { font-size: 1.5rem; } .btn-large { font-size: 2rem; } -> Option CQuick Check:
Loop + interpolation + if = correct CSS [OK]
- Mixing up font-size values
- Thinking nested if causes syntax error
- Ignoring interpolation in class names
@each loop:@each $color (red, green, blue) {
.text-#{$color} {
color: $color;
}
}Solution
Step 1: Check the
The correct syntax requires the keyword@eachloop syntaxinbetween the variable and the list.Step 2: Identify the missing keyword
The code missesinafter$color, causing a syntax error.Final Answer:
Missing 'in' keyword after $color in the loop. -> Option DQuick Check:
@each $var in (list)needs 'in' [OK]
- Forgetting the 'in' keyword
- Removing $ from variable names
- Misusing interpolation syntax
(10px, 20px, 0, 30px), how can you use @each to create margin classes but skip the zero value?Solution
Step 1: Loop over all margin values
The@eachloop goes through each value including zero.Step 2: Use
Inside the loop, add@ifto skip zero@if $m != 0to only create classes for non-zero margins.Final Answer:
@each $m in (10px, 20px, 0, 30px) { @if $m != 0 { .m-#{$m} { margin: $m; } } } -> Option BQuick Check:
Use@ifinside@eachto filter items [OK]
- Not filtering zero, so unwanted class is created
- Using wrong condition like == 0 instead of != 0
- Trying to filter outside the loop
Ready to prove it?
Pass the test and this pattern moves unknown
