BEM in CSS: Block, Element, Modifier Naming Explained
Contents
BEM (Block, Element, Modifier) is a naming convention for CSS classes. Every class name says what it styles and where it belongs, so styles don’t leak between components and you can tell from the HTML alone which CSS applies.
The pattern is block__element--modifier:
- Block: a standalone component, like
card,menuorsearch-form. - Element: a part of a block, joined with two underscores:
card__title. - Modifier: a variation or state, joined with two hyphens:
card--featured,card__button--disabled.
Demo:
Both cards use the same block and elements. The second adds a block modifier (card--featured) and an element modifier (card__button--ghost). Everything else is shared.
The rules
Blocks
A block is a reusable, independent piece of the interface. Name it after what it is, not what it looks like: alert, not red-box. Blocks can be nested inside other blocks, but they never depend on them.
<form class="search-form">...</form>
<nav class="menu">...</nav>
Elements
An element is a part of a block that has no meaning on its own. It’s always written as block__element, and that’s the only selector you need: no nesting like .card .title.
<form class="search-form">
<input class="search-form__input" type="search">
<button class="search-form__button">Search</button>
</form>
Modifiers
A modifier changes the look, state or behavior of a block or element. A modified element keeps its base class too, so the modifier only has to contain the differences. Boolean modifiers are one word (--disabled); key-value modifiers use a second word (--size-large).
<button class="button button--primary button--size-large">Buy</button>
<li class="menu__item menu__item--active">Home</li>
Common mistakes
Don't chain elements: card__header__title
BEM names don’t mirror the DOM tree. Every element belongs directly to the block, however deep it sits in the HTML. Long chains break the moment you move something.
<!-- Avoid -->
<div class="card__header__title"></div>
<!-- Do -->
<div class="card">
<div class="card__header">
<h3 class="card__title"></h3>
</div>
</div>
Don't use a modifier on its own
A modifier only describes the difference, so it needs its base class next to it.
<!-- Avoid: loses all the base .button styles -->
<button class="button--primary">Buy</button>
<!-- Do -->
<button class="button button--primary">Buy</button>
Keep specificity flat
BEM works because almost every selector is a single class, so every rule has the same specificity and the order of your CSS decides what wins. Here is how CSS class selectors and their specificity work. Avoid IDs, tag selectors and !important (here’s why). The one accepted exception is styling an element inside a modified block, like .card--featured .card__text in the demo.
BEM with Sass
Sass’s & parent selector makes BEM shorter to write, and still compiles to flat, single-class selectors.
.card {
padding: 20px;
&__title {
font-size: 18px;
}
&--featured {
background: #1e3a8a;
}
}
/* Compiles to: .card {} .card__title {} .card--featured {} */
BEM vs Tailwind vs CSS Modules
| BEM | Utility CSS (Tailwind) | CSS Modules / scoped CSS | |
|---|---|---|---|
| Avoids style conflicts by | Naming discipline | Not writing component CSS | Generating unique class names |
| Needs a build step | No | Yes | Yes |
| HTML readability | Descriptive class names | Long class lists | Short names |
| Good fit | Plain CSS, WordPress themes, CMS templates | Apps with a component framework | React, Vue and Svelte components |
They also mix well: many teams use utilities for spacing and layout, and BEM-named classes for components that are reused across templates. Cascade layers and native CSS nesting have made plain CSS easier to organize too, but BEM is still the most widely understood convention.
FAQ
What does BEM stand for?
Block, Element, Modifier. It was created at Yandex and is now one of the most common CSS naming conventions.
Why two underscores and two hyphens?
So block names can contain single hyphens (search-form) without ambiguity. search-form__button--disabled is always readable as block, element and modifier.
Is BEM still relevant?
Yes, especially for plain CSS and server-rendered sites. With component frameworks, scoped styles or utility classes solve the same problem, so BEM is less necessary there.
Can an element have its own elements?
Not in the name. Every element is block__element. If a part needs its own elements, it’s probably a block of its own.
Related: CSS !important, comments in CSS, CSS tips you won’t see in most tutorials and the CSS tutorial.