The @each loop helps you repeat styles for many items in a map easily. It saves time and keeps your code neat.
@each loop over maps in SASS
@each $key, $value in $map {
// CSS styles using $key and $value
}The $key is the map's key (like a name).
The $value is the map's value (like a color or size).
$colors: (primary: #06c, secondary: #ccc); @each $name, $color in $colors { .btn-#{$name} { background-color: $color; } }
$fonts: (small: 0.8rem, medium: 1rem, large: 1.2rem); @each $size, $value in $fonts { h#{$size} { font-size: $value; } }
This code creates three CSS classes: .theme-light, .theme-dark, and .theme-blue. Each class sets a background color from the map. The text color changes to black for the light theme and white for others. Padding, rounded corners, and spacing are added for style.
@use "sass:map"; $themes: ( light: #fff, dark: #000, blue: #00f ); @each $name, $color in $themes { .theme-#{$name} { background-color: $color; color: if($name == light, #000, #fff); padding: 1rem; border-radius: 0.5rem; margin-bottom: 1rem; } }
Use #{$variable} to insert variables inside selectors or property names.
Maps store pairs like a dictionary: keys and values.
You can use if() inside styles to choose values based on conditions.
@each loops over map keys and values to create repeated styles.
Use $key and $value to access each pair inside the loop.
This helps keep your CSS DRY (Don't Repeat Yourself) and easy to update.
Practice
@each loop do when used with a map in Sass?Solution
Step 1: Understand the purpose of
The@eachwith maps@eachloop 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 CQuick Check:
@eachloops over map pairs = repeats styles [OK]
@each repeats code for each map pair [OK]- Thinking
@eachdeletes map entries - Confusing map keys with list items
- Assuming it converts maps to lists
$colors with @each in Sass?Solution
Step 1: Recall the correct
The syntax requires two variables separated by a comma, then@eachsyntax for mapsin, 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 DQuick Check:
Two variables separated by comma +in+ map name [OK]
- Swapping key and value order
- Missing comma between variables
- Using wrong variable order or syntax
$themes: (light: #fff, dark: #000); @each $name, $color in $themes { .#{$name}-theme { background: $color; } }What CSS will this generate?Solution
Step 1: Understand the map and loop variables
The map has keyslightanddarkwith colors#fffand#000.Step 2: Analyze the generated CSS selectors and properties
The selector uses interpolation.#{$name}-themeproducing.light-themeand.dark-theme. The background uses the color value.Final Answer:
.light-theme { background: #fff; } .dark-theme { background: #000; } -> Option AQuick Check:
Interpolation creates .light-theme and .dark-theme with correct colors [OK]
- Forgetting interpolation syntax
- Using keys as colors instead of values
- Mixing up class name order
$sizes: (small: 1rem, medium: 2rem); @each $size in $sizes { .btn-#{$size} { font-size: $size; } }Solution
Step 1: Check the
The map has key-value pairs, so the loop needs two variables separated by a comma.@eachloop variablesStep 2: Identify the problem with one variable
The code uses only$sizeinstead of$key, $value, so it cannot access both parts of the map.Final Answer:
Using only one variable for a map instead of two. -> Option BQuick Check:
Maps need two variables in@eachloops [OK]
@each loops [OK]- Using one variable for map iteration
- Forgetting comma between variables
- Misusing interpolation syntax
$shadows: (none: 0 0 0 transparent, light: 0 1px 3px rgba(0,0,0,0.12), heavy: 0 4px 6px rgba(0,0,0,0.3));
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?Solution
Step 1: Identify correct variable order for map iteration
Maps use@each $key, $value in $map, so$nameis key and$shadowis 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 AQuick Check:
Key first, value second, use interpolation for class [OK]
- Swapping key and value variables
- Using only one variable for map
- Interpolating value instead of key in class name
Ready to prove it?
Pass the test and this pattern moves unknown
