Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion with BEM modifiers

Tags:

css

bem

I have started using BEM methodology while writing my CSS and there have been few occasions where I have struggled to find out the best way to do a particular thing.

I would like to take up a simple example of a panel here.

Lets say I am writing a panel component CSS using BEM style. So my CSS might look as follows:

.panel {}

.panel__titlebar {}

.panel__content { display: none; }

A panel can be either chromeless or with chrome. So I define another modifier class for the panel:

.panel--with-chrome {
 border: 4px solid black;
 border-radius: 4px;
}

Now lets say, the panel can be in a fullscreen/maximized state also in which the chrome and titlebar disappear. Instead of defining modifiers for both panel and titlebar, it would be be wise to define the modifier just on parent (say panel--fullscreen) and rest elements shall change accordingly. So now my CSS becomes:

.panel--fullscreen {
 /* something has to be done here */
}

.panel--fullscreen .panel__titlebar { display: none; }

To remove the chrome in fullscreen mode, I can either:

  1. toggle the panel--with-chrome class in JS along with the panel--fullscreen class

  2. overwrite the chrome CSS inside the panel--fullscreen class.

First isn't good because ideally I would like to simply toggle just one class (.panel--fullscreen) in JS to toggle fullscreen mode.

And second one is bad because I'll have to overwrite previous CSS which is a bad practice.

So whats the best way to go about it? Appreciate your comments.

Thanks

like image 697
Kushagra Gour Avatar asked Dec 28 '12 18:12

Kushagra Gour


People also ask

Is BEM obsolete?

In CDD (component driven development) BEM might be considered obsolete, but a naming convention is still recommended. This counts for any language, including CSS.

Should we use BEM?

Benefits of using BEMBecause of its unique naming scheme, we won't run into conflicts with other CSS names. BEM also provides a relationship between CSS and HTML. Ambiguous names are hard to maintain in the future⁣. Overall, BEM is my favourite CSS naming scheme, and I strongly suggest you try using it too!

What is a modifier in BEM?

A modifier is a flag that changes how a block or an element looks or behaves. For example: The two buttons are the same block, but they look different. The power BEM gives us lets us use the same block twice and still have them look very different.

What is the BEM principle?

What is BEM? BEM is a front-end naming method for organizing and naming CSS classes. The Block, Element, Modifier methodology is a popular naming convention for class names in HTML and CSS. It helps to write clean CSS by following some simple rules.


Video Answer


2 Answers

The answer depends on many things.

First, how much logic and appearance have "panel--with-chrome" and "panel--fullscreen" modifiers. And also on what kind this logic is.

If "panel--with-chrome" brings a lot of CSS properties and special JS functionality, I would toggle it in JavaScript when applying "panel--fullscreen".
It also depends on a JavaScript framework you use. In "i-bem.js" which we use at Yandex it's easy to react to appending a modifier:

  • A square changes size modifier when after a click
  • Reacting on applying a modifier

But if the framework you use doesn't allow to express such a reaction handy, this answer won't work that great for you.

In the other case, when "panel--with-chrome" has not very much properties and doesn't bring any JavaScript logic to a page, I would redefine those CSS properties in "panel--fullscreen" class.

To sum up, there is no universal solution and strict rules to follow. You should decide yourself what will be useful in your case. The decision should depend on many things:

  • if you expect your project to be maintained in the future, which solution will be easier to support?
  • capabilities of the JavaScript framework you use
  • performance stuff
    Not in this particular case, but sometimes we measure speed of rendering for variants we are choosing from.
  • opinion of the other guys, if you work in team
  • file structure of your project
    We, here at Yandex, store CSS and JavaScript for a block in the same block folder. So, it is not a problem to share logic between CSS and JavaScript since they all are in one place.
    But if you keep your JavaScript files separately, this can influence on how comfortable it is to support shared logic.

And so on...

like image 98
Varvara Stepanova Avatar answered Sep 27 '22 17:09

Varvara Stepanova


I’d go with the first option; you are toggling state after all, so you need to add/remove/toggle classes accordingly. It’s better than undoing a load of stuff in CSS IMO.

like image 33
csswizardry Avatar answered Sep 27 '22 17:09

csswizardry