Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a theme when writing CSS using BEM naming methodology?

Tags:

html

css

bem

I use the BEM CSS naming convention on a project i'm working on. Everything peachy so far. However i need to implement several themes (meaning slighly different designs) on the same html page. Writing this:

.name-of-theme .block1 {
    ...
}

.name-of-theme .block2 {
    ...
}

wouldn't be BEM but would allow to put a single modifier in eg the body-tag.

Writing this:

.block1--name-of-theme {
   ...
}

.block1--name-of-theme {
   ...
}

would be BEM but would also mean writing a lot of redundant code in HTML as every affected block would have to be modified with an additional CSS class. This would also be hard to maintain.

Is there a BEM compatible way of solving this without writing redundant code?

like image 804
T. Jans Avatar asked Oct 29 '25 09:10

T. Jans


1 Answers

Let me cite a piece of FAQ on bem.info:

nesting is appropriate for changing elements depending on the state of a block or its assigned theme

like image 101
tadatuta Avatar answered Oct 31 '25 00:10

tadatuta



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!