CSS variables and architecture
Small sites can get away with messy CSS. Big ones can't: thousands of rules, many developers, and redesigns. This lesson covers custom properties (CSS variables) and the architecture habits that keep CSS maintainable.
Custom properties
- Define them with
--name: value;, usually on:root(the<html>element), so they're global. - Use them with
var(--name), orvar(--name, fallback). - Unlike Sass variables, they're live: they cascade, inherit, change in media queries, and JavaScript can update them.
Theming: dark mode in a few lines
This website uses exactly this technique for its light and dark modes.
Component-scoped variables
Variables can be set on a component to create variants without repeating rules:
Design tokens
Professional teams define a design system as tokens: a limited set of colours, spacing steps, font sizes, radii and shadows. Developers pick from the scale instead of inventing new values.
:root {
/* spacing scale */
--space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px; --space-6: 24px; --space-8: 32px;
/* type scale */
--text-sm: 0.875rem; --text-base: 1rem; --text-lg: 1.25rem; --text-xl: 1.5rem; --text-2xl: 2rem;
/* semantic colours */
--color-surface: #fff; --color-text: #0f172a; --color-primary: #4f46e5; --color-danger: #dc2626;
}Naming: the BEM convention
BEM (Block, Element, Modifier) gives every class a predictable name and keeps specificity flat:
.card { } /* Block: a standalone component */
.card__title { } /* Element: part of the block */
.card__image { }
.card--featured { } /* Modifier: a variant */<article class="card card--featured">
<img class="card__image" src="…" alt="…">
<h3 class="card__title">…</h3>
</article>Architecture rules that scale
- Keep specificity low and flat. Prefer single classes. Avoid ids and long chains like
.page .sidebar ul li a. - Avoid
!important, except in utility classes or when overriding third-party CSS. - Organise by layer, from generic to specific:
styles/
1-settings/ tokens.css (variables)
2-base/ reset.css, typography.css (element selectors)
3-layout/ container.css, grid.css
4-components/ button.css, card.css, navbar.css
5-utilities/ .sr-only, .mt-4, .text-center- Use a modern reset:
box-sizing: border-box,margin: 0, andimg { display: block; max-width: 100% }.
Popular approaches in the industry
| Approach | Idea | Examples |
|---|---|---|
| BEM + plain CSS | Naming conventions | Many agencies and design systems |
| CSS Modules | Class names scoped per component file automatically | React and Vite projects |
| Utility-first | Small single-purpose classes in the HTML | Tailwind CSS |
| CSS-in-JS | Styles written in JavaScript components | styled-components, Emotion |
| Preprocessors | Variables, nesting and mixins compiled to CSS | Sass/SCSS |
Native CSS now has variables, color-mix(), nesting and cascade layers, so much of what Sass did is built in.
/* Native CSS nesting */
.card {
padding: 16px;
& h3 { margin: 0; }
&:hover { box-shadow: var(--shadow); }
@media (min-width: 768px) { padding: 24px; }
}Practice
Refactor this CSS to use variables for every repeated value, add a .btn--danger modifier that only changes a variable, and add a dark theme with [data-theme="dark"].
1.How do you use a custom property named --gap?
2.In BEM, what does card--featured represent?