Search code examples
htmlcsssassbem

BEM syntax in SASS


I have a simple HTML and CSS with BEM. I want style for class .block__item-header-mark inside class .block__item-header--has-round. I use CSS .block__item-header--has-round .block__item-header-mark { /styling here/ }. But I don't think this is good syntax.

My question is:

  1. How to call .block__item-header-mark inside .block__item-header--has-round with better syntax in my SCSS code ?
  2. My BEM syntax is good ?

Code

.block {
  &__item {
    &-header {
      &--has-round {
        /* How to call .block__item-header-mark with better syntax ??? */
        .block__item-header-mark {
          /*overide style*/
        }
      }
      &-mark {
        /*normal style*/
      }
    }
  }
}
<div class="block">
  <div class="block__item">
    <div class="block__item-header block__item-header--has-round"><span class="block__item-header-mark"></span></div>
    <div class="block__item-body"></div>
  </div>
</div>


Solution

  • You can create a variable to refer to the scope you want

    .block {
      &__item {
        &-header {
          $header: &;
          &--has-round {
            #{ header }-mark {
              /* override style */
            }
          }
          &-mark {
            /*normal style*/
          }
        }
      }
    }