Skip to content

Instantly share code, notes, and snippets.

@csswizardry
Created October 2, 2012 20:09
Show Gist options
  • Save csswizardry/3822990 to your computer and use it in GitHub Desktop.
Save csswizardry/3822990 to your computer and use it in GitHub Desktop.
Thoughts on BEM for inuit.css

Bringing BEM to inuit.css

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?

Comment on this post on GitHub.

@matyus
Copy link

matyus commented Oct 18, 2012

I have devised a similar technique for things like the media object abstraction, with one small exception: I make a point of not using one-word classnames anywhere in my codebase. It's really tough to gather context from a single word as generic as '.media', same goes with things like '.nav'. I've found that it's crucial to pair it with some sort of context, nav_list, nav_sublist, etc.

So, .media doesn't fly.

.media {}
.media-image{}
.media-body{}

Instead, I've been using the suffix _module for setups like this. It helps denote that the parent (media_module) will have children somewhere that relate directly to this abstract system and aren't a single instance:

.media_module {}
.media_image {}
.media_body {}

I haven't completely adopted the BEM syntax, but I agree that the concepts behind BEM (that drive the syntax) are definitely worthwhile.

@varya
Copy link

varya commented Oct 18, 2012

@necolas, Just to clarify, BEM does not require any specific convention. The one described in the Smashing Magazine article was just an example of BEM-based naming convention what a particular team uses. Another team is free to use different naming. But if they have blocks, elements and modifiers, this is also BEM.

@YemSalat
Copy link

You guys suck..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment