CSS architecture: BEM, file order and cascade layers
Keep big stylesheets tidy with BEM naming, a broad-to-specific file order, a small reset and cascade layers.
In this lesson you will
- Name classes with BEM
- Order a stylesheet from broad to specific
- Write a small reset
- Control priority with @layer
About 17 minutes
When the stylesheet becomes a junk drawer
Every project starts tidy. Six months later the stylesheet has 4,000 lines, three rules for .button that fight each other, a few !important flags nobody dares remove, and a developer who says “I changed one colour and the footer broke”. This lesson is about preventing that. The tools are simple: a naming system, a sensible file order, and a modern feature called cascade layers.
A naming system: BEM
BEM stands for Block, Element, Modifier. It is a plain naming convention that makes a class tell you exactly what it is and where it belongs.
/* Block: a standalone component */
.card { }
/* Element: a part of the block, joined with two underscores */
.card__title { }
.card__image { }
.card__button { }
/* Modifier: a variation, joined with two hyphens */
.card--featured { }
.card__button--disabled { }
<article class="card card--featured">
<img class="card__image" src="a.jpg" alt="">
<h3 class="card__title">Learn CSS</h3>
<a class="card__button" href="#">Start</a>
</article>
- Every rule is one class, so specificity stays low and equal and rules do not fight.
- You can read the HTML and know what each part is for.
- Components can be moved or deleted without side effects, because no rule depends on where the element sits in the page.
You do not have to use BEM. Other teams prefer utility classes or component libraries. What matters is that you pick one convention and follow it. Consistency matters more than which system you choose.
Style this card using BEM names: a .card__title element and a .card–featured modifier with a violet border.
<style>
body { font-family:Arial; padding:20px; background:#F4F1FC; }
.card { background:#fff; padding:18px; border-radius:14px; border:2px solid transparent; max-width:260px; }
/* .card__title */
/* .card--featured */
</style>
<div class="card card--featured"><h3 class="card__title">Learn CSS</h3><p>Free lessons.</p></div>
.card__title{margin:0 0 6px; color:#5A28C8} .card–featured{border-color:#5A28C8}
<style>
body { font-family:Arial; padding:20px; background:#F4F1FC; }
.card { background:#fff; padding:18px; border-radius:14px; border:2px solid transparent; max-width:260px; }
.card__title { margin:0 0 6px; color:#5A28C8; }
.card--featured { border-color:#5A28C8; }
</style>
<div class="card card--featured"><h3 class="card__title">Learn CSS</h3><p>Free lessons.</p></div>
Organise the file from broad to specific
Because later rules win ties, the order of your stylesheet is itself a tool. A reliable layout moves from the broadest rules to the most specific:
- Tokens: the
:rootvariables for colours, spacing and type. - Reset and base: box-sizing, body, headings, links, images.
- Layout: containers, grids, page structure.
- Components: cards, buttons, forms, navigation, one section each.
- Utilities: tiny single-purpose helpers such as
.visually-hiddenor.text-center. - Page-specific and overrides: the last few exceptions.
Put a short comment banner above each section so you can search for it. On big sites split these into separate files and combine them with a build tool, or with @import in development.
A tiny, honest reset
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; line-height: 1.6; }
img, svg, video { max-width: 100%; height: auto; display: block; }
button, input, select, textarea { font: inherit; }
:focus-visible { outline: 3px solid #5A28C8; outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) {
* { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
This is enough for most sites. Do not copy a 600-line reset you do not understand. Every line in your base should be one you can explain.
Cascade layers: control the order, not the weight
Specificity wars happen when a library’s rule beats yours and you answer with a longer selector or !important. Cascade layers end that. You declare named layers in order, and a rule in a later layer beats a rule in an earlier layer no matter how specific the earlier one is.
@layer reset, base, components, utilities;
@layer reset {
*, *::before, *::after { box-sizing: border-box; }
}
@layer base {
a { color: #5A28C8; }
}
@layer components {
.nav a { color: #14112E; }
}
@layer utilities {
.text-brand { color: #5A28C8; }
}
The first line is the important one. It fixes the priority: utilities beats components, which beats base, which beats reset. A simple .text-brand in the utilities layer now beats .nav a, even though .nav a is more specific.
The paragraph is styled by a very specific selector, but the utilities layer is declared last. Declare the layers in the right order so .accent wins, without !important.
<style>
/* declare the layer order here */
@layer components { section p.text { color: #888; } }
@layer utilities { .accent { color: #5A28C8; font-weight: 700; } }
body { font-family: Arial; padding: 20px; }
</style>
<section><p class="text accent">Which colour do I get?</p></section>
@layer components, utilities;
<style>
@layer components, utilities;
@layer components { section p.text { color: #888; } }
@layer utilities { .accent { color: #5A28C8; font-weight: 700; } }
body { font-family: Arial; padding: 20px; }
</style>
<section><p class="text accent">Which colour do I get?</p></section>
Unlayered styles beat all layered styles. If you put your own CSS in layers but leave a third-party stylesheet outside any layer, the third party wins. Wrap imported libraries with @import url(lib.css) layer(vendor); and list vendor first.
Habits that keep CSS healthy
- Style with classes, not IDs or deep selectors. Keep selectors to one or two parts.
- Avoid !important. Reserve it for utilities like a hide helper, or for overriding code you cannot edit.
- Use variables for anything repeated: colours, spacing, radii, shadows.
- Delete dead CSS. Search for a class before removing it, then trust version history.
- Name by purpose, not looks.
.btn-primaryages better than.big-purple-button. - One component, one place. Everything about the card lives in the card section.
- Comment the why, not the what. “Safari needs this for sticky inside flex” is helpful. “Sets colour” is not.
Over-qualifying selectors, such as div.container ul.menu li a.link. It is brittle and heavy. If you change the markup, the style breaks. Use .menu__link instead.
Open the stylesheet once a month. Merge duplicates, remove dead rules, rename the confusing. Ten minutes of care saves hours of fear later.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
What does BEM stand for?
- Block, Element, Modifier
- Base, Edge, Margin
- Box, Embed, Module
- Browser Engine Model
It is a naming convention.
Which BEM class is a modifier?
.card__title.card--featured.card.title
Two hyphens mark a modifier.
In @layer reset, base, components; which layer has the highest priority?
- reset
- base
- components
- They are equal
Later layers win.
Why avoid long selectors like div.a ul.b li a.c?
- They are brittle and tied to markup
- They are invalid
- They are faster
- They cannot be minified
Short class selectors are easy to override and maintain.
Which style beats layered styles?
- Unlayered styles
- Reset layer
- Utilities layer only
- None
Normal unlayered rules outrank layered ones.
Summary
- BEM names show a part’s role at a glance.
- Order tokens, base, layout, components, utilities.
- @layer sets priority independent of specificity.
- Short selectors, few !important, delete dead code.
Questions
Is BEM required?
No. It is one option. Choose a convention and keep it consistent.
Should I use a CSS framework?
Frameworks save time on big projects. Learn core CSS first so you can fix and customise what they produce.
What is a CSS preprocessor?
A tool like Sass that adds features and compiles to CSS. Native variables and nesting have removed many reasons to need one.
