Sub topic 4
Contrast and Accessible Color Choices
This chapter assumes you already know hex, rgb() and hsl() notation, custom properties, and currentColor from the earlier chapters. It covers how contrast is measured, which ratios to meet, and how to build those checks into your color system.
Why contrast is a design constraint
Color choices decide whether people can read your page. Low-vision users, people with color-vision deficiencies, and anyone reading on a dim phone in sunlight all depend on enough difference between text and its background. About 1 in 12 men has some form of red-green color-vision deficiency. Hue alone is not a reliable signal for those users, but lightness difference is.
The standard most teams and laws refer to is WCAG 2.x (Web Content Accessibility Guidelines). It defines contrast as a number you can calculate and test, not a matter of taste.
How the contrast ratio is calculated
WCAG compares the relative luminance of two colors. Relative luminance is a value from 0 (black) to 1 (white) that approximates how bright a color appears to the eye. The calculation has three steps:
- Convert each sRGB channel to a 0 to 1 value, then linearize it. Values at or below 0.04045 are divided by 12.92. Larger values become
((c + 0.055) / 1.055) ** 2.4. - Combine the channels with weights that reflect the eye's sensitivity:
L = 0.2126 R + 0.7152 G + 0.0722 B. Green counts most and blue least. - Divide the lighter luminance plus 0.05 by the darker luminance plus 0.05.
The result ranges from 1:1 (identical colors) to 21:1 (black on white). You can run the same math yourself in Node.js:
function channel(value) {
const c = value / 255;
return c <= 0.04045 ? c / 12.92 : ((c + 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 contrast(a, b) {
const l1 = luminance(a);
const l2 = luminance(b);
const [hi, lo] = l1 >= l2 ? [l1, l2] : [l2, l1];
return (hi + 0.05) / (lo + 0.05);
}
const white = [255, 255, 255];
console.log(contrast([0, 0, 0], white).toFixed(2)); // 21.00
console.log(contrast([118, 118, 118], white).toFixed(2)); // 4.54 (#767676)
console.log(contrast([119, 119, 119], white).toFixed(2)); // 4.48 (#777777)The last two lines show how sharp the threshold is. #767676 is the lightest neutral gray that passes the 4.5:1 minimum on white. #777777 looks identical to most eyes and fails.
The thresholds to remember
| Content | Level AA | Level AAA |
|---|---|---|
| Normal text | 4.5:1 | 7:1 |
| Large text | 3:1 | 4.5:1 |
| UI components and graphics | 3:1 | not defined |
"Large" means at least 18pt (24 CSS px), or at least 14pt (about 18.66 px) when bold. Most projects target AA as the baseline and use AAA values where it costs nothing, such as for long-form reading.
The "UI components and graphics" row covers the visible boundary of an input, the icon that is the only label on a button, chart lines, and focus indicators. These must reach 3:1 against their neighbors.
Building contrast into your tokens
Contrast is a property of a pair of colors, so check pairs when you define your palette, not when you write each rule. The custom properties from the theming chapter are the right place for this. Record the measured ratio next to each token so that later edits stay honest.
:root {
--bg: #ffffff;
--text: #1a1a1a; /* about 17:1 on --bg */
--muted: #595959; /* about 7:1 on --bg */
--link: #0b5cad; /* about 6.7:1 on --bg */
--focus: #0b5cad; /* 3:1 or more against --bg */
}
body {
background-color: var(--bg);
color: var(--text);
}
.hint {
color: var(--muted);
}
a {
color: var(--link);
text-decoration: underline;
}
:focus-visible {
outline: 3px solid var(--focus);
outline-offset: 2px;
}Three habits keep this system reliable:
- Name tokens by role (
--text,--muted), not by appearance. Then a dark theme can change the values without renaming anything. - Keep a small set of approved pairs. If every text color is verified against every background it can sit on, new components can't introduce failures by accident.
- Include a focus color in the system. Browser default outlines can disappear against some backgrounds.
Common ways contrast fails
Gray text that is too light
Placeholder text, captions, and "muted" labels are the most frequent failures. Placeholder text counts as text, so ::placeholder needs 4.5:1 too. Many default placeholder colors fall below that.
Transparency and `opacity`
A color such as rgb(0 0 0 / 0.5) has no fixed contrast. The ratio depends on what is painted behind it, so you must calculate it on the blended result. The opacity property has the same effect on a whole element, including its text. Fading a disabled button with opacity: 0.4 is a common way to push text below the threshold. WCAG exempts disabled controls, but that is no excuse for text users still need to read.
Text over images and gradients
A single ratio cannot describe a background that changes. Put a solid or semi-opaque panel behind the text, or check the worst point of the image. Test the lightest area under dark text and the darkest area under light text.
Hover and state colors
Each state is its own pair. A hover color derived with color-mix() or a lighter oklch() shade can look fine and still fail against white text. Re-check every derived color. Moving lightness in oklch() is predictable, but the 4.5:1 rule is based on WCAG luminance, so a perceptual lightness value is not a substitute for measuring.
Do not rely on color alone
WCAG also requires that color is not the only way to convey information. Two cases come up constantly:
- Links inside body text. If you remove the underline, the link color must differ from the surrounding text by at least 3:1, and you need another cue on hover and focus. The simplest fix is to keep the underline.
- Form errors. A red border alone is invisible to some users. Add an icon or an explicit message such as "Email address is required".
A quick test is to view your page in grayscale. If any meaning disappears, add a non-color cue.
Respecting user preferences
Users can tell the browser whether they prefer a dark or high-contrast display. Your CSS can read both settings. Define the theme as tokens, then override the tokens inside media queries:
:root {
color-scheme: light dark;
--bg: #ffffff;
--text: #1a1a1a;
--muted: #595959;
--link: #0b5cad;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
--text: #e8e8e8;
--muted: #a8a8a8;
--link: #8ab4f8;
}
}
@media (prefers-contrast: more) {
:root {
--muted: var(--text);
}
}Dark themes need their own contrast checks. Don't just invert the light values. Very bright text on pure black can also cause a glare effect that some readers find tiring, which is why this example uses off-white on a near-black gray. The values above all exceed 4.5:1 against #121212, but measure again whenever you change them.
The color-scheme property tells the browser to render built-in controls and scrollbars to match, so you don't style those by hand.
Tools and their limits
- Browser DevTools. The color picker shows the contrast ratio and marks the AA and AAA lines.
- Lighthouse and axe. These audit a rendered page and flag text that fails. They test only what is visible on load.
- Your own script. The
contrast()function above can run in a build step against your token file and fail the build when a pair drops below its target.
WCAG 3 drafts propose a different model called APCA, which accounts for font size and weight and treats light-on-dark differently. It is not yet the legal standard, so keep using the WCAG 2.x ratios for compliance.
Summary
Contrast is measured, not guessed. Meet 4.5:1 for normal text, 3:1 for large text and UI parts, check every state and every theme, and never let color carry meaning by itself.
Exercise
A designer asks for #777777 caption text on a white background at 14px regular weight. Does it meet WCAG AA, and what is the smallest change that fixes it?
Show answer
It fails. #777777 on white is about 4.48:1, and 14px regular text counts as normal text, so it needs 4.5:1. Darken the gray to #767676 (4.54:1) or darker, such as #595959 (about 7:1) for extra margin.
Exercise
A button uses color: white on background-color: rgb(11 92 173 / 0.5) over a white page, and the text is hard to read. Why can't you state a single contrast ratio for this button, and what should you do?
Show answer
The semi-transparent background blends with whatever is behind it, so the real background color is the blended result and changes with the surface. Replace it with an opaque color, such as #0b5cad (about 6.7:1 against white text), or calculate the ratio on the composited color and verify it reaches 4.5:1.