📖 Scenario: You are creating a simple style for a website that highlights different sections with distinct background colors. You want to use Sass to loop over a list of colors and apply each color to a different section.
🎯 Goal: Build a Sass stylesheet that uses an @each loop to assign background colors from a list to multiple sections with classes .section-1, .section-2, and .section-3.
📋 What You'll Learn
Create a Sass list variable named $colors with exactly these colors: red, green, blue
Create a variable $section-count set to 3
Use an @each loop with variables $color and $index to loop over $colors
Inside the loop, style the class .section-#{$index} with the background color set to $color
💡 Why This Matters
🌍 Real World
Using loops in Sass helps you write less repetitive CSS and easily manage styles for multiple elements with similar patterns.
💼 Career
Front-end developers often use Sass loops to create scalable and maintainable stylesheets for websites and applications.
Progress0 / 4 steps
1
Create the color list
Create a Sass list variable called $colors with these exact colors: red, green, blue
SASS
Hint
Use parentheses or commas to create a list in Sass. For example: $list: item1, item2;
2
Add the section count variable
Create a variable called $section-count and set it to 3
SASS
Hint
Use $variable-name: value; to create variables in Sass.
3
Write the @each loop with index
Write an @each loop using variables $color and $index to loop over $colors with index starting at 1
SASS
Hint
Use @each $item, $index in $list { ... } to loop with index in Sass.
4
Complete the style for sections
Ensure the loop styles the classes .section-1, .section-2, and .section-3 with their respective background colors from $colors
SASS
Hint
Inside the loop, set background-color to $color for each section.
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]