Sub topic 3
Contrast and color: making the cue visible to everyone
This chapter assumes you can write basic HTML and CSS, including selectors, the box model, hex colors and pseudo-classes like :hover. It also assumes you can open your browser's developer tools. The previous chapters covered how an element signals that it can be clicked (affordances and signifiers) and when to use a button or a link. Here we make those signals visible to people with low vision, color vision deficiency, glare on their screen, or an unusual display setup.
Why color alone is not enough
Many clickable styles rely on one cue: a blue word, a green rectangle. That cue fails in several common situations:
- About 1 in 12 men and 1 in 200 women have some form of color vision deficiency. Two colors that look different to you can look nearly identical to them.
- Low vision, aging eyes, and small screens reduce the contrast people can perceive.
- Sunlight on a phone washes out light, subtle colors.
- Some operating systems offer a forced-colors mode (Windows High Contrast is one). It replaces your colors with a small user-chosen palette.
The fix is not to find a perfect color. Pair color with a second cue that does not depend on hue, such as an underline, a border, a shape, or a change in weight. Then make sure each cue has enough contrast against what is next to it.
What contrast ratio measures
A contrast ratio compares the relative luminance (perceived brightness) of two colors. It runs from 1:1 (identical) to 21:1 (black on white). The formula is:
ratio = (L_lighter + 0.05) / (L_darker + 0.05)Relative luminance L is computed from the red, green and blue channels. The channels are first linearized, because screen values are not proportional to emitted light. Green counts the most and blue the least. Hue does not matter in the formula, only brightness. Two colors with the same luminance, such as a red and a green, give a ratio near 1:1 even though they look very different to most people.
You can compute this yourself. The script below runs in Node.js with no dependencies. It accepts six-digit hex colors only.
function channel(value) {
const s = value / 255;
return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
}
function luminance(hex) {
const n = parseInt(hex.replace("#", ""), 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 [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
return (hi + 0.05) / (lo + 0.05);
}
const checks = [
["Link on white page", "#1d4ed8", "#ffffff"],
["Link against body text", "#1d4ed8", "#1f2937"],
["Gray placeholder on white", "#9ca3af", "#ffffff"],
["White label on light blue button", "#ffffff", "#93c5fd"],
];
for (const [name, fg, bg] of checks) {
console.log(`${name}: ${contrast(fg, bg).toFixed(2)}:1`);
}The output is close to 6.7, 2.2, 2.5 and 1.8, in that order. Keep these four results in mind. Each one illustrates a different rule below.
The three WCAG requirements that apply
The Web Content Accessibility Guidelines (WCAG) are the standard most accessibility laws and policies point to. Three success criteria matter for clickable elements. Level AA is the usual target.
| Criterion | What it covers | Requirement |
|---|---|---|
| 1.4.3 Contrast (Minimum) | Text and images of text, including button labels and link text | 4.5:1, or 3:1 for large text (at least 24px, or 18.66px bold) |
| 1.4.11 Non-text Contrast | Visual information needed to identify a control and its states, such as a button border or a focus ring | 3:1 against adjacent colors |
| 1.4.1 Use of Color | Any information conveyed only by color | Provide another visual cue |
Criterion 1.4.1 has a specific consequence for links inside a paragraph. If a link is distinguished from surrounding text only by color, that color needs 3:1 contrast against the surrounding text. It also needs a non-color cue on hover and focus. Most designs skip both conditions by keeping the underline.
Applying the numbers to links
In the script, the blue #1d4ed8 on a white page scores about 6.7:1. That passes the 4.5:1 text requirement easily. Against dark gray body text #1f2937, though, the same blue scores only about 2.2:1. Two dark colors sit close together in brightness. If you removed the underline, the link would fail 1.4.1 and many readers could not pick it out of the sentence.
This is why the link styles from the spine keep the underline:
a {
color: #1d4ed8;
text-decoration: underline;
text-decoration-thickness: 1px;
text-underline-offset: 0.15em;
}The underline is the second cue, and it does not depend on hue. It also survives forced-colors mode, which recolors the text but keeps the decoration.
Applying the numbers to buttons
A button has two things to check: the label against the button fill, and the button shape against the page.
The primary button in the spine uses white text on #1d4ed8. That is also about 6.7:1, so the label passes. Now compare the failure case from the script. White text on light blue #93c5fd scores about 1.8:1. The label is hard to read for almost everyone. A common fix is to keep the light fill but use dark text. Dark navy on that light blue passes comfortably.
The button shape is a separate question. Criterion 1.4.11 requires that visual information needed to identify a control reaches 3:1. Think about a light gray button on a white page. If the label alone tells users it is a button, the shape contrast may not be required. In practice, a visible edge is a strong signifier, so most designs include one. For a secondary button, the 2px border in #1d4ed8 provides it:
.btn--secondary {
background: transparent;
color: #1d4ed8;
border: 2px solid #1d4ed8;
}The border is also your safety net in forced-colors mode. Browsers in that mode usually discard background colors. A button defined only by its fill can lose its shape and look like plain text. A real border keeps the outline.
Contrast for states
Every state you add has to pass on its own. A common mistake is checking only the default state.
- Hover: the text must still reach 4.5:1 against the hover background. In the spine,
#ffffffon#1e40afis darker than the default and passes. A hover style that lightens a button toward white can drop below the threshold. - Focus: the focus ring needs 3:1 against the adjacent colors. The
outline: 3px solid #1d4ed8with a 3px offset sits against the page background, not against the button, so it is checked against the page. - Visited links: the visited color, for example
#6d28d9, must also pass 4.5:1 on its background. It must remain distinguishable from unvisited links too, but WCAG does not set a ratio for that. - Disabled controls: WCAG exempts inactive components from the contrast rules. The exemption does not excuse unreadable disabled buttons, because people still need to read what is unavailable. A later chapter covers this.
Text on images and gradients
Backgrounds that vary make a single ratio meaningless. If a button or link sits on a photo or gradient, measure against the worst-case spot behind the text. A reliable approach is to put the label on a solid fill, or add a solid or semi-opaque panel behind it. Do not rely on a text shadow to rescue a weak ratio.
Adapting to user preferences
Users can tell the browser how they want to see your page. CSS media queries let you respond. Here is a small, runnable example:
:root {
--link: #1d4ed8;
--link-visited: #6d28d9;
--page: #ffffff;
}
@media (prefers-color-scheme: dark) {
:root {
--link: #93c5fd;
--link-visited: #c4b5fd;
--page: #111827;
}
}
@media (prefers-contrast: more) {
:root {
--link: #1e3a8a;
}
}
body {
background: var(--page);
}
a {
color: var(--link);
text-decoration: underline;
}
a:visited {
color: var(--link-visited);
}
@media (forced-colors: active) {
a {
color: LinkText;
}
a:visited {
color: VisitedText;
}
}Three points about this code:
- Dark mode needs its own palette. Do not just reuse the light-mode blue, because
#1d4ed8on a near-black page has poor contrast. Re-run the calculator for every pairing you ship. - In forced-colors mode, system keywords like
LinkTextandVisitedTextmap to the colors the user chose. Usually you can leave the browser defaults alone. The block shows what the browser does and is where you would add overrides when needed. - The
prefers-contrast: moreblock is optional. It is useful if your default palette is only just above the threshold.
Common mistakes
- Checking contrast only for text and forgetting the border and the focus ring.
- Using a placeholder-style gray such as
#9ca3af(about 2.5:1 on white) for link or button text. - Removing underlines and relying on a blue that is close in brightness to the body text.
- Signaling an error or a selected state only with red versus green.
- Trusting a color picker's "looks fine" preview on a bright, high-quality monitor.
- Forgetting that opacity lowers contrast. A button at
opacity: 0.6, as used in the disabled example later, mixes its colors with the background and can fail.
Check your work
You can verify the ratios without writing code. In Chrome and Edge DevTools, inspect an element and click its color swatch. The picker shows the contrast ratio and marks the AA and AAA thresholds. Firefox's accessibility inspector has a contrast check as well. To simulate color vision deficiency, open Chrome DevTools, go to Rendering, and choose a vision deficiency to emulate. If your links and buttons are still recognizable under every setting, your cues are not relying on color alone.
Exercise
A paragraph contains links colored #1d4ed8 on a white page. The body text is #1f2937. A designer proposes removing the underline from links. The link color against white scores 6.7:1, and against the body text it scores 2.2:1. Does the proposal pass WCAG 1.4.1 as it stands? What would you change?
Show answer
No. Without an underline, color is the only cue, so the link color must be at least 3:1 against the surrounding body text. It is only about 2.2:1, so it fails. The 6.7:1 ratio against white is irrelevant to this rule, because it measures text readability, not distinctness from neighboring text. Keep the underline. Alternatively, darken or change the link color until it reaches 3:1 against the body text, and add an underline on hover and focus.
Exercise
A secondary button has a transparent background, a 1px light gray border (#d1d5db) and blue text that passes 4.5:1. A user in forced-colors mode reports it looks like plain text. Why might that happen, and which rule from this chapter did the design neglect?
Show answer
Forced-colors mode replaces most author colors, and the border here may fail to give the button a clear shape. The design never made the control's boundary a strong visual cue. WCAG 1.4.11 asks for 3:1 contrast on the parts needed to identify a control, and a pale gray border on white is far below that. Use a thicker border in a darker color, such as the 2px #1d4ed8 border in the spine, and check that it is a real border, not only a background.