Organizing a Stylesheet With Layers: Reset, Theme, Layout, Components and Utilities
A complete layered stylesheet: what belongs in reset, theme, base, layout, components and utilities, where third-party CSS goes, splitting files with @import layer(), nested layers for states, and why specificity stops being a fight.
kvi5hnuCode Sprout team member7 min readOn this page
Cascade layers (article 20) let you decide which group of rules wins, regardless of specificity. That raises a practical question: what groups should a real stylesheet have? There's no official answer, but most teams converge on the same few layers, ordered from general to specific. This article builds one complete stylesheet that way and shows what goes where, and why.
The layer order statement
The first line of the stylesheet names every layer, from lowest to highest priority:
@layer reset, theme, base, layout, components, utilities;
Declaring the order up front means it can't change by accident, whatever order the files load in later. Each layer has one job:
| Layer | Job | Typical selectors |
|---|---|---|
reset | fix browser defaults you don't want | *, element types |
theme | site-wide design tokens | :root |
base | default look of plain HTML elements | element types, :where() |
layout | page structure: header, sidebar, columns | .l-* classes, IDs |
components | reusable modules (article 116) | one class per module |
utilities | single-purpose classes that must win | .visually-hidden, .text-center |

Each frame above adds one more layer to the same HTML. The reset removes the defaults, theme and base add the site's look to plain elements, layout arranges the page, components style the reusable pieces, and utilities make small final adjustments.
reset: fixing the defaults
The reset makes browser defaults predictable. It contains no design decisions, so the same layer can be reused across projects almost unchanged:
@layer reset {
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; }
img, svg, video { display: block; max-inline-size: 100%; }
input, button, textarea, select { font: inherit; }
h1, h2, h3, p, figure { margin: 0; }
@media (prefers-reduced-motion: reduce) {
*, ::before, ::after { animation-duration: 1ms !important; transition-duration: 1ms !important; }
}
}
Browsers are consistent enough today that resets mainly fix unwanted defaults: content-box sizing (article 36), the body margin, form controls not inheriting fonts (article 102), and inline images leaving a gap below (article 45). Because the reset is the lowest layer, everything later overrides it without specificity tricks.
The !important in the reduced-motion rule is deliberate: important declarations in an earlier layer beat important declarations in later ones (article 20), so a reset can protect a rule that nothing else should undo.
theme: design tokens
Custom properties used across the whole site go here, along with the properties that set the page's overall color behavior:
@layer theme {
:root {
color-scheme: light dark;
--color-ink: light-dark(#2a2118, #f1ebe2);
--color-paper: light-dark(#fbf8f3, #1c1813);
--color-brand: #8a3b12;
--font-body: system-ui, sans-serif;
--space: 0.75rem;
--radius: 0.5rem;
accent-color: var(--color-brand);
}
}
Article 118 covers organizing tokens. Custom properties used by only one component, like a card's --card-padding, belong with that component instead.
base: plain HTML elements
Default styles for elements without classes: body text, headings, links, lists, tables, code. Almost every selector is an element type:
@layer base {
body { font: 1rem/1.5 var(--font-body); color: var(--color-ink); background: var(--color-paper); }
h1, h2, h3 { line-height: 1.15; text-wrap: balance; }
a { color: var(--color-brand); }
:where(ul, ol)[role="list"] { list-style: none; padding: 0; }
}
This layer is sometimes called global or elements.
layout: page structure
Where the big areas go: header, footer, sidebars, the main column, and recurring layout patterns such as stacks and card grids. A common convention prefixes layout classes with l- to tell them apart from components at a glance:
@layer layout {
.l-page { display: grid; grid-template-columns: minmax(0, 1fr); gap: var(--space); }
@media (width >= 50rem) { .l-page { grid-template-columns: 14rem minmax(0, 1fr); } }
.l-stack > * + * { margin-block-start: var(--space); }
.l-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(12rem, 1fr)); gap: var(--space); }
}
IDs for page types, like #home .l-page, used to be avoided because their specificity was impossible to override. In a low layer, that no longer matters: any later layer beats them.
components: most of the CSS
Buttons, cards, menus, dialogs and every other module from article 116. This is by far the largest layer, usually split into one file per component:
@layer components {
.button { padding: 0.4em 0.9em; border: 0; border-radius: var(--radius); background: var(--color-brand); color: white; }
.button--secondary { background: transparent; color: var(--color-brand); box-shadow: inset 0 0 0 2px currentColor; }
.card { padding: var(--space); border-radius: var(--radius); background: Canvas; box-shadow: 0 1px 3px rgb(0 0 0 / 0.15); }
}
utilities: small classes that win
A utility does one thing, and when you add it to an element, you expect it to apply: .visually-hidden, .text-center, .muted, .flow. As the last layer, utilities beat any component rule, however specific, so they don't need !important:
@layer utilities {
.text-center { text-align: center; }
.muted { color: color-mix(in oklab, currentColor 65%, transparent); }
.visually-hidden {
clip-path: inset(50%); block-size: 1px; inline-size: 1px;
overflow: hidden; position: absolute; white-space: nowrap;
}
}

Avoid !important in utilities anyway: important declarations reverse the layer order, so an important utility would lose to an important rule in any lower layer.
Third-party CSS and unlayered styles
Put CSS you don't control in a low layer of its own, so your own layers override it without a specificity battle:
@layer reset, vendor, theme, base, layout, components, utilities;
@import url("vendor/datepicker.css") layer(vendor);
@import must come before every other rule except @charset and @layer statements, which is why the order statement can sit above it.
Anything not in a layer beats every layer. That's useful for a quick override during debugging or a one-off fix, but it's also a trap: a third-party file loaded without layer() silently outranks your whole layered stylesheet. Keep your own CSS in layers and use unlayered rules sparingly.
Splitting into files
With layers, file order no longer decides priority, so files can be organized purely for humans:
/* main.css */
@layer reset, vendor, theme, base, layout, components, utilities;
@import url("reset.css") layer(reset);
@import url("tokens.css") layer(theme);
@import url("base.css") layer(base);
@import url("layout.css") layer(layout);
@import url("components/button.css") layer(components);
@import url("components/card.css") layer(components);
@import url("utilities.css") layer(utilities);
In production, a build tool usually bundles these into one file, keeping the layers. Several imports can share one layer; their rules are combined in import order.
Nested layers for more structure
A big layer can be split with nested layers, for example to make states always beat variants inside components:
@layer components {
@layer base, variants, states;
}
@layer components.base { .tab { color: GrayText; } }
@layer components.variants { .tab--pill { border-radius: 99px; } }
@layer components.states { .tab[aria-selected="true"] { color: CanvasText; } }
Some teams add a top-level states layer instead, or an overrides layer after utilities for page-specific exceptions. Add layers only when they solve a real conflict: six or seven are plenty for most sites.
Layers in frameworks
Many current frameworks ship their CSS in layers with similar names. Recent utility-first frameworks, for example, emit @layer theme, base, components, utilities;. If you add your own rules, put them in the matching layers so they interact predictably, and check the framework's order statement in DevTools: Chrome's Styles pane can show the full layer order.
@layer reset, theme, base, layout, components, utilities;
@layer reset {
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; }
h1, h2, p { margin: 0; }
button { font: inherit; }
}
@layer theme {
:root {
color-scheme: light dark;
--ink: light-dark(#2a2118, #f1ebe2);
--paper: light-dark(#fbf8f3, #1c1813);
--brand: #8a3b12;
--space: 0.75rem;
--radius: 0.5rem;
}
}
@layer base {
body { font: 0.9rem/1.45 system-ui, sans-serif; color: var(--ink); background: var(--paper); }
h1 { font-size: 1.4rem; } h2 { font-size: 1rem; }
a { color: var(--brand); }
:where(ul)[role="list"] { list-style: none; padding: 0; margin: 0; }
}
@layer layout {
.l-page { padding: var(--space); }
.l-stack > * + * { margin-block-start: var(--space); }
.l-header { display: flex; justify-content: space-between; align-items: center; }
.l-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr)); gap: 0.5rem; }
}
@layer components {
.button { padding: 0.35em 0.8em; border: 0; border-radius: var(--radius); background: var(--brand); color: white; }
.card { padding: 0.6rem; border-radius: var(--radius); background: Canvas; box-shadow: 0 1px 3px rgb(0 0 0 / 0.15); }
.card > * + * { margin-block-start: 0.25rem; }
.tag { display: inline-block; padding: 0 0.45em; border-radius: 99px; background: #f1e3d3; color: #8a3b12; font-size: 0.8em; }
}
@layer utilities {
.muted { color: color-mix(in oklab, currentColor 65%, transparent); }
.text-center { text-align: center; }
}
Try moving utilities before components in the first line: the order statement alone changes which rules win, without touching any selector.
What's current
- Cascade layers work in all browsers; building whole stylesheets around them is now common practice, and many frameworks ship layered CSS.
- A
layerattribute for<link>, to put a linked stylesheet into a layer from HTML, has been proposed but isn't supported yet. Use@import … layer().
Common questions
Do I need all six layers?
No. Small sites often use reset, base, components and utilities. Add layers when two groups of rules start to conflict.
Where do component states go?
In the component itself, or in a nested components.states layer if they keep needing to beat variants.
Why does my third-party CSS override everything?
It was loaded without a layer, and unlayered styles beat all layers. Import it with layer(vendor).
Can I still use specificity? Yes, inside a layer it works as always. Layers just stop it from leaking between groups.
Summary
- Start with one
@layerorder statement, from lowest to highest priority. resetfixes defaults,themeholds tokens,basestyles elements,layoutarranges the page,componentsholds most of the CSS, andutilitieswin.- A rule in a later layer beats any rule in an earlier one, whatever its specificity; unlayered rules beat all layers.
- Put third-party CSS in a low
vendorlayer with@import … layer(). - Use nested layers for structure inside a big layer, and avoid
!importantin high layers.
Practice questions
A component rule with specificity 0,3,0 should be overridden by a utility class with 0,1,0. How do you guarantee that without !important?
Answer
Put the utility in a layer declared after the components layer:
@layer components, utilities;
@layer utilities { .hidden { display: none; } }
Layer order is compared before specificity.
Your layered stylesheet is being overridden by a date-picker library's CSS. Fix it without editing the library.
Answer
Import it into a low layer:
@layer reset, vendor, base, components, utilities;
@import url("datepicker.css") layer(vendor);
Unlayered, it would beat every layer.
In which layer does each rule belong?
:root { --radius: 0.5rem; }
.visually-hidden { position: absolute; clip-path: inset(50%); }
img { display: block; max-inline-size: 100%; }
.l-sidebar { grid-column: 1; }
.alert--warning { background: #fff4d6; }
Answer
theme, utilities, reset, layout and components, in that order.
Why can a rule in reset use !important to protect itself?
Answer
Important declarations reverse the layer order, so an important declaration in an earlier layer beats important declarations in every later layer.
Previous: Modular CSS: Components, Variants, Naming Conventions and BEM

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.