- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS Architecture
CSS Output & Performance
CSS Architecture
Organize tokens, base rules, components, utilities, and exceptions so a stylesheet remains easy to change. The chapter organizes tokens, base rules, components, utilities, variants, layers, selector weight, and the maintenance of exceptions.
CSS architecture is simply a set of agreements: where a rule belongs, how it is named, and what is allowed to override what. Without them, every change risks breaking something on another page, and the stylesheet only ever grows.
You do not need a big framework for this. Two modern tools do much of the work: Cascade Layers for deciding which group of rules wins, and CSS Variables for shared design decisions like colors and spacing.
Global foundations, local components
Example
css
@layer reset, tokens, base, components, utilities;
@layer tokens {
:root {
--color-action: #0369a1;
--space-2: 0.5rem;
--space-4: 1rem;
}
}
@layer components {
.button { /* component contract */ }
.button--danger { /* explicit variant */ }
}The layers line goes from weakest to strongest: resets, then design tokens, then element defaults, then components, then small utility classes. Each rule has an obvious home, and a utility like .mt-0 reliably beats a component without needing !important.
Name classes by role, like .card and .card--featured, not by look or location, like .blue-box or .homepage-left. Roles stay true when the design changes.
Habits that keep CSS healthy
- Keep selectors shallow and specificity intentionally low.
- Co-locate component states and responsive rules.
- Use custom properties for meaningful design decisions, not every literal.
- Document escape hatches and delete expired exceptions.
- Lint syntax and property mistakes in continuous integration.
- Track unused styles instead of assuming a class is safe to remove.
Stylesheet Layers
| Layer | Responsibility | Example |
|---|---|---|
| Tokens | Shared design decisions. | --color-accent, --space-3 |
| Base | Element defaults and document-wide behavior. | body, button, a |
| Components | Named interface patterns. | .card, .site-nav |
| Utilities | Small single-purpose overrides. | .visually-hidden |
Complete Example
Complete runnable example
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS Architecture example</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
padding: 2rem;
font-family: system-ui, sans-serif;
line-height: 1.5;
}
main { max-width: 70rem; margin-inline: auto; }
article, section, aside, form, .card, .panel {
padding: 1rem;
border: 1px solid #cbd5e1;
border-radius: 0.6rem;
}
@layer reset, tokens, base, components, utilities;
@layer tokens {
:root {
--color-action: #0369a1;
--space-2: 0.5rem;
--space-4: 1rem;
}
}
@layer components {
.button { /* component contract */ }
.button--danger { /* explicit variant */ }
}
</style>
</head>
<body>
<main><h1>Layered component styles</h1><button class="button">Default button</button><button class="button button--danger">Danger variant</button></main>
</body>
</html>Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| Custom properties | 49 | 15 | 31 | 9.1 |
| Cascade layers | 99 | 99 | 97 | 15.4 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.
Naming systems are tools
BEM, utility classes like Tailwind, CSS Modules and scoped component styles all solve the same problem in different ways. Any of them works if the whole team follows it. Mixing three systems in one project is worse than picking the "wrong" one.
Every few months, delete what is no longer used. Unused CSS is not harmless: everyone is afraid to touch it, so it never goes away on its own.
CSS Architecture exercises
One place for a color
On :root define --brand as #0f766e and use it as the background of every .btn.
The HTML your code works on
<button class="btn">Buy</button>
<button class="btn btn--primary">Pay</button>Result
