Given an element with some styling and an additional class (mywidget_button--disabled) that works as BEM modifier, does it has sense, as practice, to use the !important clause?
.mywidget__default ~ .mywidget__button {
border: 1px solid #000;
}
.mywidget__button--disabled {
border: 1px solid transparent !important;
}
The first class is more specific and wins on the second one, but being the disabling class a modifier that (theoretically) should have more priority than the "common" styling, is it correct to rely on the !important clause?
Or does it make the code prone to spaghetti-headache?
Why should you use BEM? CSS is a language that's easy to learn but very hard to maintain. As the project grows larger, without proper structure, maintaining CSS is unbearable, hence we use the BEM methodology to make CSS maintainable.
BEM stands for Block Element Modifier, and it is a way of naming your elements so that they are easier to understand and manage. BEM CSS is a great way to keep your css files organized and easy to read. It is recommended to use it with a CSS preprocessor like Sass. But, it can also be used without a preprocessor.
The idea behind BEM is for developers to write HTML and CSS in a way that avoids inheritance issues. As we all know, CSS in bigger projects can become really messy really quickly when not managed and structured correctly. BEM helps with this. So we have an element, the block component with the class .
BEM names intentionally use double underscores and double hyphens instead of single to separate Block-Element-Modifier. The reason is so that single hyphens can be used as word separators. Class names should be very readable, so abbreviation isn't always desirable unless the abbreviations are universally recognizable.
I personally prefer using BEM with namespaces, and then chaining the temporary states to other classes, which, in turn, increases the specificity.
Harry Roberts from CSS Wizardry has some great resources that cover these more-complicated situations. Namespaces, theming, and general guidelines.
Without seeing your specific project, it's hard to tell what might be the best approach, but either a chained state class, or a utility class — which does allow for !important — should work well. That is, of course, if you choose to take BEM up a notch with these additional techniques.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With