Sub topic 11
Adapting the palette for dark mode
A dark theme is a second mapping from the same palette to the same interface roles. It is not a second palette, and it is not a colour inversion. This chapter shows how to build that mapping on the token layers from the previous chapters, and where it usually goes wrong.
What you need before starting
This chapter assumes you have:
- The palette from earlier chapters, with primary, neutral, and accent scales written as HSL custom properties.
- A semantic layer such as
--color-bgand--color-textthat components use instead of raw shades. - The ability to calculate a WCAG contrast ratio. The JavaScript
contrastRatiofunction from the contrast chapter is used here. - Basic knowledge of media queries.
Why inverting colours does not work
The simplest idea is to swap light and dark: white becomes black and every colour is flipped. This produces poor results for four reasons.
- Brand colours change meaning. An inverted blue turns orange, so the brand is no longer recognisable.
- Contrast does not carry over. A pair that passes in the light theme can fail in the dark one. A mid-tone blue that is readable on near-white can be hard to read on near-black.
- Depth changes direction. Light interfaces suggest elevation with shadows. Shadows are barely visible on dark surfaces, so dark interfaces use lighter surfaces for raised elements.
- Saturated colours look louder. A fully saturated colour on a dark background appears to glow and can tire the eye.
So you keep the hues and re-pick the lightness and saturation for each role.
The structure that makes it cheap
In the token chapter you built two layers. The palette layer holds raw steps such as --primary-500. The semantic layer holds roles such as --color-text. Components only use the semantic layer.
Dark mode changes which palette step each role points at. The components and the palette stay as they are.
This is why the semantic layer is worth building early. Without it, you would search every component for raw shades and change them one by one.
Choosing dark-theme values
Treat each semantic role separately and ask what it needs to do.
Surfaces
Use a very dark, slightly tinted neutral for the page background. Pure black (#000) works against a bright monitor but causes problems:
- White text on pure black has a contrast ratio of 21:1. That is the maximum, and it can look harsh and cause text to appear to bloom.
- There is no darker colour left for sunken areas such as input fields.
- You cannot show elevation by getting lighter.
The palette's --neutral-900 (hsl(220 20% 12%)) is a better base. It keeps the same blue tint as the light-theme neutrals, so both themes feel related.
Raised surfaces such as cards, menus, and dialogs should be slightly lighter than the page. This needs one more neutral step than the light theme used. Add --neutral-800 to the scale.
Text
Avoid pure white for body text. Use a near-white step such as --neutral-50, which is the same step the light theme uses for its background. Reusing the same step on both sides keeps the two themes consistent.
Borders
Borders in the light theme are a light grey on a white surface. In the dark theme they should be a dark grey on a dark surface, so use a mid-dark neutral such as --neutral-700. A border only needs to be visible. It does not need to stand out.
Links and primary colours
This is the role that fails most often. Take the primary shade used for buttons in the light theme, --primary-500 (hsl(220 80% 50%)). On the dark page background, --neutral-900, its contrast ratio is about 3.3. That is fine for a large UI shape such as a button outline (threshold 3), but it fails for text (threshold 4.5).
So a link cannot reuse the button colour. In the dark theme, links move to a lighter step, such as --primary-100. If you need a middle step, add something like --primary-300 to the scale and check it with the contrast function.
Filled buttons are a separate case. A button with a --primary-500 background and white text has a contrast of roughly 5, so it passes in both themes. Its background can stay the same, and it also needs a visible edge against the dark page.
Saturation
Bright, saturated colours vibrate against dark backgrounds. A common adjustment is to lower saturation by 10 to 20 points for the dark-theme version of a colour, and raise lightness a little. Because the palette is in HSL, this is easy: the hue stays the same and you change two numbers.
Writing the dark theme
Here is a complete, runnable stylesheet. It repeats the palette so you can try it as one file, and adds the extra --neutral-800 surface step.
:root {
color-scheme: light dark;
/* Palette layer */
--primary-100: hsl(220 80% 94%);
--primary-500: hsl(220 80% 50%);
--primary-700: hsl(220 80% 34%);
--neutral-50: hsl(220 15% 97%);
--neutral-300: hsl(220 12% 80%);
--neutral-700: hsl(220 15% 28%);
--neutral-800: hsl(220 18% 18%);
--neutral-900: hsl(220 20% 12%);
--accent-500: hsl(35 95% 50%);
/* Semantic layer: light theme */
--color-bg: var(--neutral-50);
--color-surface: #fff;
--color-text: var(--neutral-900);
--color-border: var(--neutral-300);
--color-link: var(--primary-700);
--color-button-bg: var(--primary-500);
--color-highlight: var(--accent-500);
}
@media (prefers-color-scheme: dark) {
:root {
--color-bg: var(--neutral-900);
--color-surface: var(--neutral-800);
--color-text: var(--neutral-50);
--color-border: var(--neutral-700);
--color-link: var(--primary-100);
}
}
body {
background: var(--color-bg);
color: var(--color-text);
}
a {
color: var(--color-link);
}
.card {
background: var(--color-surface);
border: 1px solid var(--color-border);
}
.button {
background: var(--color-button-bg);
color: #fff;
}Notice what the media query leaves out. --color-button-bg and --color-highlight are not reassigned, because --primary-500 and --accent-500 already work on both backgrounds. Only roles that fail or look wrong need a new mapping.
The line color-scheme: light dark; tells the browser that the page supports both themes. The browser then adjusts the things you did not style, such as form controls, scrollbars, and the default canvas colour. Without it, a dark page can still show a bright white scrollbar and white text inputs.
Also add the meta tag
Put this in the HTML head so the browser picks the correct canvas colour before the stylesheet loads. It prevents a white flash on a dark page.
<meta name="color-scheme" content="light dark">Letting the user choose
prefers-color-scheme follows the operating system setting. Many sites also add a manual toggle, because a user might want a dark site on a light system, or the reverse. The usual pattern is a data-theme attribute on the root element that overrides the system setting.
/* Dark when the user picks it explicitly */
:root[data-theme="dark"] {
color-scheme: dark;
--color-bg: var(--neutral-900);
--color-surface: var(--neutral-800);
--color-text: var(--neutral-50);
--color-border: var(--neutral-700);
--color-link: var(--primary-100);
}
/* Dark when the system asks for it and the user has not chosen light */
@media (prefers-color-scheme: dark) {
:root:not([data-theme="light"]) {
color-scheme: dark;
--color-bg: var(--neutral-900);
--color-surface: var(--neutral-800);
--color-text: var(--neutral-50);
--color-border: var(--neutral-700);
--color-link: var(--primary-100);
}
}This works but repeats the same block twice, and the two copies can drift apart. If you are willing to rely on newer CSS, the light-dark() function lets you write both values on one line:
:root {
color-scheme: light dark;
--color-bg: light-dark(var(--neutral-50), var(--neutral-900));
--color-text: light-dark(var(--neutral-900), var(--neutral-50));
}light-dark() follows the computed color-scheme, so a manual toggle only needs to set color-scheme: light or color-scheme: dark on the root. Check browser support for your audience first. The function is available in current browsers but not in older ones.
Avoiding the flash on load
If you store the user's choice in localStorage, a script that sets data-theme after the page renders will cause a flash of the wrong theme. Run a tiny inline script in the head, before the stylesheet, that reads the stored value and sets the attribute immediately.
Common mistakes
- Redefining the palette in dark mode. If you change
--primary-500inside the media query, every use of it changes, including places that were already correct. Change semantic tokens instead. - Skipping the contrast check. Passing in light mode says nothing about dark mode. Run every text and UI pair for each theme. Use 4.5 for normal text and 3 for large text and UI components.
- Using shadows for elevation. A
box-shadowon a dark surface is almost invisible. Use a lighter surface step, and keep the shadow only as a secondary cue. - Forgetting non-text colours. Focus rings, chart lines, icons, and input borders must also meet the 3:1 threshold against their dark background.
- Leaving images and logos unchanged. A dark logo on a dark background disappears. Provide a light variant, or use a filter or an SVG that uses
currentColor. Large bright photographs can also feel glaring, so some sites dim them slightly. - Status colours that are too bright. Success, warning, and error colours from the semantic status chapter usually need a lighter, less saturated step for text on dark backgrounds, and a darker step for the tinted backgrounds of alerts.
Testing the dark theme
Review the dark theme the same way as the light one.
- Run the
contrastRatiofunction on every text and UI pair in the dark mapping and record the results. - Switch the operating system or browser DevTools to emulate
prefers-color-scheme: darkand click through every page type. - Check the states that are easy to miss: hover, focus, disabled, error, and selected.
- Look for hard-coded colours that bypass the tokens by searching the stylesheet for
#andrgb(. Any hit outside the palette definitions is a bug in the dark theme. - View the page with a colour-vision simulator, as in the final testing chapter.
Exercise
In the dark theme, --color-link is currently var(--primary-500), which has a contrast of about 3.3 against --neutral-900. Which token should replace it, and why does the button background not need the same change?
Show answer
Replace it with a lighter step such as --primary-100 (or an intermediate step like --primary-300 that you verify reaches at least 4.5). Link text is normal-size text, so it needs 4.5, and 3.3 fails. The button background is a UI shape with a 3:1 threshold, which 3.3 meets, and its white label has a contrast of roughly 5 against it.
Exercise
A teammate fixes dark mode by redefining --primary-500 inside the prefers-color-scheme: dark block. Name one problem with this and describe the better approach.
Show answer
It changes every place that uses --primary-500, including ones that already looked right, and it breaks the idea that palette steps are fixed values. The better approach is to leave palette steps alone and reassign only the semantic tokens, such as --color-link, so each role points at a step that suits the dark background.
Exercise
Why should you set color-scheme: light dark on :root as well as defining dark tokens?
Show answer
It tells the browser the page supports both themes, so unstyled parts such as form controls, scrollbars, and the default canvas colour follow the active theme. Without it, a dark page can still show white scrollbars and white inputs.