BEM Overview

BEM (Block-Element-Modifier) is a CSS naming convention developed by the team at Yandex to improve scalability and maintainability in web development.

Put simply, the idea of BEM is to “divide the user interface into independent blocks” by naming CSS classes in the following methodology:

1. Block: an independent component that can be reused (e.g. with class name .nav )

2. Element: a child within a block that cannot be used separately from that block (e.g. with class name .nav__item )

3. Modifier: a variation in the style of either a block or modifier (e.g. with class name .nav — dark )

Let’s dive into some real CSS examples to get a hang of this thing.

Blocks

Blocks are reusable components. Like buttons, cards or form fields.

When naming your blocks, focus on describing its purpose (i.e. what it is) rather than its state (i.e. what it looks like).

For example, .btn or .nav follows the correct naming convention for a block.

.big or .bright-pink describes how it looks, so it doesn’t scale well when you want to change the design later on.

If you’re wondering how to place blocks within blocks (for example, a button inside a nav), here’s a short article to help you with that.

Elements

Inside blocks are where elements live. Elements are dependent on their parent block, and so cannot be used without them.

Elements also have a unique CSS class naming convention which works like this:

.block__element

For example, using the .card component, an element inside the card component (like an image) would have a class name like .card__img .

The element name always appends the block name, separated by a double underscore __ .

Note how the 1st code snippet uses more than 1 selector to target the styles (e.g. .card img {} ).

It’s considered best practice to use a BEM element class and use that directly instead (e.g. .card__img {} ) as this has a lower specificity.

Following this approach reduces the chance of cascade issues down the line.

Modifiers

When you have varying styles in blocks (or elements), that’s where modifiers come in.

For example, your ‘card’ block might have a light and dark version. Or you might have primary and secondary buttons.

Modifiers have a unique CSS naming convention which works like this:

block — modifier or block__element — modifier .

That’s right- BEM modifiers can be applied to both blocks and elements.

Let’s dive into some bad and good practices:

It’s considered bad practice to use a modifier class in isolation (i.e. without the block or element class).

That’s because the modifier is meant to add incremental style changes to the block.

Therefore, whenever using a modifier, ensure it’s used with the base class:

And that’s it!

Those are the fundamentals to get you off and running with BEM.

If you’re interested to learn more about the ‘why’ behind BEM, I recommend checking out this CSS Tricks article.

Like learning anything new, practicing is key. Give BEM a shot in your next project and see where it takes you!