Skip to main content
🎨 CSS as a design system·Module B5 · Lesson 7
TaskVerify a token palette passes AA. :root tokens: --bg #ffffff (rgb 255,255,255), --text oklch(0.20 0 0) (passes AAA against white), --text-muted oklch(0.50 0 0) (passes AA), --primary oklch(0.55 0.18 252) (a darker blue that passes AA against white), --primary-fg white. body uses --bg + --text. .alert uses --primary as background + --primary-fg as color.

Contrast for accessibility (WCAG AA recap)

75 XP6 min
Theory

The numbers from A5, applied to design tokens

(Quick recap from HTML A5 lesson 4 — now we apply it through tokens.)

WCAG AA requires:

  • Body text: 4.5:1 contrast against background.
  • Large text (18pt+ regular or 14pt+ bold): 3:1.
  • UI elements (buttons, focus rings): 3:1.

When you build a token palette, ensure every --text value passes against every --bg value:

:root {
  --bg: white;
  --text: oklch(0.20 0 0);        /* dark gray — passes 7:1 against white */
  --text-muted: oklch(0.50 0 0);  /* mid gray — passes 4.5:1 (just) */
  --text-disabled: oklch(0.65 0 0); /* light gray — FAILS body, passes large only */
}

Be honest about what disabled colors are FOR. --text-disabled is meant for disabled UI controls where 3:1 is the floor.

Tool tips

  • Chrome DevTools color picker → shows the contrast ratio + AA/AAA pass.
  • axe DevTools extension → flags every contrast violation across the page.
  • WebAIM Contrast Checker → compute manually.

Dark mode contrast

In dark mode, contrast is BETWEEN dark and light. Pure black (#000) on near-black surface (#0D1117) fails. Use:

  • Dark mode body bg: oklch(0.18 0 0) (≈ #1c1c1c)
  • Dark mode body text: oklch(0.95 0 0) (≈ #f2f2f2)

That pair = 12:1 contrast. Passes AAA.

Brand-color contrast

.btn-primary { background: var(--primary); color: var(--primary-fg); }

Make sure --primary-fg (the text COLOR shown on the primary background) passes 4.5:1 against --primary. White-on-blue (#3B82F6) passes (4.5:1 exactly). White-on-yellow fails — needs dark text.

🔒

Sign up to start coding

Theory is open to everyone. The interactive editor, live preview, and check are unlocked with a 7-day free trial — card required, cancel anytime.

Sign up — free trial →

First 15 lessons in each track are free. No card needed for those.

PreviousNext lesson →

Get one Python or web tip a day — by email

Short, hand-written, no spam. Unsubscribe in one click.