Sub topic 6
A Practical Layer Structure: Reset, Base, Components, Utilities
What you need first
This chapter assumes you can write CSS selectors and compare their specificity. It also assumes you know the @layer block syntax and the order statement from earlier chapters, and that you can use DevTools to see which rule won. Here you will apply those pieces to a four-layer structure that works for most projects.
Why a fixed structure helps
Without layers, you have to keep every rule's specificity in line with every other rule's. A .card a rule (specificity 0,1,1) beats a .text-red utility (0,1,0). People then reach for !important or longer selectors, and the stylesheet gets harder to change.
Layers let you decide priority by category instead. You sort rules into a few named groups and rank the groups once. Inside a group, normal specificity and source order still apply. Between groups, only the layer order matters.
The four-layer structure follows how CSS is usually written: general to specific, and from "defaults" to "deliberate overrides".
| Layer | Contains | Typical selectors | Priority |
|---|---|---|---|
| `reset` | Normalizing browser defaults | `*`, `body`, `h1`, `img` | Lowest |
| `base` | Design tokens and default element styling | `:root`, `a`, `body`, `h1` | Low |
| `components` | Reusable UI pieces | `.card`, `.btn`, `.nav` | Medium |
| `utilities` | Single-purpose overrides | `.text-red`, `.mt-0`, `.hidden` | Highest |
Declare the order once
Put one @layer statement at the very top of your first stylesheet. It fixes the ranking before any rules appear:
@layer reset, base, components, utilities;Layer order is set by the first time each layer name appears in the cascade. If a @layer components { ... } block shows up before this statement, components gets ranked first and your order statement can no longer move it. Keep the statement ahead of everything else, including @import rules. Only @charset may come before it, and @import is allowed after @layer statements.
Filling in each layer
reset
The reset removes inconsistencies. It belongs in the lowest layer because every other rule should be able to override it without any specificity effort.
@layer reset {
*, *::before, *::after {
box-sizing: border-box;
}
body, h1, h2, h3, p {
margin: 0;
}
img {
display: block;
max-width: 100%;
}
}An aggressive selector such as * has a specificity of zero, so layering it adds no safety. The real benefit is for resets that use higher-specificity selectors, such as button:not([disabled]). Inside the reset layer they can no longer beat a simple .btn in components.
base
The base layer holds the project's defaults. That means design tokens (custom properties) and styling for bare elements. It uses element selectors and :root, not classes.
@layer base {
:root {
--brand: royalblue;
--space: 1rem;
}
body {
font-family: system-ui, sans-serif;
line-height: 1.5;
}
a {
color: var(--brand);
}
}components
Each component gets its own rules. Because this layer outranks base, a component can restyle an element without raising specificity.
@layer components {
.card {
padding: var(--space);
border: 1px solid #ddd;
border-radius: 0.5rem;
}
.card a {
color: black;
}
.btn {
padding: 0.5rem 1rem;
background: var(--brand);
color: white;
}
}utilities
Utilities do one job each and must win when they are applied. Putting them in the top layer guarantees that, whatever selector a component used.
@layer utilities {
.text-red { color: red; }
.mt-0 { margin-top: 0; }
.hidden { display: none; }
}Seeing the structure work
Take this markup:
<div class="card">
<a href="/docs" class="text-red">Read the docs</a>
</div>Three rules match the link:
a { color: var(--brand); }inbase, with specificity 0,0,1..card a { color: black; }incomponents, with specificity 0,1,1..text-red { color: red; }inutilities, with specificity 0,1,0.
The link is red. Layer order is compared before specificity, and utilities is the last layer. The .card a selector is more specific than .text-red, but that no longer matters. Remove the text-red class and the link turns black, because components outranks base.
Without layers, you would have fixed this with .card a.text-red, or with !important. Here the utility works the same way everywhere.
Splitting the layers across files
You can keep each layer in its own file and still have a stable order. The first file only declares the order. The other files can then use @layer blocks in any sequence:
/* main.css */
@layer reset, base, components, utilities;
@import url("reset.css") layer(reset);
@import url("base.css") layer(base);
@import url("components.css") layer(components);
@import url("utilities.css") layer(utilities);The layer() function puts the entire imported file into that layer, so the imported files need no @layer wrapper of their own. A bundler can do the same job if you don't want the extra requests that @import causes in the browser. The next chapter covers importing in more detail.
If you load stylesheets with separate <link> tags, add the order statement to a small inline <style> block or to the first file in the list. Each file can then use @layer components { ... } and join the right layer.
Where unlayered CSS goes
Any rule outside every @layer is unlayered. For normal declarations, unlayered rules beat all layered rules, however high the layer is ranked. That has two consequences for this structure.
First, a stray unlayered rule can silently override your utilities:
@layer utilities {
.hidden { display: none; }
}
.modal { display: flex; }An element with class="modal hidden" still displays as a flex container, because .modal is unlayered. Move the .modal rule into the components layer and .hidden wins as intended.
Second, you can use this on purpose. Page-specific or one-off CSS that should beat everything can stay unlayered. Most teams do the opposite and put all their own CSS in layers, leaving the unlayered space for emergency overrides.
Inline style attributes also beat layered normal declarations, as they did before.
Adding more layers
Four layers are a starting point, not a rule. Common variations:
- A
vendorlayer beforeresetfor third-party CSS, so everything of yours outranks it. - A
layoutslayer betweenbaseandcomponentsfor page grids and shells. - A
themeslayer betweencomponentsandutilitiesfor dark mode or brand overrides.
Add a layer only when you can name a rule category that should always lose to, or always beat, the layers around it. If you can't explain why a layer sits in its position, you will have trouble deciding where new rules should go.
@layer vendor, reset, base, layouts, components, themes, utilities;Common errors
- Order statement too late. An earlier
@layerblock with the same name has already fixed its position. Check the top of your first file. - Misspelled layer names.
@layer component { ... }creates a new layer namedcomponent, which is ranked after all the declared ones. It then beatsutilities. Nothing warns you about it. - Using utilities with
!importantby habit. Utilities already win by layer order, so you don't need it. Also,!importantreverses layer order, so an important declaration inresetwould beat an important one inutilities. The reversal is covered in a later chapter. - Leaving some CSS unlayered by accident. A file that was never wrapped in
@layeror imported withlayer()will beat all your layers. When something unexpectedly wins, check first whether the winning rule is layered at all. - Expecting layers to change specificity inside a layer. Within
components,.card astill beats.link. Layers only settle conflicts between layers.
In DevTools, the Styles pane in current browsers labels each rule with its layer name. A rule from the winning layer appears above the crossed-out ones from lower layers. Read the layer label before you read the specificity.
Exercise
A project declares @layer reset, base, components, utilities;. The base layer contains #sidebar a { color: gray; } and the utilities layer contains .link-accent { color: orange; }. A link with class link-accent inside #sidebar is displayed. What colour is it, and why?
Show answer
Orange. Both rules are normal declarations in layers, so the layer order is compared first and utilities outranks base. The ID selector's higher specificity in base is never compared.
Exercise
You add .alert { display: block; } at the top level of a stylesheet, outside any layer, and an element has class="alert hidden" where .hidden { display: none; } is in the utilities layer. Does the element show? How do you fix it so that .hidden works?
Show answer
It shows, because the unlayered .alert rule beats every layered normal declaration, including the top layer. Fix it by moving .alert into @layer components { ... }, so that utilities outranks it.
Exercise
A teammate writes @layer component { .btn { background: teal; } } (missing the "s"), while the order statement lists components. Which layer does the rule end up in, and how does it rank?
Show answer
It creates a new layer named component. Layers that were not listed in the order statement are ranked after the declared ones, so it outranks utilities and will override utilities for normal declarations. The fix is to correct the spelling.