Sub topic 6
Theming and Dark Mode
This chapter assumes you can declare custom properties and read them with var(), and that you know how they inherit down the DOM tree. It also assumes you know basic specificity and source-order rules. Here you will use those pieces to switch a whole page between a light and a dark theme.
Themes are sets of values
A theme is a group of custom properties defined on a high-level element, usually :root. Every rule in the stylesheet reads those properties with var(). To change the theme, you redefine the properties and leave the rules that use them alone.
This works because custom properties inherit. A value set on :root reaches every element, and the browser recomputes each var() when the value changes.
Following the operating system
The prefers-color-scheme media feature reports whether the user's system is set to light or dark. Define the light values first, then override them inside a media query.
:root {
--bg: #ffffff;
--fg: #111111;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #111111;
--fg: #eeeeee;
}
}
body {
background: var(--bg);
color: var(--fg);
}The body rule never mentions a theme. It asks for --bg and --fg, and the media query decides what those mean.
Letting the user choose
Many sites offer a toggle that overrides the system setting. A common approach is an attribute on the <html> element, such as data-theme="dark", with a rule that redefines the same properties.
:root {
--bg: #ffffff;
--fg: #111111;
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--bg: #111111;
--fg: #eeeeee;
}
}
[data-theme="dark"] {
--bg: #111111;
--fg: #eeeeee;
}The :not([data-theme="light"]) selector handles one case. Suppose the system is dark and the user explicitly picks light. Without the :not(), the media query would still apply and the user's choice would be ignored. With it, the media query applies only when the user has not picked light.
Specificity matters here. :root and [data-theme="dark"] each have a specificity of one class-level selector, so source order decides between them. Put the attribute rules after the base rules.
Saving the choice with JavaScript
A small script flips the attribute and stores the choice. It assumes a <button id="toggle"> in your HTML.
const root = document.documentElement;
const saved = localStorage.getItem('theme');
if (saved) root.setAttribute('data-theme', saved);
document.querySelector('#toggle').addEventListener('click', () => {
const current =
root.getAttribute('data-theme') ??
(matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
const next = current === 'dark' ? 'light' : 'dark';
root.setAttribute('data-theme', next);
localStorage.setItem('theme', next);
});The current line covers the first click. With no attribute set yet, the script asks the browser what the system theme is, so the first click always switches to the opposite theme.
Naming tokens by role
Name properties by what they do, not by what they look like. --bg and --fg still make sense in a dark theme. --white would not, because it would hold a dark color.
For larger sites, use two layers. A palette layer holds raw colors such as --gray-900. A semantic layer maps roles to palette entries, for example --bg: var(--gray-100). A theme then redefines only the semantic layer.
Common mistakes
- Placing the attribute rules before the base rules, so the base values win on source order.
- Defining the dark values only on
body. Elements outsidebody, such as the root background, will not pick them up. - Using hard-coded colors in some components. Those components will not change with the theme.
- Forgetting the user-chosen light case, as described above.
Exercise
A user's system is set to dark, and they click your toggle to choose light, which sets data-theme="light" on the html element. Your media query uses plain :root { ... } with no :not(). Which theme do they see, and how do you fix it?
Show answer
They still see the dark theme, because the media query matches the system setting and nothing in it checks the attribute. Change the selector inside the media query to :root:not([data-theme="light"]) so the dark values apply only when the user has not chosen light.