Sub topic 10
Checking contrast and accessibility
A dark theme can look right and still be hard to read. This chapter covers how to measure contrast, which thresholds apply, and which parts of a theme people usually miss. It assumes you can already switch themes with custom properties and prefers-color-scheme, and that you can read basic JavaScript.
What contrast ratio measures
The Web Content Accessibility Guidelines (WCAG) define contrast as a ratio between the relative luminance of two colors. Luminance is the perceived brightness of a color, from 0 (black) to 1 (white). The ratio is:
(L_lighter + 0.05) / (L_darker + 0.05)The result runs from 1:1 (identical colors) to 21:1 (black on white). The formula is symmetric, so light text on a dark background and dark text on a light background with the same two colors give the same ratio. Your dark theme does not need a different rule. It needs the same rule applied again to every pair of colors.
| Content | WCAG AA minimum | WCAG AAA |
|---|---|---|
| Normal text | 4.5:1 | 7:1 |
| Large text (18pt, or 14pt bold, and up) | 3:1 | 4.5:1 |
| UI components and graphics (borders of inputs, icons, focus rings) | 3:1 | no separate level |
| Disabled controls, logos, decorative content | exempt | exempt |
AA is the usual target. Treat the exemptions with care: a control that looks disabled but is actually usable still needs to be readable.
Why dark themes break contrast
Contrast failures in dark mode tend to come from the same few causes:
- Reusing light-theme accent colors. A saturated blue that works on white often has too little contrast on a dark surface.
- Opacity-based text. A rule like
color: rgb(255 255 255 / 0.5)produces a color that depends on whatever is behind it. Its contrast changes from one surface to the next. - Muted text. Secondary text, placeholders, captions, and timestamps are usually the first colors to drop below 4.5:1.
- Borders and dividers. A dark gray border on a dark gray card can fall below the 3:1 requirement for component boundaries.
- Hover, active, and visited states. These are rarely checked, so they fail quietly.
Calculate it yourself
You can compute contrast without a tool. This Node.js script implements the WCAG formula and tests several pairs from the palette used earlier in this tutorial.
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(hex) {
const n = parseInt(hex.slice(1), 16);
const r = (n >> 16) & 255;
const g = (n >> 8) & 255;
const b = n & 255;
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
}
function contrast(a, b) {
const la = luminance(a);
const lb = luminance(b);
const lighter = Math.max(la, lb);
const darker = Math.min(la, lb);
return (lighter + 0.05) / (darker + 0.05);
}
const pairs = [
["body text, dark", "#e6e6e6", "#121212", 4.5],
["link, dark", "#8ab4ff", "#121212", 4.5],
["light-theme link on dark", "#0b5fff", "#121212", 4.5],
["link, light", "#0b5fff", "#ffffff", 4.5],
];
for (const [name, fg, bg, min] of pairs) {
const ratio = contrast(fg, bg);
const verdict = ratio >= min ? "pass" : "FAIL";
console.log(`${name}: ${ratio.toFixed(2)}:1 ${verdict} (needs ${min}:1)`);
}Save it as contrast.mjs and run node contrast.mjs. The body text comes out at about 15:1 and the dark link at about 9:1, so both pass. The light-theme blue #0b5fff on #121212 comes out at about 3.7:1, which fails for normal text. On white the same blue reaches about 5.1:1. This is why the earlier examples define a separate, lighter --link for dark mode.
You can run this check in a build step or a unit test over your token values, so a palette change that breaks contrast fails the build.
Check every token pair
Contrast belongs to a pair of colors, not to a single color. For each theme, list which foreground tokens appear on which background tokens, then test each pair.
| Foreground token | Background token | Minimum |
|---|---|---|
| `--text` | `--bg` | 4.5:1 |
| `--text` | `--surface` (cards, panels) | 4.5:1 |
| `--text-muted` | `--bg` and `--surface` | 4.5:1 |
| `--link` | `--bg` and `--surface` | 4.5:1 |
| `--border-input` | `--bg` | 3:1 |
| `--focus` | `--bg` and `--surface` | 3:1 |
| Text on `--accent` buttons | `--accent` | 4.5:1 |
The raised surface matters most. Dark themes often make cards slightly lighter than the page to show depth. That slightly lighter background lowers the contrast of all text placed on it, so test text against the lightest surface it can appear on.
Do not rely on color alone
WCAG also requires that color is not the only way to convey information. In dark mode, this matters for:
- Links inside body text. If you remove the underline, the link must still differ from surrounding text by 3:1 and gain a non-color cue on hover and focus.
- Form errors. Pair a red border with an icon or a text message.
- Charts. Give series different shapes, line styles, or labels, because red and green lose separation for people with color vision differences.
Focus indicators
Keyboard focus rings are a common dark-mode failure. A dark outline on a dark page is nearly invisible. Make the focus color a token and give it a value for each theme.
:root {
color-scheme: light dark;
--bg: #ffffff;
--text: #1a1a1a;
--focus: #0b5fff;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
--text: #e6e6e6;
--focus: #8ab4ff;
}
}
body {
background: var(--bg);
color: var(--text);
}
:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 2px;
}Use :focus-visible so the ring appears for keyboard users and not on every mouse click. Do not write outline: none unless you replace it with something at least as visible.
Respect `prefers-contrast`
Some users ask their operating system for higher contrast. The prefers-contrast media query lets you respond. You can combine it with the color scheme query to strengthen the dark theme.
@media (prefers-color-scheme: dark) and (prefers-contrast: more) {
:root {
--text: #ffffff;
--bg: #000000;
--link: #b3d0ff;
--border-input: #ffffff;
}
}This is an opt-in override. The default dark theme stays softer, which avoids the harshness of pure white on pure black for most users, and people who need more contrast get it.
Forced colors mode
Windows contrast themes and similar settings turn on forced colors mode. The browser replaces your colors with a small set of system colors, and your custom properties are largely ignored. This is intended behavior. Things that usually go wrong:
- Background images and gradients that carried meaning are removed, so a button that was only a colored rectangle can lose its boundary.
- Box shadows are removed, so shadow-only focus rings or borders disappear.
Give controls a real border, which the browser keeps and recolors. If you need an element to keep a visible edge only in this mode, use a transparent border, which forced colors turns into a visible one.
.button {
border: 2px solid transparent;
background: var(--accent);
color: var(--on-accent);
}
@media (forced-colors: active) {
.button {
border-color: ButtonText;
}
}Avoid using forced-colors: none to fight the mode. Let the user's palette win.
Dark palette checks beyond the ratio
A passing ratio does not guarantee comfort. Check these as well:
- Pure white on pure black gives 21:1, which passes, but the extreme brightness difference can make text appear to glow or smear (halation), especially for people with astigmatism. Off-white on near-black, as in the earlier examples, avoids this.
- Highly saturated colors on dark backgrounds can vibrate visually. Desaturate and lighten accent colors for the dark theme instead of reusing the light values.
- Large text has a lower threshold, but body-sized text in a muted color still needs 4.5:1.
- Text over images needs its own check. If text sits on a photo, add a solid or semi-opaque scrim behind it, and test against the brightest part of the photo.
Test with real tools
Automated checks cover pairs you list, but they miss pairs you forgot. Combine several methods:
- In Chrome or Edge DevTools, select an element and open the Styles pane. The color picker shows the contrast ratio against the computed background and marks the AA and AAA lines.
- In the Rendering panel, emulate
prefers-color-scheme: dark,prefers-contrast: more, andforced-colors: active. You can also emulate vision deficiencies such as protanopia from the same panel. - Run an automated audit such as Lighthouse or axe in each theme. Run it twice, once per theme, because the audit only sees the colors that are active at that moment.
- Tab through the page in both themes and confirm that the focus ring is always visible.
Common mistakes
- Testing only the default text color and ignoring muted text, placeholders, and disabled-looking links.
- Using the same accent color in both themes.
- Using alpha-based colors, then checking contrast against only one background.
- Removing the underline from links and relying on color alone.
- Setting
outline: noneon focus. - Checking the light theme thoroughly, then assuming the dark theme inherits the result.
Exercise
A dark theme uses --text-muted: #8a8a8a on a card background of #1e1e1e. A reviewer says it fails AA for normal text. What would you do to fix it, and how would you confirm the fix?
Show answer
Lighten the muted text color (for example toward #a8a8a8 or lighter) until the pair reaches at least 4.5:1, since the text is normal size. Do not darken the card, because that would affect every other pair on that surface. Confirm by running the pair through a contrast function like the one in this chapter or the DevTools color picker. Then re-check the muted text on every other surface it appears on, such as the page background.
Exercise
Your site uses a blue focus ring, #0b5fff, in both themes. Why is it a problem in dark mode, and how do you correct it using the tokens from this chapter?
Show answer
On a #121212 background the ring has only about 3.7:1 contrast, and it would be lower on lighter dark surfaces. That is close to the 3:1 minimum and easy to miss. Define --focus as #0b5fff in the light theme and a lighter value such as #8ab4ff in the dark theme. Use outline: 3px solid var(--focus) on :focus-visible, and test the ring against both --bg and --surface.