0

While i was trying BEM and SCSS. Using SCSS to select modifier and Elements is easy but the hard part is while using modifier and referencing element that doesn't have modifier class is breaking the neat approach i made. Here is the quick example what i am trying to do.

This is the HTML

<div class="icon-box icon-box--red">
   <h3 class="icon-box__title>This is title</h3>
</div>

And the SCSS

.icon-box {
  &__title {
    color: black;
  }

  &--red {
    &__title {
      color: red;
    }
  }
}

And the output css is

.icon-box {
   background: white;
}
.icon-box__title {
  color: black;
}
.icon-box--red {
  background: black;
}
.icon-box--red__title {
  color: red;
}

The compiled CSS is correct but i want to achieve this

.icon-box {
   background: white;
}
.icon-box__title {
  color: black;
}
.icon-box--red {
  background: black;
}
.icon-box--red .icon-box__title {
  color: red;
}

While i can reference it using name .icon-box__title but i want to go back to grand parent. Is there any approach i can use?

Thank you

2 Answers2

1

Using the ampersand only looks at that paticular styles parent hence why your SASS is compiling like that.

Here are two options for you:

    .icon-box {

        &__title {

            color: black;

        }

        &--red {

            color: red;

        }

        &__title {

            color: red;

        }
    }


   .icon-box {

        &__title {

            color: black;

        }

        &--red, &__title  {

            color: red;

        }
    }
fish
  • 56
  • 1
  • 4
-1

Finally, i thought a lot and found a way around not sure why there is not a way around to select grandparent on SCSS.

Here is how i am doing that

.icon-box {
  &__title {
    color: black;
  }

  &--red {
    .icon-box {
      &__title {
        color: red;
     }
  }
}  

Hope someone will find it usefull

  • This is very nearly the same as the above answer. Could you either accept the answer above as the correct one or select yours as the correct one. – Josh Bolton Dec 15 '16 at 11:42