It repeats a block of styles for each item in a list or map, helping you write less code and keep styles consistent.
Click to reveal answer
beginner
How do you write an @each loop to go through a list of colors named $colors?
You write:
@each $color in $colors { /* styles using $color */ }
Click to reveal answer
intermediate
Can @each loop handle multiple variables at once?
Yes! If the list contains sublists, you can unpack them like:
@each $name, $value in $list { /* use $name and $value */ }
Click to reveal answer
beginner
Why use @each loop instead of writing styles manually?
It saves time, reduces mistakes, and makes your code easier to update because you change the list once and all styles update automatically.
Click to reveal answer
intermediate
What happens if you use @each with an empty list?
The loop does nothing and no styles are generated, so it’s safe to use even if the list might be empty.
Click to reveal answer
What keyword starts a loop over a list in Sass?
A@for
B@while
C@each
D@loop
✗ Incorrect
The @each keyword is used to loop over lists or maps in Sass.
How do you access each item in the list inside an @each loop?
AUsing a function call
BUsing an index number
CUsing a map key
DUsing a variable declared after <code>in</code>
✗ Incorrect
You declare a variable after in to represent each item as the loop runs.
Which of these is a valid @each loop syntax for a list $sizes?
A@each $size in $sizes { ... }
B@each $sizes in $size { ... }
C@each $size from $sizes { ... }
D@each $sizes from $size { ... }
✗ Incorrect
The correct syntax is @each $variable in $list { ... }.
If a list contains pairs like (name, value), how do you loop over them?
A@each $name, $value in $list { ... }
B@each $pair in $list { ... }
C@each $name in $value { ... }
D@each $value, $name in $list { ... }
✗ Incorrect
You can unpack pairs by listing multiple variables separated by commas.
What happens if the list used in @each is empty?
AAn error occurs
BNo styles are generated
CThe loop runs once with no value
DThe compiler crashes
✗ Incorrect
The loop simply does nothing and no styles are output.
Explain how to use the @each loop in Sass to style multiple elements with different colors.
Think about how you would paint different walls with different colors using a list.
You got /3 concepts.
Describe the benefits of using @each loops over writing repetitive CSS manually.
Imagine changing many similar things at once by changing just one list.
You got /4 concepts.
Practice
(1/5)
1. What does the @each loop do in Sass when used with a list?
easy
A. It repeats a block of styles for each item in the list.
B. It creates a new list from the original list.
C. It deletes items from the list one by one.
D. It converts the list into a map automatically.
Solution
Step 1: Understand the purpose of @each
The @each loop 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 A
Quick Check:
@each loops = repeat styles [OK]
Hint: Remember: @each repeats code for every list item [OK]
Common Mistakes:
Thinking @each modifies the list itself
Confusing @each with list functions like map
Assuming it deletes or filters items
2. Which of the following is the correct syntax to loop over a list (red, green, blue) using @each in Sass?
easy
A. @each $color in (red, green, blue) { color: $color; }
B. @each color in red, green, blue { color: color; }
C. @each $color from (red, green, blue) { color: $color; }
D. @each $color on (red, green, blue) { color: $color; }
Solution
Step 1: Identify correct @each syntax
The correct syntax uses @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 A
Quick Check:
Use @each $var in (list) syntax [OK]
Hint: Use 'in' keyword and $variable for each item [OK]