Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is CSS !important ok into a BEM modifier?

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?

like image 621
Kamafeather Avatar asked Sep 22 '15 10:09

Kamafeather


People also ask

Should you use BEM CSS?

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.

Is BEM a CSS preprocessor?

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.

What are the benefits of using the BEM convention in HTML and CSS?

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 .

How do you name a CSS class in BEM?

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.


Video Answer


1 Answers

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.

like image 152
jabacchetta Avatar answered Sep 30 '22 14:09

jabacchetta