Sub topic 10
Checking contrast and accessibility
This chapter assumes you can write CSS custom properties, know the prefers-color-scheme media query, and have a light and dark set of color tokens like the ones built earlier in this tutorial. Here you will check that both themes stay readable for people with low vision, and you will respect other user preferences.
Why contrast needs checking in both themes
A palette that passes in light mode can fail in dark mode. Colors that look fine next to each other on a white page often lose separation on a near-black page. Each theme is a separate design, so each one needs its own check.
The Web Content Accessibility Guidelines (WCAG) measure contrast as a ratio between 1:1 (no difference) and 21:1 (black on white). The ratio compares the relative luminance of two colors, which is a measure of how light each one appears.
| Content | Minimum ratio (WCAG AA) |
|---|---|
| Normal body text | 4.5:1 |
| Large text (about 24px, or 19px bold) | 3:1 |
| UI components and icons, such as input borders and focus rings | 3:1 |
Level AAA asks for 7:1 for normal text. Aim for it on long-form reading when you can.
Measuring a ratio
You can check ratios in browser developer tools. The color picker in Chrome and Edge shows the ratio for a text color against its background. Online contrast checkers do the same job. You can also compute it yourself, which helps you understand what the tools report.
function channel(value) {
const c = value / 255;
return c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
}
function luminance(hex) {
const n = parseInt(hex.replace("#", ""), 16);
const r = channel((n >> 16) & 255);
const g = channel((n >> 8) & 255);
const b = channel(n & 255);
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
function contrast(foreground, background) {
const a = luminance(foreground);
const b = luminance(background);
const lighter = Math.max(a, b);
const darker = Math.min(a, b);
return (lighter + 0.05) / (darker + 0.05);
}
console.log(contrast("#e6e6e6", "#121212").toFixed(2)); // dark theme text
console.log(contrast("#8ab4ff", "#121212").toFixed(2)); // dark theme link
console.log(contrast("#0b5fff", "#ffffff").toFixed(2)); // light theme linkRun it in Node.js or a browser console. The dark-theme text and link colors from earlier chapters pass AA comfortably, and the light-theme link passes at roughly 5:1. If you change a token, rerun the check for every pair it appears in.
Checking every pair
Test each combination that really occurs on the page, not just the main text color. The usual pairs are:
- Body text on the page background.
- Muted or secondary text on the page background and on cards.
- Links on every surface they appear on.
- Button text on the button background, including hover and disabled states.
- Borders, icons, and focus outlines against their surroundings.
Because your colors live in custom properties, you can keep the list of pairs in one place and test it automatically with a script like the one above.
Avoid pure white on pure black
Maximum contrast is not always the most readable. Pure white (#ffffff) text on pure black (#000000) produces glare, and some readers see letters blur or smear. The palette in this tutorial uses #e6e6e6 on #121212, which keeps the ratio well above 4.5:1 and softens the edge.
Do not rely on color alone
Links that differ from body text only by color are hard to spot for people with color vision deficiencies. Keep the underline, or add another cue. Error states should pair color with an icon or text. This rule is independent of the theme, but dark palettes often reduce the color differences, so it matters more there.
Keep focus visible
Keyboard users depend on the focus outline. A dark blue outline that works on white can disappear on a dark background. Give it a token, so each theme can choose a visible value.
:root {
--focus: #0b5fff;
}
@media (prefers-color-scheme: dark) {
:root {
--focus: #8ab4ff;
}
}
:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 2px;
}The outline must reach 3:1 against the colors next to it.
Respect related preferences
Users can also ask for more contrast or less motion. Both are media queries, and you can combine them with your theme tokens.
@media (prefers-contrast: more) {
:root {
--text: #ffffff;
--bg: #000000;
}
}
@media (prefers-reduced-motion: reduce) {
* {
transition: none !important;
}
}If your toggle fades colors between themes, the reduced-motion rule turns that fade off for people who asked for less animation.
Common mistakes
- Checking only the light theme, then shipping an unreadable dark one.
- Testing text colors but not borders, icons, and focus rings.
- Using opacity to dim text. The resulting color depends on the background, so the ratio changes between themes. Use a solid token instead.
- Assuming a brand color works in both themes. Often it needs a lighter variant on dark backgrounds.
Exercise
A dark theme uses the text color #777777 on the background #121212. Muted captions at 14px use this pair. Will it pass WCAG AA, and how would you check it and fix it?
Show answer
Check it with the contrast() function above or the developer tools color picker. The ratio is about 4.3:1, just under the 4.5:1 required for normal-size text, so it fails. Fix it by lightening the caption token, for example to #8c8c8c or lighter, and then rerun the check for every surface where captions appear.