0
0
SASSmarkup~5 mins

@each loop over maps in SASS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the @each loop in Sass when used with maps?
The @each loop in Sass lets you go through each key-value pair in a map, so you can use those values to create styles dynamically.
Click to reveal answer
beginner
How do you access the key and value inside an @each loop over a Sass map?
You write @each $key, $value in $map. This sets $key to the map's key and $value to its value for each loop cycle.
Click to reveal answer
intermediate
Example: What does this Sass code do?<br>
@each $color, $hex in $colors {
  .text-#{$color} {
    color: $hex;
  }
}
It creates CSS classes like .text-red or .text-blue with the text color set to the corresponding hex value from the $colors map.
Click to reveal answer
intermediate
Can you use @each to loop over just the keys or just the values of a Sass map?
Yes! You can loop over just keys by using map-keys($map) or just values with map-values($map) inside the @each loop.
Click to reveal answer
beginner
Why is using @each loop over maps helpful in real web design?
It helps keep your styles organized and easy to update. For example, changing a color in one place updates all related styles automatically.
Click to reveal answer
What does @each $key, $value in $map do in Sass?
AConverts the map to a list
BLoops through each key-value pair in the map
CDeletes all keys in the map
DCreates a new map
How do you write a loop to get only the keys from a Sass map?
A@each $key in map-keys($map)
B@each $key in map-values($map)
C@each $value in $map
D@each $key, $value in $map
What will this Sass code generate?<br>
$colors: (red: #f00, blue: #00f);
@each $name, $hex in $colors {
  .bg-#{$name} {
    background-color: $hex;
  }
}
AAn error because maps can't be looped
BCSS classes .red and .blue with text colors
CNothing, it's invalid syntax
DCSS classes .bg-red and .bg-blue with background colors
Which Sass function returns all values from a map?
Amap-values()
Bmap-keys()
Cmap-get()
Dmap-remove()
Why is looping over maps with @each useful?
AIt slows down the CSS rendering
BIt deletes unused styles automatically
CIt helps create repetitive styles easily and keeps code DRY
DIt converts CSS to JavaScript
Explain how to use the @each loop to create CSS classes from a Sass map of colors.
Think about how to turn map keys into class names and values into style values.
You got /4 concepts.
    Describe the difference between looping over a map directly and looping over map-keys or map-values in Sass.
    Consider what you want to access inside the loop.
    You got /4 concepts.