Sub topic 8
Dark mode with prefers-color-scheme and color-scheme
This chapter assumes you can declare custom properties and read them with var(). It also assumes you know the primitive, semantic and component tiers, and that you can override tokens in a scope. Here you use those skills to add dark mode without touching any component rule.
Two features, two jobs
Dark mode in CSS relies on two separate features:
prefers-color-schemeis a media query. It reports whether the user's operating system or browser asks for a light or dark interface.color-schemeis a property. It tells the browser which schemes your page supports, so the browser can restyle its built-in UI to match.
You need both. The media query switches your tokens. The property switches everything you did not style yourself.
Switch semantic tokens, not components
Components already reference semantic tokens such as --color-bg and --color-text. To add a dark theme, redefine those tokens and leave the components alone.
:root {
--color-bg: #ffffff;
--color-text: #111827;
}
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
--color-bg: #0b1020;
--color-text: #e5e7eb;
}
}
[data-theme="dark"] {
--color-bg: #0b1020;
--color-text: #e5e7eb;
}
body {
background: var(--color-bg);
color: var(--color-text);
}The :not([data-theme="light"]) selector lets an explicit user choice beat the system preference. A visitor with a dark OS who picks "light" in your toggle gets data-theme="light". That attribute stops the media block from matching, so the default light tokens apply.
Tell the browser with color-scheme
Custom properties only affect elements you style with them. Form controls, scrollbars, and the default page background still follow the browser's defaults. The color-scheme property fixes this.
:root {
color-scheme: light dark;
}
[data-theme="light"] {
color-scheme: light;
}
[data-theme="dark"] {
color-scheme: dark;
}The value light dark means the page supports both, and the browser follows the user's preference. Setting a single value forces that scheme. Keep it in step with your tokens. If data-theme="dark" sets dark tokens but color-scheme stays light, you get dark text on a dark page next to white form inputs.
You can also declare support in the HTML head, which lets the browser pick the right canvas color before your stylesheet loads:
<meta name="color-scheme" content="light dark">Common errors
| Mistake | Symptom | Fix |
|---|---|---|
| Overriding primitive tokens in dark mode | Names like `--color-gray-50` hold dark values and confuse readers | Override only semantic tokens |
| Omitting `color-scheme` | Light inputs and scrollbars on a dark page | Set `color-scheme` on `:root` |
| Using only the media query | Users cannot override the system setting | Add a `data-theme` attribute |
| Setting the attribute after first paint | A brief flash of the wrong theme | Set it from a small inline script in the head |
Try it
Copy the first block into a page with a heading and a text input. Then do the following:
- Switch your OS to dark mode and reload. The page should turn dark.
- Add
data-theme="light"to the<html>element. The page should return to light, even with the OS in dark mode. - Remove
color-schemeand compare the input and scrollbar in both modes.
Exercise
A visitor's OS is set to dark, and your page uses the CSS from this chapter. The <html> element has data-theme="light". Which tokens apply, and why?
Show answer
The light tokens apply. The media query block uses :root:not([data-theme="light"]), which does not match because the attribute is present. The default :root values therefore stay in effect, and color-scheme: light on [data-theme="light"] keeps the built-in controls light too.