Organizing layout CSS
Overview
Small projects can get away with one stylesheet written top to bottom. Real projects grow: dozens of components, several people, years of changes. Layout CSS then tends to rot — specificity wars, !important, margins fighting each other, and the same flexbox rules copied into twenty components.
This lesson covers four habits that keep layout CSS maintainable: cascade layers, layout primitives, spacing tokens, and a clear split of responsibilities.
Cascade layers
The cascade decides which rule wins using, in order: importance (!important), layer, specificity, then source order. Layers sit above specificity: a rule in a later layer beats a rule in an earlier layer, however specific the earlier one is.
/* Declare the order once, at the top of your first stylesheet */
@layer reset, base, layout, components, utilities;
@layer components {
.card .card-title { margin-block-start: 2rem; } /* specificity 0,2,0 */
}
@layer utilities {
.mt-0 { margin-block-start: 0; } /* 0,1,0 — but a later layer, so it wins */
}Utilities can be simple single-class rules and still win reliably — no !important needed. And a component’s more specific selector can’t accidentally beat a utility.
Things to know
- The first declaration sets the order. Later
@layer components { … }blocks add to an existing layer without changing its position. That’s why you declare the full order once, up front. - Unlayered styles win over all layers. Styles not in any
@layerbehave like a final, highest layer. This is handy for page-specific overrides, but it also means an old unlayered stylesheet will beat your layered system. - Third-party CSS can be layered on import:
@import url("library.css") layer(vendor);— then putvendorearly in your order so your styles win. !importantreverses the order: an important declaration in an earlier layer beats one in a later layer. This lets a reset layer protect critical rules, but it’s another reason to avoid!importantfor everyday overrides.
Cascade layers are supported in all major browsers since 2022.
Layout primitives
Most layouts are combinations of a few patterns you’ve already built in this course. Instead of rewriting them inside every component, define each once as a layout primitive — a small class with one job:
@layer layout {
/* Vertical rhythm between children */
.stack { display: flex; flex-direction: column; }
.stack > * + * { margin-block-start: var(--space, 1rem); }
/* A wrapping row: tags, buttons, nav links */
.cluster { display: flex; flex-wrap: wrap; gap: var(--space, 1rem); align-items: center; }
/* Responsive grid without breakpoints */
.auto-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(var(--min, 15rem), 100%), 1fr));
gap: var(--space, 1rem);
}
/* Sidebar that wraps by itself */
.with-sidebar { display: flex; flex-wrap: wrap; gap: var(--space, 1rem); }
.with-sidebar > :first-child { flex-basis: var(--sidebar, 15rem); flex-grow: 1; }
.with-sidebar > :last-child { flex-basis: 0; flex-grow: 999; min-inline-size: 60%; }
/* Centred column */
.center { box-sizing: content-box; max-inline-size: var(--measure, 65ch); margin-inline: auto; padding-inline: 1rem; }
}Then compose them in HTML:
<main class="center stack">
<h1>Courses</h1>
<ul class="cluster">…</ul>
<div class="auto-grid" style="--min: 18rem">…cards…</div>
</main>Custom properties make each primitive configurable (--space, --min, --sidebar) without new classes. A handful of primitives covers a surprising share of any site’s layout, and changing the spacing scale or the breakpoint behaviour happens in one place.
Why .stack uses the “owl” selector
.stack > * + * selects every child that has a previous sibling — so spacing goes between items, never before the first or after the last. You could use display: flex; flex-direction: column; gap: … instead; the margin version lets an individual child override its own spacing (with a utility like .mt-0).
Spacing tokens
Hard-coded spacing values (13px here, 1.1rem there) make layouts feel inconsistent. Define a small scale of custom properties and use only those:
:root {
--space-2xs: 0.25rem;
--space-xs: 0.5rem;
--space-s: 0.75rem;
--space-m: clamp(1rem, 0.8rem + 1vw, 1.5rem);
--space-l: clamp(1.5rem, 1rem + 2vw, 2.5rem);
--space-xl: clamp(2rem, 1.25rem + 3.75vw, 4.5rem);
}Combine with fluid clamp() values for the larger steps, and your whole site’s rhythm scales smoothly with the screen.
Separate responsibilities
A useful rule for large codebases: components don’t set their own outer margins.
- A component (
.card,.button,.nav) styles its own insides: padding, borders, internal layout. - A layout (
.stack,.cluster, a page grid) decides the space between components, withgapor the stack’s margins.
If a card sets margin-bottom: 2rem, it’ll be wrong somewhere — in a grid with gap, at the end of a list, inside a sidebar. Leaving spacing to the parent means components can be dropped anywhere and just fit.
File structure
How you split files matters less than being consistent. A common structure:
styles/
index.css → @layer order + @imports
reset.css → @layer reset
tokens.css → custom properties (colours, spacing, type)
base.css → @layer base
layout.css → @layer layout (primitives, page grid)
components/
card.css → @layer components
nav.css
utilities.css → @layer utilitiesWith layers, the import order no longer decides which styles win — the declared layer order does — so files can be split and loaded in whatever way suits your build.
Summary: declare your layers once, encode common layouts as a few configurable primitives, use spacing tokens, and let layouts own the space between components.
A layered mini design system
Organise the starter’s CSS into cascade layers declared once in this order: reset, base, layout, components, utilities. Put the box-sizing and margin resets in reset; body and heading styles in base; three layout primitives in layout — .stack (vertical spacing using --space, default 1rem), .cluster (wrapping row with gap) and .auto-grid (auto-fit grid, 15rem minimum); the .card styles in components; and .mt-0 in utilities. Show that .mt-0 beats the more specific .card .card-title rule because utilities is a later layer.
<main class="stack">
<h1>Courses</h1>
<ul class="cluster"><li>CSS</li><li>Layout</li><li>Grid</li></ul>
<div class="auto-grid">
<article class="card stack" style="--space: 0.5rem"><h2 class="card-title mt-0">Flexbox</h2><p>Rows and columns.</p></article>
<article class="card stack" style="--space: 0.5rem"><h2 class="card-title mt-0">Grid</h2><p>Two dimensions.</p></article>
</div>
</main>
<style>
/* Write your layers here */
</style><main class="stack">
<h1>Courses</h1>
<ul class="cluster"><li>CSS</li><li>Layout</li><li>Grid</li></ul>
<div class="auto-grid">
<article class="card stack" style="--space: 0.5rem"><h2 class="card-title mt-0">Flexbox</h2><p>Rows and columns.</p></article>
<article class="card stack" style="--space: 0.5rem"><h2 class="card-title mt-0">Grid</h2><p>Two dimensions.</p></article>
</div>
</main>
<style>
@layer reset, base, layout, components, utilities;
@layer reset {
*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; }
img { max-inline-size: 100%; display: block; }
}
@layer base {
body { font-family: system-ui, sans-serif; line-height: 1.5; padding: 1rem; }
h1, h2 { line-height: 1.15; }
}
@layer layout {
.stack { display: flex; flex-direction: column; }
.stack > * + * { margin-block-start: var(--space, 1rem); }
.cluster {
display: flex;
flex-wrap: wrap;
gap: var(--space, 1rem);
list-style: none;
padding: 0;
}
.auto-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(15rem, 100%), 1fr));
gap: var(--space, 1rem);
}
}
@layer components {
.card { border: 1px solid #d6d6d2; border-radius: 12px; padding: 1rem; }
.card .card-title { margin-block-start: 2rem; font-size: 1.25rem; }
}
@layer utilities {
.mt-0 { margin-block-start: 0; } /* wins over .card .card-title: later layer */
}
</style>Summary
- Cascade layers (
@layer) let you decide which group of styles wins, regardless of selector specificity. - Declare the layer order once at the top:
@layer reset, base, layout, components, utilities;. - Unlayered styles beat all layered styles — useful for overrides, surprising if forgotten.
- Layout primitives (stack, cluster, sidebar, auto-grid, center) encode common patterns once and are composed everywhere.
- Let components handle their insides and parents (layouts) handle spacing between components.