Skip to main content

BEM Architecture & Naming Conventions

As CSS codebases grow, maintaining predictability and preventing specificity conflicts become major engineering challenges. BEM (Block, Element, Modifier) is a battle-tested naming convention created to keep component CSS modular, flat, and scalable.

1. BEM Core Concepts​

BEM decomposes user interfaces into three distinct entities:


β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ BLOCK β”‚
β”‚ .card β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ ELEMENT β”‚ β”‚
β”‚ β”‚ .card__title β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ MODIFIER β”‚ β”‚
β”‚ β”‚ .card--featured β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

  • Block (.block): A standalone, reusable component entity (e.g., card, nav, button).
  • Element (.block__element): A dependent sub-part of a block that has no standalone meaning outside of it, delineated by double underscores __ (e.g., card__title, nav__item).
  • Modifier (.block--modifier or .block__element--modifier): A flag that alters the visual appearance, state, or behavior of a block or element, delineated by double hyphens -- (e.g., card--featured, button--large).

2. Specificity and the Flat Structure Advantage​

Without BEM, developers often rely on deep structural nesting, leading to fragile specificity wars:

/* ❌ Anti-Pattern: High Specificity & Fragile HTML Coupling */
div.sidebar ul.menu > li.item a {
color: #2563eb;
}

/* βœ… BEM Solution: Flat Single-Class Specificity (0,0,1,0) */
.menu__link {
color: #2563eb;
}

.menu__link--active {
color: #059669;
}

3. BEM Best Practices & Common Pitfalls​

  1. Avoid Multi-Level Element Nesting: Avoid syntax like .card__header__title. Keep element names shallow relative to the block (e.g., .card__title).
  2. Combine Block and Modifier Classes: Never use a modifier class in isolation. Always apply the base block/element class alongside its modifier (class="btn btn--primary").
  3. Use Utility Classes Sparingly: Keep block styling encapsulated within the BEM scope to maintain predictability across teams.

Interactive Playground: BEM Notification Card​

Observe how BEM classes structure blocks, elements, and modifier states cleanly without specificity escalation:

Loading Interactive Editor...

Summary Reference Table​

EntitySyntax PatternExample ClassPurpose
Block.block.btnStandalone UI block container
Element.block__element.btn__iconDependent sub-part of block
Modifier.block--modifier.btn--successVariant style or state flag