-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path6-use-each-to-map-over-items-in-a-list.html
50 lines (40 loc) · 1.58 KB
/
6-use-each-to-map-over-items-in-a-list.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!-- Use @each to Map Over Items in a List
The last challenge showed how the @for directive uses a starting and ending value to loop a certain number of times. Sass also offers the @each directive which loops over each item in a list or map. On each iteration, the variable gets assigned to the current value from the list or map.
@each $color in blue, red, green {
.#{$color}-text {color: $color;}
}
A map has slightly different syntax. Here's an example:
$colors: (color1: blue, color2: red, color3: green);
@each $key, $color in $colors {
.#{$color}-text {color: $color;}
}
Note that the $key variable is needed to reference the keys in the map. Otherwise, the compiled CSS would have color1, color2... in it. Both of the above code examples are converted into the following CSS:
.blue-text {
color: blue;
}
.red-text {
color: red;
}
.green-text {
color: green;
}
Write an @each directive that goes through a list: blue, black, red and assigns each variable to a .color-bg class, where the color part changes for each item. Each class should set the background-color the respective color.
- Your code should use the @each directive.
- Your .blue-bg class should have a background-color of blue.
- Your .black-bg class should have a background-coloç95r of black.
- Your .red-bg class should have a background-color of red.
-->
<style type='text/scss'>
@each $color in blue, black, red {
.#{$color}-bg {
background-color: $color;
}
}
div {
height: 200px;
width: 200px;
}
</style>
<div class="blue-bg"></div>
<div class="black-bg"></div>
<div class="red-bg"></div>