Files
TylerGlenski 8d5d211975 SASS. @each. Replace stub (#34844)
* SASS. @each. Replace stub

Added 1x guide/example, 1x problem, and 1x solution.

* Removed problem from solution

Removed the problem from solution.

* Fix languages and made formatting changes
2019-03-09 12:33:18 +05:30

964 B

title
title
Use @each to Map Over Items in a List

Use @each to Map Over Items in a List

The @each directive loops over a list and for each iteration the variable is assigned the value in the list.

Example:

<style type='text/sass'>
  
@each $color in white, black, blue {
  .#{$color}-font {
    color: $color;
  }
}

</style>

<div class="white-font"></div>
<div class="black-font"></div>
<div class="blue-font"></div>

Solution

<style type='text/sass'>

  @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>

The solution above will generate the following CSS:

.blue-bg {
   background-color: blue;
 }
 
.black-bg {
   background-color: black;
 }
 
.red-bg {
   background-color: red;
 }
 
div {
  height: 200px;
  width: 200px;
}