Skip to content
CSS-02Intermediate4 min read

Color and contrast

How contrast ratios are calculated, what WCAG asks for, and how to build palettes that pass in light and dark.

Updated

Text that is hard to read is a problem for everyone, not only for people with low vision. Glare on a phone, a dim laptop screen or tired eyes all reduce effective contrast. The Web Content Accessibility Guidelines give you a measurable target, and it takes seconds to check.

How contrast is measured

Contrast is the ratio between the relative luminance of two colors, from 1:1 for identical colors to 21:1 for black on white. Luminance weights green most heavily, because human eyes are most sensitive to it, and blue least.

contrast.js
// Relative luminance as defined by WCAG
function luminance([r, g, b]) {
  const channel = (value) => {
    const c = value / 255
    return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4
  }

  return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b)
}

export function contrastRatio(a, b) {
  const [light, dark] = [luminance(a), luminance(b)].sort((x, y) => y - x)
  return (light + 0.05) / (dark + 0.05)
}

contrastRatio([107, 114, 128], [255, 255, 255]) // 4.83

What WCAG asks for

ContentLevel AALevel AAA
Body text4.5:17:1
Large text: 24px, or 18.66px and bold3:14.5:1
Icons, input borders and focus indicators3:1No extra requirement
Disabled controls and pure decorationNo requirementNo requirement

Level AA is the target most laws and design systems use. Aim for it everywhere, and for AAA on long form reading where you can.

Check it live

Large heading text

Body text at 14 pixels. Small, regular weight text needs the most contrast to stay readable for everyone.

Contrast ratio

4.83:1

  • AA body textPass
  • AA large textPass
  • AAA body textFail
  • AAA large textPass
Contrast checkerPick a text and background color or start from a preset. The ratio and the pass or fail results update as you change either color.

Building palettes that pass

Choose colors by role, such as body text, muted text and links, and check every pair you actually use rather than every color in isolation. Perceptual color spaces like OKLCH make this easier: keep hue and chroma fixed, adjust only lightness, and contrast changes in predictable steps.

tokens.css
:root {
  --background: oklch(1 0 0);
  --text: oklch(0.2 0.008 270);
  --text-muted: oklch(0.5 0.014 270);
  --link: oklch(0.54 0.21 262);
}

.dark {
  --background: oklch(0.155 0.005 270);
  --text: oklch(0.95 0.004 270);
  --text-muted: oklch(0.71 0.012 270);
  --link: oklch(0.75 0.14 258);
}

Beyond the ratio

  • Never use color as the only signal: pair it with text, an icon or a pattern.
  • Thin and small type needs more contrast than the formula suggests.
  • Text on top of images needs an overlay or a solid backdrop to keep its ratio.
  • Check hover, focus, selected and error states, not just the default one.

Summary

  • Contrast ratios range from 1:1 to 21:1.
  • AA needs 4.5:1 for body text and 3:1 for large text and interface parts.
  • Truncate ratios when you report them; never round a failure into a pass.
  • Check real color pairs in both light and dark themes.

Finished this lesson?

Mark it complete to track your progress across every track in the curriculum.