Contrast for accessibility (WCAG AA recap)
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 10 lessons in each track are free. No card needed for those.