CSS Best Practices

BEM Methodology

Block Element Modifier - a structured approach to CSS naming that creates scalable, maintainable code.

What is BEM in CSS?

BEM (Block, Element, Modifier) is a class naming convention that keeps CSS predictable. A block is a standalone component (.card), an element is a part of it joined with two underscores (.card__title), and a modifier is a variation joined with two dashes (.card--featured). Every selector is a single class, so specificity stays flat and low.

<article class="card card--featured">
  <h3 class="card__title">…</h3>
  <button class="card__button card__button--primary">…</button>
</article>

.card { }
.card__title { }
.card--featured { }

Complete BEM Example

Code Editor

<article class="card card--featured">
  <h3 class="card__title">BEM card</h3>
  <p class="card__text">Block, element and modifier classes.</p>
  <button class="card__button card__button--primary">Read more</button>
</article>

Preview

BEM Fundamentals

The BEM Formula

/* BEM Naming Structure */ .block { } /* Independent component */
.block__element { } /* Child of block */
.block--modifier { } /* Variation of block */
.block__element--modifier { } /* Variation of element */

/* Real Example */ .card { /* Block: main component */ }
.card__title { /* Element: part of card */ }
.card__image { /* Element: part of card */ }
.card--featured { /* Modifier: card variant */ }
.card__title--large { /* Modifier: title variant */ }

/* Multiple modifiers */ .button { }
.button--primary { }
.button--large { }
.button--primary.button--large { } /* Combined */

BEM uses double underscores (__) for elements and double dashes (--) for modifiers.

Block

/* Independent component */ .menu {
  display: flex;
  background: white;
  padding: 1rem;
}

.card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.button {
  padding: 0.5rem 1rem;
  border: none;
  cursor: pointer;
}

Standalone components that can exist anywhere

Element

/* Parts of the block */ .menu__item {
  padding: 0.5rem;
}

.menu__link {
  text-decoration: none;
  color: #333;
}

.card__header {
  padding: 1rem;
  border-bottom: 1px solid #eee;
}

.card__title {
  font-size: 1.25rem;
  margin: 0;
}

Child components that belong to a block

Modifier

/* Variations */ .menu--vertical {
  flex-direction: column;
}

.menu--dark {
  background: #333;
  color: white;
}

.card--featured {
  border: 2px solid gold;
}

.button--primary {
  background: blue;
  color: white;
}

Different states, themes, or sizes

Advanced BEM Patterns

Complex Component Structure

/* Product Card Component */ .product-card { /* Block */ }
.product-card--featured { /* Block modifier */ }
.product-card--sale { /* Block modifier */ }

.product-card__image { /* Element */ }
.product-card__image--zoomed { /* Element modifier */ }

.product-card__content { /* Element */ }
.product-card__title { /* Element */ }
.product-card__title--truncated { /* Element modifier */ }

.product-card__price { /* Element */ }
.product-card__price--discounted { /* Element modifier */ }

.product-card__actions { /* Element */ }
.product-card__button { /* Element */ }
.product-card__button--primary { /* Element modifier */ }

/* Multiple modifiers */ .product-card--featured.product-card--sale {
  /* Combination styles */
}

Build complex components with clear hierarchies and variations.

State Management

/* BEM with state classes */ .modal { display: none; }
.modal.is-open { display: block; }

.tab { /* inactive state */ }
.tab.is-active { /* active state */ }

.form__field { }
.form__field.has-error {
  border-color: red;
}

.button { }
.button.is-loading {
  opacity: 0.5;
  pointer-events: none;
}

/* Alternative: BEM-only approach */ .modal--hidden { display: none; }
.modal--visible { display: block; }

Combine BEM with state classes for dynamic interfaces

Nested Components

/* Avoid deep nesting */ /* ❌ Don't do this */ .card__content__title__text { }

/* ✅ Keep it flat */ .card__title { }
.card__title-text { }

/* Separate components */ .article { }
.article__header { }
.article__content { }

/* Button inside article */ .button { /* Independent block */ }
.button--small { }

/* Context-specific styling */ .article .button {
  margin-top: 1rem;
}

/* Or use modifier */ .button--article {
  margin-top: 1rem;
}

Keep components independent and avoid deep element nesting

BEM Best Practices

Do's

  • • Use meaningful, descriptive block names
  • • Keep element names simple and clear
  • • Use modifiers for variations, not new components
  • • Maintain flat element structure (no deep nesting)
  • • Combine with utility classes when appropriate
  • • Document your component patterns
  • • Use consistent naming across your team
  • • Test components in isolation
❌

Don'ts

  • • Don't create elements of elements (.block__elem__elem)
  • • Don't use generic names (div, content, item)
  • • Don't rely solely on HTML structure
  • • Don't create modifiers that change the element's purpose
  • • Don't nest blocks inside elements unnecessarily
  • • Don't use BEM for simple utility classes
  • • Don't mix BEM with other naming conventions randomly
  • • Don't overcomplicate simple components

CSS BEM — step by step

1 / 4
1

Block — Independent Component

A Block is a standalone component: .card, .header, .form. It has meaning on its own and can be reused anywhere on the page.

1 of 4
🧠Block__Element--Modifier
The pattern: Block is the component, Element is a part of it (double underscore), Modifier is a variant (double dash). Example: .nav__link--active means the active variant of the link element inside a nav block.

🎯 Quick Check

In BEM, how do you name an error state for an input inside a form block?

BEM Methodology: summary

Key points

  • Don't nest element names: write .card__title, not .card__header__title.
  • Modifiers go alongside the base class, never on their own: class="btn btn--large".
  • Single-class selectors make overrides simple and avoid specificity wars.

Frequently asked questions

›Is BEM still used?

Yes, especially in projects with plain CSS or Sass. Projects that use CSS Modules or Tailwind get the same isolation automatically and don't need it.