📖 Scenario: You are designing a simple website theme. You want to create CSS classes for different button colors using Sass. Each button color has a name and a hex color code.
🎯 Goal: Build a Sass stylesheet that uses an @each loop to create CSS classes for buttons with background colors from a map.
📋 What You'll Learn
Create a Sass map called $button-colors with three entries: primary with #3498db, secondary with #2ecc71, and danger with #e74c3c.
Create a variable $padding set to 1rem.
Use an @each loop to generate CSS classes named .btn-primary, .btn-secondary, and .btn-danger with the correct background colors and padding.
Add a final style rule for .btn that sets the text color to white and border radius to 0.25rem.
💡 Why This Matters
🌍 Real World
Using Sass maps and loops helps you write less code and keep your styles consistent when designing websites with multiple color themes.
💼 Career
Front-end developers often use Sass to manage complex styles efficiently, especially when working on large projects with many design variations.
Progress0 / 4 steps
1
Create the $button-colors map
Create a Sass map called $button-colors with these exact entries: primary: #3498db, secondary: #2ecc71, and danger: #e74c3c.
SASS
Hint
Use parentheses ( ) to define a map in Sass. Separate entries with commas.
2
Add the $padding variable
Create a variable called $padding and set it to 1rem.
SASS
Hint
Variables in Sass start with $. Assign values with a colon and end with a semicolon.
3
Use @each loop to create button classes
Write an @each loop that iterates over $button-colors with variables $name and $color. Inside the loop, create CSS classes named .btn-#{$name} with background-color: $color and padding: $padding.
SASS
Hint
Use @each $name, $color in $button-colors { ... } to loop over map entries. Use interpolation #{$name} to build class names.
4
Add the base .btn style
Add a CSS rule for the class .btn that sets color to white and border-radius to 0.25rem.
SASS
Hint
Write a normal CSS rule for .btn with the specified properties.
Practice
(1/5)
1. What does the @each loop do when used with a map in Sass?
easy
A. It creates a new map from the keys only.
B. It deletes all map entries after looping.
C. It repeats styles for each key-value pair in the map.
D. It converts the map into a list.
Solution
Step 1: Understand the purpose of @each with maps
The @each loop iterates over each key-value pair in a map.
Step 2: Identify what happens during iteration
For each pair, it repeats the block of styles or code inside the loop.
Final Answer:
It repeats styles for each key-value pair in the map. -> Option C
Quick Check:
@each loops over map pairs = repeats styles [OK]
Hint: Remember: @each repeats code for each map pair [OK]
Common Mistakes:
Thinking @each deletes map entries
Confusing map keys with list items
Assuming it converts maps to lists
2. Which of the following is the correct syntax to loop over a map $colors with @each in Sass?
easy
A. @each $key $value in $colors { ... }
B. @each $colors in $key, $value { ... }
C. @each $value, $key in $colors { ... }
D. @each $key, $value in $colors { ... }
Solution
Step 1: Recall the correct @each syntax for maps
The syntax requires two variables separated by a comma, then in, then the map name.
Step 2: Match the syntax to the options
@each $key, $value in $colors { ... } correctly uses @each $key, $value in $colors. Others have wrong order or missing comma.
Final Answer:
@each $key, $value in $colors { ... } -> Option D
Quick Check:
Two variables separated by comma + in + map name [OK]
Hint: Use comma between key and value, then 'in' before map [OK]
Write a @each loop that creates classes .shadow-none, .shadow-light, and .shadow-heavy with the correct box-shadow property. Which code snippet is correct?
hard
A. @each $name, $shadow in $shadows { .shadow-#{$name} { box-shadow: $shadow; } }
B. @each $shadow, $name in $shadows { .shadow-#{$name} { box-shadow: $shadow; } }
C. @each $name in $shadows { .shadow-#{$name} { box-shadow: $name; } }
D. @each $name, $shadow in $shadows { .shadow-#{$shadow} { box-shadow: $name; } }
Solution
Step 1: Identify correct variable order for map iteration
Maps use @each $key, $value in $map, so $name is key and $shadow is value.
Step 2: Check class name and property usage
The class uses interpolation with the key $name, and the property uses the value $shadow.
Final Answer:
@each $name, $shadow in $shadows { .shadow-#{$name} { box-shadow: $shadow; } } -> Option A
Quick Check:
Key first, value second, use interpolation for class [OK]
Hint: Map loops: key first, value second, interpolate key for class [OK]
Common Mistakes:
Swapping key and value variables
Using only one variable for map
Interpolating value instead of key in class name
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.