Skip to content

Instantly share code, notes, and snippets.

@cristinafsanz
Last active September 6, 2022 15:18
Show Gist options
  • Select an option

  • Save cristinafsanz/b7aa805047301dfc46c254d17f529471 to your computer and use it in GitHub Desktop.

Select an option

Save cristinafsanz/b7aa805047301dfc46c254d17f529471 to your computer and use it in GitHub Desktop.
<section>
  <div class="ballet"></div>
  <div class="cat"></div>
  <div class="gramophone"></div>
  <div class="sparrow"></div>
  <div class="sunset"></div>
  <div class="turkey"></div>
</section>

Without each:

section {
  div {
    width: 100px;
    height: 100px;
  }
  
  .ballet {
    background-image: url:("../img/ballet.jpg");
    background-size: cover;
  }
  .cat {
    background-image: url:("../img/cat.jpg");
    background-size: cover;
  }
}

Method 1:

$bg-pics: (ballet, cat, gramophone, sparrow, sunset, turkey);

@each $bg-divs in $bg-pics {
  .#{$bg-divs} { //first would be .ballet
    background-image: url("../img/#{$bg-divs}.jpg");
  }
}

Method 2:

@each $bg-divs in (ballet, cat, gramophone, sparrow, sunset, turkey) {
  .#{$bg-divs} { 
    background-image: url("../img/#{$bg-divs}.jpg");
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment