BEM is a methodology for naming and classifying CSS selectors in a way to make them a lot more strict, transparent and informative.
The naming convention follows this pattern:
.block{}
.block__element{}
.block--modifier{}
.block
represents the higher level of an abstraction or component..block__element
represents a descendent of.block
that helps form.block
as a whole..block--modifier
represents a different state or version of.block
.
If you are familiar with OOCSS then you will no doubt be familiar with the media object. In BEM form, the media object would now read:
.media{}
.media__img{}
.media__img--rev{}
.media__body{}
From the way this CSS is written we can already glean that .media__img
and .media__body
must live inside .media
and that .media__img--rev
is a slight variation on .media__img
. All that information gathered from CSS selectors alone!
This is a powerful feature. This is what inuit.css is about.
BEM would also enforce stricter use of classes. A lot of times (and I have seen it) developers will use a class in their HTML because the styling applied to that class simply happens to work in a certain context. It shouldn’t be used in that context, but by pure coincidence it just works. This means that—if the styles on the class they used ever change—it may have negative ramifications in their implementation that they might not be able to explain or understand. By namespacing things (e.g. .media__img
rather than .img
) then developers are a lot less likely to use classes in the wrong place. BEM will enforce much stricter limitations.
Another benefit I can see is the combatting of the following situation. If we take the media object again:
<div class=media>
<img src=logo.png alt="Foo Corp logo" class=img-rev>
<div class=body>
<h3 class=alpha>Welcome to Foo Corp</h3>
<p class=lede>Foo Corp is the best, seriously!</p>
</div>
</div>
How are the classes .media
and .alpha
related to each other? Are they? What about .body
and .lede
or .img-rev
and .media
? From that HTML (unless we’re very familiar with the media object) we have no idea what makes up that component and what else is optional. If we were to rework it with BEM:
<div class=media>
<img src=logo.png alt="Foo Corp logo" class=media__img--rev>
<div class=media__body>
<h3 class=alpha>Welcome to Foo Corp</h3>
<p class=lede>Foo Corp is the best, seriously!</p>
</div>
</div>
We can now instantly see that .media
is the block, .media__img--rev
is an element of .media
that has a modifier applied and .media__body
is an unmodified element of .media
. All through the names of their classes. That is incredibly useful.
BEM may look a little funny, and it might require more typing (most text editors have autocomplete, and gzip will negate any differences in filesize) but it is so powerful, and inuit.css is all about power…
inuit.css is a powerful framework. It’s not a framework that’ll give you some nice glossy buttons; it’s a framework that will allow you to scale a 1,000+ page website, it’s an object oriented framework designed for use on big projects with big teams. One of the things I spoke about most recently was handling CSS in larger teams and I believe BEM is a great step in that direction.
BEM allows us to namespace our styles for stricter and appropriate implementations, it allows us to work out HTML’s relationship to itself (or if there is no relationship at all), it allows us to see what role a piece of HTML plays in a wider picture. inuit.css was designed to be scalable and used on larger projects with larger teams and I believe, although unsightly at first, BEM will massively help that.
I think the odd looking syntax (and my open hatred of underscores) is a fair price to pay for the value BEM notation would give us… Thoughts?
I'm not entirely keen on the BEM underscore style of namespacing, but have found the namespacing itself invaluable.
I've modified my media block so as to not assume that the first child of the block is an img - digging through Podio's source and seeing how much and where they use the media object opened my eyes to just how crazy the media block is, and how naming the first child as an image of some sort doesn't do it justice.
I've opted for the following naming scheme for my media blocks:
where
.mblock-alpha
represents the traditional.img
element. Makes me feel at ease when not using an image (:P), as well shows to who the element belongs, as mentioned.I generally reserve -alpha -beta etc. namespacing for my modifier classes (as in your post on using rems to maintain vertical rhythm), so I may revisit this particular block at a later stage.
I'm even developing a styleguide with a Sass structure abstracting core concepts in my files beyond that of those outlined in BEM. My Sass is split into variables, utilities, and partials (where BEMs will actually fall under).
Variables are only to be declared under my var folder, css mixins and functions are only to be declared under my utl folder, and all blocks will then go under my par folder where they will eventually be abstracted into block, modifier, and media query sections.
Many of the modifiers I intend on adding to blocks will be namespaced according to the variables they reference. It's made modifying blocks with colours completely trivial, and that's just the beginning!
Great to see BEM and OOCSS gaining exposure!