E
E
Evtera2021-01-22 23:09:00
Sass
Evtera, 2021-01-22 23:09:00

How to correctly write nesting in scss?

Hello. There is a record like

select {

  /* when select open add class OPEN */
  &.open {
    z-index: 2;
    .select__dropdown {
      display: block;
      background-color: white;
    }

    .select__label {
      border-bottom: none;
    }

    .select__arrow {
      transition: 0.3s ease;
      transform: rotate(180deg);
    }
  }  
}


What is the proper way to replace .select__ prefixes using nesting and the & sign in this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
inkShio, 2021-01-23
@Evtera

you can make a variable and just substitute it

$self: "select";

.#{$self} {
  &.open {
    z-index: 2;
    .#{$self}__dropdown {
      display: block;
      background-color: white;
    }

    .#{$self}__label {
      border-bottom: none;
    }

    .#{$self}__arrow {
      transition: 0.3s ease;
      transform: rotate(180deg);
    }
  }
}

Or here's another option for you
.select {
  &.open {
    z-index: 2;
  }

  &.open &__dropdown {
    display: block;
    background-color: white;
  }

  &.open &__label {
    border-bottom: none;
  }

  &.open &__arrow {
    transition: 0.3s ease;
    transform: rotate(180deg);
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question