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

BEM Nesting Rules and When to Create New Blocks

Understanding when nested elements should become their own blocks to avoid deep nesting.

BEM Nesting Rules and When to Create New Blocks

You'll learn when deeply nested elements should become their own blocks to keep your BEM structure maintainable.

The Nesting Problem

In BEM, elements belong to blocks using the double-underscore syntax: .card__title, .card__body, .card__footer. But what happens when your footer contains a button? Should you write .card__footer__button? No! Triple (or more) underscores are a red flag in BEM.

The Rule: Maximum Two Levels

BEM intentionally limits you to block + element only. There's no "element of an element" syntax. If you find yourself wanting .block__element__subelement, it's time to create a new block instead.

When to Create a New Block

Think of blocks as reusable components. If a nested piece could exist independently or appear elsewhere in your site, make it a block:

/* ❌ Don't do this */
.card__footer__button { }

/* ✅ Do this instead */
.card__footer { }
.button { }  /* New block! */
.button--primary { }  /* Modifier for the button block */

In your HTML, the button sits inside the footer but maintains its own identity as a separate block.

The Independence Test

Ask yourself: "Could this component exist outside its current parent?" If yes, it deserves to be a block. Buttons appear everywhere. Titles might be card-specific (.card__title), but buttons are universal (.button).

Key Takeaway: BEM elements never nest more than one level deep; when you need deeper structure, create a new independent block instead of chaining element names together.