Sub topic 9
Designing a Dark Palette That Is Comfortable to Read
What this chapter assumes
You have already built semantic color tokens as CSS custom properties and can override them inside a prefers-color-scheme: dark media query. This chapter is about which values to put in the dark set. It covers how to choose them, how to relate them to each other, and where the usual shortcuts fail. Formal contrast checking gets its own chapter, so here contrast appears only as a design constraint.
Why inverting colors fails
A common first attempt is to swap light and dark: white becomes black, black becomes white, and everything else is flipped. The result is technically a dark page, but it is often tiring to read, for these reasons:
- Pure white text on pure black has the maximum possible contrast, 21:1. Many readers see it as glaring, and thin letterforms seem to bleed into the dark background (a effect called halation, strongest for people with astigmatism).
- Saturated colors that look fine on white, such as a strong blue link, vibrate against a dark background.
- In a light theme, depth comes from shadows. Shadows are nearly invisible on a dark surface, so layered interfaces turn into a flat sheet.
A dark palette is a separate design with its own rules. The tokens keep the same names, and their values are chosen again for the new background.
Start from a surface scale
Pick a base background that is a very dark gray, not pure black. Then build a few slightly lighter steps for raised elements such as cards, menus, and dialogs. In a dark theme, lighter means closer to the user.
The diagram below shows the idea. Each layer sits one step lighter than the layer beneath it.
Keeping the steps small, around 4 to 8 percent in lightness, is enough for the eye to read the layering. If the steps are large, the page looks patchy.
A neutral gray is safe. You can also tint the whole scale slightly toward your brand hue, for example a blue-gray. Keep the tint low, because strongly colored backgrounds make every other color on the page harder to judge.
Choose text colors below pure white
Use an off-white for body text and a dimmer gray for secondary text. This gives you two levels of hierarchy without changing font weight or size.
| Role | Light theme | Dark theme | Notes |
|---|---|---|---|
| Primary text | `#1a1a1a` | `#e6e6e6` | About 15:1 on `#121212`, which is comfortable and still strong |
| Secondary text | `#555555` | `#a8a8b0` | Still above 4.5:1 on every surface step in this chapter |
| Disabled text | `#999999` | `#6b6b73` | Disabled controls are exempt from contrast rules, but keep them visible |
Do not make secondary text too dim. The most common failure in dark themes is gray text on a dark gray card that looks elegant on a calibrated monitor and unreadable on a laptop in daylight. Check secondary text against the lightest surface it can appear on, not only the page background.
Desaturate and lighten accent colors
Accent colors need two changes when they move to a dark background: higher lightness and lower saturation. A blue like #0b5fff is vivid on white. On #121212 it looks dim, and its saturation makes it appear to glow. A lighter, softer blue such as #8ab4ff has the same hue and reads clearly.
Apply the same adjustment to status colors:
- Red errors become a lighter, slightly pinker red.
- Greens become less neon.
- Yellows and oranges usually need the least change, because they are already light.
A useful workflow is to keep the hue the same and adjust lightness and chroma in a perceptual color space. CSS supports oklch() in current browsers, and it makes these adjustments predictable, because changing lightness does not shift the apparent hue the way it does in HSL. The example below uses oklch() for the accent family.
:root {
--accent: oklch(0.55 0.22 262);
--accent-text-on: #ffffff;
}
@media (prefers-color-scheme: dark) {
:root {
--accent: oklch(0.78 0.12 262);
--accent-text-on: #0b1020;
}
}Notice the second token. When the accent becomes light, the text that sits on top of it, such as the label on a filled button, must become dark. If you forget this, a button gets white text on a pastel blue and fails to be readable. Treat "text on accent" as its own token for this reason.
Replace shadows with borders and lighter surfaces
A box shadow on a dark page needs a very high opacity before anyone sees it, and then it looks heavy. Use these tools instead:
- A lighter surface step for raised elements, as described above.
- A hairline border made from translucent white, such as
rgba(255, 255, 255, 0.12). Because the border is translucent, it adapts to whichever surface is under it. - A shadow that is kept in addition, for overlays like dialogs, where separation from a dimmed page matters.
Define a --shadow token so that the light theme can keep its soft shadow while the dark theme uses a stronger one with a border.
A complete token set
This stylesheet puts the choices together. It extends the token approach from earlier chapters, so only the values change.
:root {
color-scheme: light dark;
/* surfaces */
--surface-0: #ffffff;
--surface-1: #f6f7f9;
--surface-2: #ffffff;
/* text */
--text: #1a1a1a;
--text-muted: #555555;
/* lines and depth */
--border: rgba(0, 0, 0, 0.12);
--shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
/* accent and status */
--accent: #0b5fff;
--accent-text-on: #ffffff;
--danger: #c62828;
}
@media (prefers-color-scheme: dark) {
:root {
--surface-0: #121212;
--surface-1: #1b1b1d;
--surface-2: #232326;
--text: #e6e6e6;
--text-muted: #a8a8b0;
--border: rgba(255, 255, 255, 0.12);
--shadow: 0 0 0 1px rgba(255, 255, 255, 0.06), 0 8px 24px rgba(0, 0, 0, 0.6);
--accent: #8ab4ff;
--accent-text-on: #0b1020;
--danger: #ff8a80;
}
}
body {
background: var(--surface-0);
color: var(--text);
}
.card {
background: var(--surface-1);
border: 1px solid var(--border);
box-shadow: var(--shadow);
border-radius: 8px;
padding: 1rem;
}
.menu {
background: var(--surface-2);
border: 1px solid var(--border);
}
.muted {
color: var(--text-muted);
}
.button-primary {
background: var(--accent);
color: var(--accent-text-on);
border: 0;
padding: 0.5rem 1rem;
border-radius: 6px;
}
.error {
color: var(--danger);
}In the light theme, --surface-2 is the same as --surface-0. That is fine. The token exists so that components can say "I am a menu" and the theme decides what that looks like. If you later want a tinted menu in the light theme, only one line changes.
If your site also has a manual toggle, put the same dark values in a :root[data-theme="dark"] rule as shown in the toggle chapter. Keep the two copies in sync, or generate one from the other with a build step, because a drift between them produces a page that looks different depending on how dark mode was turned on.
Edge cases to plan for
- Large flat areas of bright color. A full-width banner in your light-theme brand color will dominate a dark page. Give banners their own token and use a darker or less saturated variant.
- Text selection and focus rings. The default focus ring may be nearly invisible on a dark surface. Define
--focusas a light, saturated color and use it in:focus-visiblestyles. - Charts and data visualization. Series colors chosen for white often lose separation on dark. Test every series against the dark surface, and do not rely on color alone to distinguish them.
- Third-party widgets. Embedded content in an iframe has its own styles. If the widget does not support dark mode, put it on a light card instead of trying to invert it.
- Text on images. If text overlays a photo, add a scrim (a semi-transparent gradient) behind it. Do not trust that the photo will stay dark enough.
Common mistakes
| Mistake | Symptom | Fix |
|---|---|---|
| Pure `#000` background and `#fff` text | Glare, halation, smearing on scroll | Use `#121212`-style backgrounds and off-white text |
| Reusing light-theme accent colors | Colors glow or look muddy | Raise lightness and lower saturation |
| Forgetting text-on-accent | Unreadable button labels | Add a dedicated `--accent-text-on` token |
| Relying on shadows for depth | Cards merge into the page | Use lighter surfaces and translucent borders |
| Hard-coded colors in components | Pieces of the page stay light | Replace with tokens, and search the stylesheet for stray hex values |
| Testing only the page background | Muted text fails on cards | Check each text token on each surface |
Exercise
A card uses --surface-1 as its background and --text-muted for a caption. Your designer asks for a new menu that floats above the card. Which surface token should the menu use in the dark theme, why, and which two other properties help separate it visually from the card?
Show answer
The menu should use --surface-2, a step lighter than --surface-1, because in a dark theme elements that sit higher are shown with lighter surfaces. A translucent border such as rgba(255, 255, 255, 0.12) and a stronger shadow token help separate it, since shadows alone are hard to see on dark backgrounds. You should also confirm that --text-muted still has enough contrast on --surface-2, because it is now on a lighter background than the one you originally tested.
Exercise
A filled button uses --accent as its background and white text. In the dark theme you change --accent from #0b5fff to #8ab4ff. What breaks, and how do you prevent it in the token design?
Show answer
The white label now has low contrast against the light blue background and becomes hard to read. Prevent this by introducing a separate token such as --accent-text-on and giving it a dark value (for example #0b1020) in the dark theme, so the label color changes together with the accent.