Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BEM CSS Inheritance?

Tags:

css

bem

I am working on a large scale website. We are considering to write CSS in BEM style, but not sure if this is actually a good practice. CSS is all about inheritance, but I don't see that in BEM. For example:

.toolbar {
  &-btn {}  
}

VS

.toolbar {
  .btn {}
}

The .btn in 2nd example can inherit from global .btn class, but .toolbar-btn class will not inherit those default properties. So you end up writing a lot repeatitive properties. Why people say BEM is reusable at all if it doesn't inherit properties? Edit: We are trying to avoid using extend feature as much as possible.

like image 780
user2734550 Avatar asked Sep 16 '26 16:09

user2734550


1 Answers

If you need to define .btn as a global class, you should use it as a modifier (for variations)


 .btn.btn--primary

 // OR
 .my-cool-btn.btn--primary.btn--red

Here is the Google's approach for the new "Material Design Lite" framework


 .mdl-layout__container  //Parent element

 --- .mdl-layout  //Block

 ------ .mdl-layout__header   //element

 ------ .mdl-layout__content  //element

And some modifiers on the .mdl-layout__header element


// Local modifiers
.mdl-layout__header-row
.mdl-layout__header--scroll


// Global modifiers
.demo-header 
.is-casting-shadow
.mdl-color--grey-100
.mdl-color-text--grey-800

I highly suggest you take a look at their repository and this template example

like image 79
Monfa.red Avatar answered Sep 20 '26 23:09

Monfa.red



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!