Color Contrast and Accessibility: WCAG Ratios, contrast-color() and Forced Colors
Making color accessible: WCAG contrast ratios for text and interface parts, how the ratio is calculated, contrast-color(), not relying on color alone, text over images, forced colors mode with system colors, and prefers-contrast.
kvi5hnuCode Sprout team member9 min readOn this page
Color is one of the main ways a design communicates, but it fails if people can't see it. About one in twelve men and one in two hundred women have some color vision deficiency, many people have low vision, and everyone reads phones in bright sunlight. This article covers how to measure contrast, the CSS features that help, and how pages behave when users override colors entirely.
Contrast ratios
The Web Content Accessibility Guidelines (WCAG) measure contrast as a ratio between the relative luminance of two colors, from 1 (identical) to 21 (black on white). Version 2.2 sets these minimums:
| Content | Level AA | Level AAA |
|---|---|---|
| normal text | 4.5 | 7 |
| large text: at least 24px, or 18.66px bold | 3 | 4.5 |
| interface parts and meaningful graphics: input borders, icons, focus indicators, chart lines | 3 |
Level AA is the usual legal and practical target. Logos and purely decorative text are exempt, and so is the text of disabled controls.
Some examples, measured against white unless noted:
body { font-family: system-ui, sans-serif; display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.5rem; margin: 1rem; }
p { margin: 0; padding: 0.6rem 0.8rem; border-radius: 6px; background: white; border: 1px solid #ddd; }
b { float: right; font-weight: 600; }
Note the last two: a brand color that's excellent on white can fail on a dark background, so each theme needs its own checks (article 34).
How the ratio is calculated
Each color's relative luminance is computed from its linearized sRGB channels, weighted for how bright each looks: L = 0.2126 R + 0.7152 G + 0.0722 B. The ratio is (L1 + 0.05) / (L2 + 0.05), with the lighter color as L1. Green counts the most, which is why yellow and light green text are so often too pale.
You don't need to compute it by hand: browser DevTools show the contrast ratio in the color picker, flag low-contrast text in accessibility audits, and online checkers accept any CSS color.
The numbers aren't everything
- Thin and small fonts need more contrast. A 300-weight font at 4.5 is harder to read than a 400-weight one at the same ratio.
- Very high contrast isn't always better for long reading: pure black on pure white can feel harsh for some readers, which is why many designs use a very dark gray. Stay well above the minimum, though.
- WCAG's formula has known flaws, especially for light text on dark colors and for mid-tones. A proposed method, APCA, has been discussed for WCAG 3, but WCAG 3 is an early draft and hasn't adopted any contrast method. WCAG 2.2's ratios are the standard to meet.
contrast-color()
contrast-color(<color>) returns white or black, whichever contrasts more with the given color:
.tag {
background: var(--tag-bg);
color: contrast-color(var(--tag-bg));
}
That's ideal for components with user-chosen or data-driven colors, such as labels, avatars and calendar events. It's Baseline since April 2026.
It only picks between black and white, and it doesn't guarantee a ratio: on a mid-tone like #777777, black wins with about 4.7. The better of black and white always reaches at least about 4.6, so it passes AA for normal text, but on mid-tones it never reaches AAA's 7, and the result can look weak. 
The weakest labels are the mid-tones, #777777 and #e11d48, at 4.7:1: both pass AA, but neither black nor white can do better on them.
Avoid mid-tone backgrounds for small text. Earlier drafts let you pass candidate colors and a target ratio; those options were removed and may return in a later version.
Don't rely on color alone
WCAG also requires that color isn't the only way information is shown. A red border alone doesn't tell a colorblind user which field has an error, and a link that differs from text only by color is hard to find:
.field.invalid input { border-color: #b42318; border-width: 2px; }
.field.invalid .message::before { content: "⚠ "; } /* plus a text message */
a { text-decoration: underline; text-underline-offset: 0.15em; } /* underline links in text */
Charts need patterns, labels or different shapes as well as colors. Test with DevTools' vision deficiency emulation, which simulates several types of color blindness.
Text over images
Text on photos has unpredictable contrast. Add a scrim, a semi-transparent layer between the image and the text:
.hero {
background:
linear-gradient(rgb(0 0 0 / 0.55), rgb(0 0 0 / 0.2)),
url("harbor.jpg") center / cover;
color: white;
}
.caption { text-shadow: 0 1px 3px rgb(0 0 0 / 0.8); }
Always set a background color too, so the text is readable while the image loads or if it fails.

The scrim gives the text a predictable background. A shadow only darkens the edges of each letter, so contrast still depends on what's behind it.
Forced colors mode
Some users turn on a forced colors mode, such as Windows' contrast themes. The browser then replaces your colors with a small palette the user chose: backgrounds, text, links, buttons and borders all use system colors (article 34). It also removes box-shadow, text-shadow and background gradients (url() images stay), and may draw an opaque backplate behind text (Chromium does) so it stays readable.
Your job is to make sure nothing important disappears:
.button { border: 2px solid transparent; } /* invisible normally, visible in forced colors */
.toggle:focus-visible { outline: 2px solid transparent; box-shadow: 0 0 0 3px #f4c95d; }
/* the shadow vanishes in forced colors, but the outline becomes visible */
@media (forced-colors: active) {
.icon { fill: CanvasText; }
.badge { border: 1px solid; }
.selected { background: Highlight; color: HighlightText; }
}
- Transparent borders and outlines become visible, because the browser gives them a system color. That's the standard trick for buttons and focus indicators that use shadows or background colors only.
@media (forced-colors: active)lets you adjust details, using system colors such asCanvasText,LinkText,ButtonTextandHighlight.forced-color-adjust: noneopts an element out, for things like color swatches whose colors are the content. Use it rarely, and only with system colors or carefully chosen ones. It isn't in Safari, which has no forced colors mode.- System colors you set yourself are kept in forced colors mode; only other colors are replaced.
- SVG icons using
fill: currentColorfollow the forced text color automatically. Fixed SVG fills aren't replaced, so usecurrentColoror a system color.
User preferences for contrast
prefers-contrast reports whether the user asked the operating system for more or less contrast:
@media (prefers-contrast: more) {
:root { --text-muted: var(--text); --border: CanvasText; }
.card { border-width: 2px; }
}
The values are more, less, custom (a forced palette) and no-preference. Use more to strengthen subtle grays, thin borders and translucent backgrounds.
A contrast checklist
- Body text at least 4.5, large headings at least 3, ideally more.
- Input borders, icons and focus indicators at least 3 against what's next to them.
- Check every theme, and every state: hover, focus, visited, disabled, error.
- Never show information by color alone.
- Text over images has a scrim and a fallback background color.
- Test in forced colors mode, at 200% zoom, and with vision deficiency emulation.
What's current
- WCAG 2.2 is the current standard. APCA has been proposed for WCAG 3 but not adopted.
contrast-color()is Baseline since April 2026, and returns black or white only.forced-colorsandprefers-contrastmedia queries work in all browsers.forced-color-adjustis in Chrome and Firefox.
Common questions
What contrast does my body text need? At least 4.5 against its background for WCAG AA. Large text (24px, or 18.66px bold) needs 3.
Is #777777 on white accessible?
Just barely not: it's about 4.48. #767676 is about 4.54, the lightest neutral gray that passes AA on white.
Does contrast-color() make any color accessible?
No. It picks whichever of black or white contrasts more, which always passes AA (at least about 4.6), but mid-tone backgrounds can't reach AAA with either.
Why do my custom buttons disappear in Windows high contrast mode? They're probably defined only by background colors or shadows, which forced colors mode replaces or removes. Add a transparent border or outline: the browser makes it visible in forced colors.
Do placeholder text and disabled buttons need 4.5? Disabled controls are exempt. Placeholder text isn't, if it conveys information, so don't use pale placeholders as the only label.
Summary
- WCAG 2.2 AA requires 4.5 for normal text, 3 for large text, and 3 for interface parts and meaningful graphics.
- The ratio compares relative luminance. Check it in DevTools, and in every theme and state.
- Thin fonts need more contrast than the formula suggests.
contrast-color()returns black or white for a background, but can't reach AAA on mid-tones.- Don't convey information by color alone; underline links in text.
- Forced colors mode replaces your colors. Transparent borders and outlines keep shapes and focus visible, and
forced-colorsqueries let you adjust with system colors. prefers-contrast: morelets you strengthen subtle colors for users who ask.
Practice questions
Body text is #949494 on white, about 3. Which text sizes can use it at WCAG AA?
Answer
Only large text: at least 24px regular, or about 18.66px bold. Normal body text needs 4.5, so it needs a darker color, such as #767676 or darker.
Write CSS for a label whose background comes from --label-bg and whose text is automatically black or white.
Answer
.label { background: var(--label-bg); color: contrast-color(var(--label-bg)); }
A toggle button shows focus with box-shadow: 0 0 0 3px gold only. What happens in forced colors mode, and how do you fix it?
Answer
Shadows are removed in forced colors mode, so the focus indicator disappears. Add outline: 2px solid transparent; (with an offset if you like) in the focus styles: the browser gives the outline a visible system color in forced colors mode.
An error message is shown only by turning the input's border red. Name two improvements.
Answer
Add a text message that explains the error, ideally linked to the input with aria-describedby, and an icon or a thicker border, so the error doesn't depend on seeing red. Also check that the red border reaches 3 against the background.
Review quiz
Test yourself on Part 5 with the CSS Part 5 review quiz.
Previous: Light and Dark Themes: color-scheme, light-dark(), System Colors and accent-color
Next: The Box Model: Content, Padding, Border, Margin and box-sizing

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.