Multiple classes on same parent element not working at the same time: BEM

56 views Asked by At

I have two classes on the header: .header-container & a theme class, for example solid-green or solid-blue.

The current markup works correctly in terms of applying the relevant theme styles, but I want to use BEM, so the all of the CSS should be wrapped in the header-container class:

.header-container {
  // all component styles
}

.top-banner {
  height: 70px;
}

.main-banner {
  height: 140px;
}

.solid-green {
  .top-banner {
    background-color: green;
  }
  
  .main-banner {
    background-color: lightgreen;
  }
}

.solid-blue {
  .top-banner {
    background-color: blue;
  }
  
  .main-banner {
    background-color: lightblue;
  }
}
<header class="header-container solid-green">
  <div class="top-banner">Top banner</div>
  <div class="main-banner">Main banner</div>
</header>

However, when I wrap the CSS with the header-container class, the theme class no longer work. Can anyone advise where I'm going wrong?

1

There are 1 answers

0
cts On

.header-container {
  .top-banner {
    height: 70px;
  }
  
  .main-banner {
    height: 140px;
  }

  &.solid-green {
    .top-banner {
      background-color: green;
    }
    
    .main-banner {
      background-color: lightgreen;
    }
  }
  
  &.solid-blue {
    .top-banner {
      background-color: blue;
    }
    
    .main-banner {
      background-color: lightblue;
    }
  }
}