---
comments: true
date: 2013-01-25 18:56:00
layout: post
slug: mindbemding-getting-your-head-round-bem-syntax
title: MindBEMding – getting your head ’round BEM syntax
categories:
- Web Development
tag:
- OOCSS
- BEM
- Front-end architecture
- CSS
- CSS selectors
meta: A primer on the oft-confusing BEM notation for CSS
branch-id: gAPbivPskEs
branch-url: http://branch.com/b/mindbemding-getting-your-head-round-bem-syntax
---
One of the questions I get asked most frequently is what do `--` and `__`
mean in your classes?
The answer is thanks to [BEM](http://bem.info) and
[Nicolas Gallagher](http://twitter.com/necolas)…
---
BEM – meaning block, element, modifier – is a front-end
naming methodology thought up by the guys at [Yandex](http://yandex.ru). It is a
smart way of naming your CSS classes to give them more transparency and meaning
to other developers. They are far more strict and informative, which makes the
BEM naming convention ideal for teams of developers on larger projects that
might last a while.
It is important to note that I use a naming scheme _based_ on BEM, but [honed by
Nicolas Gallagher](http://nicolasgallagher.com/about-html-semantics-front-end-architecture/).
The naming techniques covered in this post are not the original BEM ones, but
are improved versions that I much prefer. Whatever the actual notation used,
they are all based on the same BEM principles.
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`.
The reason for double rather than single hyphens and underscores is so that your
block itself can be hyphen delimited, for example:
.site-search {} /* Block */
.site-search__field {} /* Element */
.site-search--full {} /* Modifier */
The point of BEM is to tell other developers more about what a piece of markup
is doing from its name alone. By reading some HTML with some classes in, you can
see how – if at all – the chunks are related; something might just be a
component, something might be a child, or element, of that component, and
something might be a variation or modifier of that component. To use an
analogy/model, think how the following things and elements are related:
.person {}
.person__hand {}
.person--female {}
.person--female__hand {}
.person__hand--left {}
The top-level block is a ‘person’ which has elements, for example, ‘hand’. A
person also has variations, such as female, and that variation in turn has
elements. This again, but written in ‘normal’ CSS:
.person {}
.hand {}
.female {}
.female-hand {}
.left-hand {}
These all make sense, but are somewhat disconnected. Take `.female` for example;
female what? What about `.hand`; a hand of a clock? A hand in a game of cards?
By using BEM we can be more descriptive but also a lot more explicit; we tie
concrete links to other elements of our code through naming alone. Powerful
stuff.
{% include promo-case-studies.html %}
Taking the previous `.site-search` example again, with ‘regular’ naming:
These classes are fairly loose, and don’t tell us much. Even though we can work
it out, they’re very inexplicit. With BEM notation we would now have:
We can see that we have a block called `.site-search` which has an element which
lives inside it called `.site-search__field`. We can also see that there is a
variation of the `.site-search` called `.site-search--full`.
Let’s take another example…
If you are familiar with OOCSS then you will no doubt be familiar with
[the media object](http://stubbornella.org/content/2010/06/25/the-media-object-saves-hundreds-of-lines-of-code).
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 the names of our
CSS selectors alone!
Another benefit is the combatting of the following situation. If we take the
media object again:
Foo Corp is the best, seriously!
Foo Corp is the best, seriously!