CSS architecture and BEM
How to keep a large stylesheet predictable with flat specificity, a layered file structure and BEM naming, including common mistakes and cascade layers.
CSS architecture and BEM
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Architecture is the set of rules that keeps CSS from fighting itself as a team and a codebase grow: where styles live, how they are named, and how much specificity they are allowed.
- BEM names a piece of UI with three parts: a block (`card`), an element that only makes sense inside it (`card__title`), and a modifier that changes it (`card--featured`, `card__title--large`).
- Because every BEM selector is a single class, specificity stays at (0,1,0). Overrides are decided by source order, not by who wrote the longer selector.
- A block must not set its own outer margin or position. Layout between blocks belongs to the parent or to a layout class, or the block cannot be reused in different places.
- Naming conventions and the cascade tools of modern CSS complement each other: declare layer order with `@layer`, keep components flat, and put resets in `:where()`.
What goes wrong at scale
Imagine ten developers and a thousand lines of CSS. One writes .nav a { color: white }. Another writes .sidebar .nav a { color: black }. A third adds !important to win a fight. None of them is doing anything wrong in isolation, but the stylesheet is now a set of rules that win or lose by specificity accidents rather than by intent. Nobody dares delete a rule because nobody knows what it still affects.
The frontend roadmap’s architecture and naming conventions lesson compares BEM, utility classes and scoped CSS. This one goes deeper on the structural ideas, and the following lessons look at the tooling approaches individually.
CSS architecture comes down to a few commitments:
- Low, even specificity. If everything is one class, nothing needs to out-shout anything else.
- Predictable names. A class name tells you what it belongs to without opening the file.
- Components that do not reach outside themselves. Changing
.cardmust not alter a table. - A known place for each kind of rule. Resets, tokens, layout, components, utilities.
BEM in one idea
BEM (Block, Element, Modifier) is a naming scheme that delivers the first three. A block is a standalone piece of UI. An element is a part of a block that has no meaning outside it. A modifier is a variant of either.
<article class="card card--featured">
<img class="card__image" src="..." alt="">
<h2 class="card__title">Title</h2>
<a class="card__link card__link--primary" href="#">Read more</a>
</article>
.card { border: 1px solid #ddd; border-radius: 0.5rem; }
.card--featured { border-color: gold; }
.card__title { font-size: 1.25rem; }
.card__link { color: inherit; }
.card__link--primary { font-weight: 700; }
The pieces are separated by double underscores (elements) and double hyphens (modifiers), so single hyphens stay free for ordinary word breaks like main-nav. The modifier class is used together with the base class, not in place of it: the element has both card and card--featured.
Every selector is a single class. Specificity is always (0,1,0) and the order of rules decides conflicts. That is the property that matters most.
The rules that keep it working
The video’s chapters list the common questions and mistakes, and they are worth remembering:
- Do not chain elements.
card__title__iconencodes the markup depth. Elements belong to the block, however deeply nested:card__icon. - Do not nest classes in the CSS.
.card .card__titleraises specificity and adds a dependency on the structure. Write.card__title. - Do not put outer margins on blocks. A block’s margin ties it to one layout. Spacing between blocks is the parent’s job, using
gapor a layout class. - Choose modifier or new block deliberately. If a variant changes small things, use a modifier. If it has its own parts and behaviour, it is probably its own block.
- Do not use BEM entities as utility classes. A
card__titlethat is used outside a card is a sign it should be a block. - Do not create blocks with too many or too few elements. A block with one element probably does not need to be a block, and a block with thirty needs splitting.
Where cascade layers and :where() fit
BEM was invented before cascade layers, and the two work well together. Declare the order of the layers once at the top, and every rule inherits a clear position:
@layer reset, tokens, base, components, utilities;
@layer reset {
:where(*, *::before, *::after) { box-sizing: border-box; }
:where(h1, h2, h3, p) { margin: 0; }
}
@layer components {
.card { ... }
.card__title { ... }
}
@layer utilities {
.mt-2 { margin-top: 0.5rem; }
}
Rules in a later layer beat earlier layers regardless of specificity, so a utility class reliably beats a component without !important. Cascade layers and scope has the full mechanics. Writing resets with :where() keeps their specificity at zero, as :is(), :where() and :has() explains.
Folder structure
A common arrangement, loosely following ITCSS (the Inverted Triangle approach, from low-level and broad to specific):
styles/
reset.css element defaults, in :where()
tokens.css custom properties: colours, spacing, type scale
base.css plain elements: body, a, headings
layout.css containers, grids, stacks
components/ one file per block: card.css, button.css, nav.css
utilities.css small single-purpose helpers
Order the imports from broad to specific, or use @layer to make it explicit. One block per file makes deletion safe: remove card.css, and if no HTML uses .card, nothing else changes.
Costs of BEM
- Class names get long (
search-form__submit-button--disabled), and some people dislike how HTML reads. - It is a convention, not a tool. Nothing stops someone writing
.card .title, so it needs review or a linter. - With native nesting,
&__titledoes not work as it does in Sass, so each class is written out in full. That is also easier to search.
Other approaches avoid the naming problem. Utility-first CSS removes component class names, and CSS Modules let the build tool make names unique.
What the video covers
Dmitry Mayorov’s “Learn CSS BEM (and avoid these common mistakes)” (March 2024, about 16 minutes) has chapters on what CSS methodologies are, blocks, naming blocks, semantic versus functional blocks, elements, modifiers, whether a modifier can be used with an element, modifier versus a new block, whether a block can also be an element, using utility classes with BEM, and a list of mistakes: margins on blocks, nested classes, BEM entities as utilities, elements of elements, and too many or too few elements. The channel is small, about four thousand subscribers when checked, so this is a one-person channel rather than an established teacher. The chapter list is thorough and matches the rules above, but I could not read a transcript, so how it explains each rule is not described here. Kevin Powell’s shorter 2018 video on why he uses BEM is in further viewing.
Where this fits
Architecture follows from specificity and the cascade. The next lesson, Sass and preprocessors, covers the tool that made BEM files easier to write.
Check yourself
In `<article class="card card--featured"><h2 class="card__title">`, which name is the block, which the element, and which the modifier?
`card` is the block, a standalone component. `card__title` is an element, a part that belongs to the block and has no meaning alone. `card--featured` is a modifier on the block that changes its look. The modifier class is added next to the base class, not instead of it.
Why is `.card__title__icon` considered a mistake?
BEM elements are always a direct part of a block, not of another element, even when the HTML is nested deeper. A chain like this ties the name to the markup structure, so rearranging the HTML means renaming classes. Write `.card__icon` instead, or make the icon its own block if it is reused.
Why does BEM discourage writing `.card .title` or `.card > h2`?
Those selectors have higher specificity than a single class and depend on the markup shape. A flat `.card__title` has specificity (0,1,0), which any other single class can override by coming later, and it works wherever the element appears.
A BEM `.button` has `margin-top: 2rem` in its own rule. What goes wrong?
Wherever the button is reused, it carries that margin, even where it should sit tight against another element. Spacing between things is a layout concern. Put it on the parent with `gap`, or add a layout or utility class at the use site, so the button stays reusable.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.