Sub topic 11
Adapting the Palette for Dark Mode
This chapter assumes you can write CSS custom properties, know HSL, and have a palette with a semantic layer on top, as built in the earlier chapters. You will add a dark theme by changing only the semantic tokens.
Why the semantic layer makes this easy
Components in your stylesheet use names like --color-text and --color-bg. They never use raw shades like --neutral-900. That means a dark theme is a different mapping from semantic names to palette steps. The palette itself and every component rule stay as they are.
The browser reports the user's system preference through the prefers-color-scheme media feature. You can read it in CSS and reassign tokens when its value is dark.
Reassigning the semantic tokens
@media (prefers-color-scheme: dark) {
:root {
--color-bg: var(--neutral-900);
--color-text: var(--neutral-50);
--color-border: var(--neutral-700);
--color-link: var(--primary-100);
}
}Each token points at a different step on the same scale. The light theme uses the lightest neutral for the background and the darkest for text. The dark theme swaps them. Rules such as body { background: var(--color-bg); } need no edits.
Declare support with color-scheme
Setting your own colours does not change the browser's built-in elements, such as form controls and scrollbars. Tell the browser that the page supports both themes:
:root {
color-scheme: light dark;
}With this, the browser draws its own widgets to match the active theme. Without it, you may see a bright scrollbar beside a dark page.
Do not simply invert the palette
Swapping light and dark is a starting point. A few adjustments usually follow.
- Avoid pure black and pure white.
--neutral-900has a lightness of 12%, which is softer than black. Large areas of white text on pure black can look harsh and cause glare. - Lower the saturation of bright colours. Saturated colours on a dark surface appear to vibrate. Use a lighter, less saturated step of the same hue.
- Show depth with lighter surfaces. Shadows are hard to see on dark backgrounds. A raised card is usually a slightly lighter neutral than the page behind it.
- Pick lighter steps for links and buttons. The light theme uses
--primary-700for links. On a dark background the darker shade would be hard to read, which is why the example maps--color-linkto--primary-100.
Recheck contrast in the dark theme
Passing contrast in the light theme says nothing about the dark one. Every pair has to be tested again, using the ratio function from the contrast chapter. The thresholds are the same: 4.5:1 for normal text, and 3:1 for large text and UI components.
function channel(value) {
const s = value / 255;
return s <= 0.04045 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
}
function luminance([r, g, b]) {
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
}
function contrastRatio(colorA, colorB) {
const a = luminance(colorA);
const b = luminance(colorB);
const lighter = Math.max(a, b);
const darker = Math.min(a, b);
return (lighter + 0.05) / (darker + 0.05);
}
// Example: white text on a pure black background
console.log(contrastRatio([255, 255, 255], [0, 0, 0])); // 21Convert each dark token pair from HSL to RGB, then run it through this function. The accent colour needs particular care. An orange that works as a highlight on a light page may need a different step to stay visible on a dark surface.
Letting the user choose
The media query follows the system setting. Some sites also add a toggle. A common approach is to put the dark values under a data attribute as well, so the user's choice overrides the system preference.
:root[data-theme="dark"] {
--color-bg: var(--neutral-900);
--color-text: var(--neutral-50);
--color-border: var(--neutral-700);
--color-link: var(--primary-100);
}You then set data-theme on the html element with JavaScript and save the choice in localStorage. The duplicated values are the cost of this approach, so keep them in sync.
Common errors
| Error | Result | Fix |
|---|---|---|
| Components use raw shades such as `--neutral-900` | Dark mode needs edits in many rules | Use semantic tokens only |
| No `color-scheme` declaration | Light scrollbars and form controls on a dark page | Add `color-scheme: light dark` |
| Contrast checked in light mode only | Unreadable text in dark mode | Retest every pair |
| Pure white text on pure black | Glare and eye strain | Use `--neutral-50` on `--neutral-900` |
Exercise
A button uses background: var(--color-button-bg) and the text colour var(--neutral-50) written directly in its rule. After you add the dark theme, the button looks wrong. Which part of the rule is the likely problem, and how would you fix it?
Show answer
The text colour uses a raw palette shade, so it does not respond to the theme and may fail contrast against the dark-theme button background. Add a semantic token such as --color-button-text, assign it a value in both themes, and use var(--color-button-text) in the rule. Then calculate the contrast ratio for the pair in each theme and check it is at least 4.5:1.
Exercise
Why does the dark theme in this chapter map --color-link to --primary-100 instead of keeping --primary-700?
Show answer
--primary-700 is a dark shade (34% lightness), so it has low contrast against the dark --neutral-900 background. --primary-100 is a light shade of the same hue that stays readable and keeps the link recognisable as part of the primary colour.