S
S
sasha_jarvi2019-06-03 21:28:44
Sass
sasha_jarvi, 2019-06-03 21:28:44

How to write a SCSS function to change background-color from line to line?

There is a table:

<table>
      <tr>
        <td>red</td>
      </tr>
      <tr>
        <td>orange</td>
      </tr>
      <tr>
        <td>yellow</td>
      </tr>
      <tr>
        <td>green</td>
      </tr>
      <tr>
        <td>blue</td>
      </tr>
      <tr>
        <td>indigo</td>
      </tr>
      <tr>
        <td>violet</td>
      </tr>
    </table>

Each row has a background-colorcorresponding color of the rainbow.
At the moment, setting a property background-colorfor each row looks like this:
tr {
      &:first-child {
        background-color: adjust-hue($bg-base, 0deg);
      }
      &:nth-child(2) {
        background-color: adjust-hue($bg-base, 30deg);
      }
      &:nth-child(3) {
        background-color: adjust-hue($bg-base, 60deg);
      }
      &:nth-child(4) {
        background-color: adjust-hue($bg-base, 120deg);
      }
      &:nth-child(5) {
        background-color: adjust-hue($bg-base, 180deg);
      }
      &:nth-child(6) {
        background-color: adjust-hue($bg-base, 240deg);
      }
      &:nth-child(7) {
        background-color: adjust-hue($bg-base, 300deg);
      }
    }

How to set background-colorfor each row via SCSS function? Thanks in advance.
Codepen: https://codepen.io/sasha_jarvi/pen/XwOywo

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey delphinpro, 2019-06-03
@sasha_jarvi

https://codepen.io/anon/pen/PvLweE?editors=0100

S
sasha_jarvi, 2019-06-03
@sasha_jarvi

@for $i from 1 through 7{
  tr{
    &:nth-child(#{$i}) {
      @if($i <= 2) {
        background-color: adjust-hue($bg-base, (30 * ($i - 1)));
      } @else {
        background-color: adjust-hue($bg-base, (60 * ($i - 2)));
      }
    }
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question