0

I want a nice elegant way in SCSS to write BEM subcomponents without being verbose, and modify them using the parent class.

.container.container--red .container__title {
}

I tried this, but it didn't work:

.container {
   &.container--red {
      &__title {
      }
   }  
}

But this doesn't work because it assumes the selector I need is .container--red__title

David Ball
  • 553
  • 9
  • 19

2 Answers2

1

scss:

.container {
    $root: &;

    &--red {
        #{$root}__title {
            color: red;
        }
    }  
}

output:

.container--red .container__title {
    color: red;
}
mwl
  • 1,448
  • 14
  • 18
0

Isn't this what you would want to use?

.container { &--red {} &__title {} }

Compiling to

.container {} .container--red {} .container__title {}

Don't forget that hierarchy still counts in sass because you need to think about the compilation output.

KINKWILDE
  • 136
  • 1
  • 15