What is BEM in CSS?
BeginnerCSS interview questionBy UIRound, updated 17 November 2026
Short answer
BEM (Block, Element, Modifier) is a naming convention for CSS classes. A block is a standalone component (.card), an element is a part of it (.card__title), and a modifier is a variant or state (.card--featured). Every rule targets one class, so specificity stays flat, names never clash across components, and you can tell from the HTML which styles apply.
The three parts
| Part | What it is | Syntax | Example |
|---|---|---|---|
| Block | A component that makes sense on its own | block |
.search-form |
| Element | A part that only has meaning inside its block | block__element |
.search-form__input |
| Modifier | A variant, size or state of a block or element | block--modifier, block__element--modifier |
.search-form--compact, .search-form__button--disabled |
Words inside a name are joined with single hyphens (search-form), elements with two underscores, modifiers with two hyphens. The original BEM methodology used single underscores for modifiers (block_mod); the double-hyphen style is the variant most teams use today. Pick one and be consistent.
An example
<article class="card card--featured">
<img class="card__image" src="/talk.jpg" alt="" />
<h3 class="card__title">Designing for slow networks</h3>
<p class="card__meta">12 min read</p>
<a class="card__link card__link--primary" href="/talks/slow-networks">Watch</a>
</article>.card { display: grid; gap: 0.75rem; padding: 1rem; border: 1px solid #ddd; }
.card--featured { border-color: gold; }
.card__image { width: 100%; aspect-ratio: 16 / 9; object-fit: cover; }
.card__title { font-size: 1.25rem; margin: 0; }
.card__meta { color: #666; }
.card__link { font-weight: 600; }
.card__link--primary { color: white; background: #0057b8; padding: 0.5rem 1rem; }Two rules make this work. A modifier is added alongside the base class (card card--featured), never instead of it, so the modifier only holds the differences. And every selector is a single class, with no nesting, no tag names and no IDs.
Why teams use it
Flat specificity. Every rule is one class, specificity (0,1,0). Which rule wins comes down to source order, which you control. You never need .sidebar .card h3 to beat .card h3, and you never reach for !important. If specificity still feels fuzzy, How does CSS specificity work? covers the rules BEM is designed to sidestep.
No accidental leaks. .title in one file can collide with .title in another. .card__title cannot, because the block name acts as a namespace.
Readable markup. card__link--primary tells you which component a node belongs to and which variant it is, without opening the stylesheet. That also makes unused CSS easier to find and delete.
Independence from HTML structure. .card__title does not care whether it is an h3 or a p, or how deep it is nested. You can change markup without breaking selectors.
Rules that trip people up
- Do not chain elements.
.card__body__titlemirrors the DOM tree and breaks when you move things. An element belongs to the block, so it is.card__titlehowever deep it sits. - Blocks can nest; their names do not. A button inside a card is its own block,
.button, not.card__button, unless it only makes sense inside the card. - Mixes are allowed. One node can be a block and an element of its parent:
class="button card__action". Thebuttonblock gives it its look;card__actionplaces it in the card (margins, alignment). - States. Many teams use BEM modifiers for permanent variants and separate state classes for things toggled by JavaScript, such as
.is-open, or ARIA attributes like[aria-expanded="true"]. Both styles are common.
BEM vs CSS Modules vs utility classes
| BEM | CSS Modules | Utility classes (Tailwind) | |
|---|---|---|---|
| How clashes are avoided | Convention: the block name is a prefix | Build tool hashes class names per file | Every class does one thing; nothing to clash |
| Enforced by | Code review | The bundler | The framework |
| Specificity | Flat, one class | Flat, one class | Flat, one class |
| Where styles live | Global stylesheet | A file next to the component | In the markup |
| Works without a build step | Yes | No | Needs the Tailwind build |
CSS Modules give you BEM's main benefit automatically: styles.title in Card.module.css becomes something like Card_title__x7f2a, unique per file. Many teams therefore drop BEM when they adopt CSS Modules, and keep plain names like .title and .featured. BEM still earns its place in server-rendered sites, design systems shipped as plain CSS, WordPress or other CMS themes, and anywhere a build step cannot rewrite class names. The broader choice is covered in CSS Modules vs CSS-in-JS vs Tailwind, and Are CSS preprocessors like Sass still worth using? explains why the Sass &__title habit many BEM codebases rely on does not work with native CSS nesting.
Practise it
Restyle a card list with CSS only shows what happens when a stylesheet does not have flat specificity, and Predict which color wins drills the cascade rules BEM avoids. Implement cva() for component variants is the modifier idea in JavaScript, and Build a CSS-in-JS runtime shows how generated class names solve the same naming problem.
Common follow-up questions
Is BEM still relevant?
Yes, though less central than it was. Scoped styling (CSS Modules, Vue <style scoped>, Angular's view encapsulation) and utility-first CSS solve naming collisions with tooling. BEM remains the simplest answer when CSS is global and you cannot rely on a build step, and its ideas (one class per rule, components as namespaces) carry over to every approach.
Why are BEM class names so long?
Because the name carries the namespace. .card__link--primary is long, but it replaces a descendant selector like .card a.primary that is shorter to type and harder to override. With Sass, you write &__link and &--primary inside .card { } and the compiled output is still flat. Native CSS nesting cannot join names like that, so without Sass you write the full class names.
How does BEM handle a modifier that changes an element?
Either put a modifier on the element (.card__title--large) or scope from the block's modifier: .card--featured .card__title { ... }. The second raises specificity to two classes, which is an accepted exception when a block variant changes several of its elements.
How does BEM relate to cascade layers?
They solve different problems. BEM keeps specificity equal within your component styles; @layer controls which group of styles wins (reset, base, components, utilities) regardless of specificity. Using both is common.
Mistakes to avoid
Chaining elements, such as
.menu__item__link, so names mirror the DOMUsing a modifier on its own (
class="card--featured") without the base classStyling elements by tag inside a block (
.card h3), which brings back specificity fightsMaking everything an element of the page block, so nothing is reusable
Mixing naming styles (
card_title,card-title,card__title) in one codebase
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Restyle a card list with CSS only, without editing the HTML or the existing stylesheetEasy30 minFree
- Predict which color wins: specificity,
!importantand cascade layersEasy20 minFree - Implement
cva()(class-variance-authority) for component variantsMedium30 min - Build a CSS-in-JS runtime: hashed class names,
insertRule, deduplication and SSR style extractionHard60 min