Course contentsShow
← Web Development
Lesson 1326 of 2,98224. CSS Architecture and MaintainabilityFree lesson

BEM in HTML: Class Application Patterns

Best practices for applying BEM classes to HTML elements without over-nesting.

BEM in HTML: Class Application Patterns

What You'll Learn

You'll learn how to apply BEM classes directly to HTML elements efficiently without creating unnecessarily deep or complicated class structures.

Applying BEM Classes Cleanly

When writing HTML with BEM, the goal is to keep your markup flat and readable even when your component structure has visual hierarchy. Each class should be independent and applied directly to the element it describes—no complex nesting required.

The Pattern: Direct Application

Think of BEM classes like nametags at a conference. Each person (element) wears their own complete tag—you don't create nested tags that reference other people's tags.

For a card component with a title and button:

<!-- ✅ Good: Flat, clear, independent classes -->
<div class="card card--featured">
  <h2 class="card__title">Welcome</h2>
  <p class="card__text">Some description here.</p>
  <button class="card__button card__button--primary">Click Me</button>
</div>

Avoid creating element chains in your class names:

<!-- ❌ Bad: Over-nested naming -->
<div class="card">
  <h2 class="card__header__title">Welcome</h2>
</div>

Key Rules

  1. One block reference per class: card__button, never card__content__button
  2. Apply classes to every meaningful element: Don't rely on tag selectors like .card h2
  3. Combine block and modifier when needed: card card--featured uses two classes, not one merged name

Key Takeaway: Apply BEM classes directly to each HTML element with a single level of relationship (Block__Element), keeping your markup flat and your CSS predictable regardless of HTML structure depth.