Menu

Migrating to Tailwind CSS v4 @theme & OKLCH Without Breaking WCAG Contrast Ratios
📰

Migrating to Tailwind CSS v4 @theme & OKLCH Without Breaking WCAG Contrast Ratios

DEV Community: tailwindcss·Jason Y. (dev_in_the_fog)·9 days ago
#GSnN5Hl8
#devtotailwindcss#devto#dev#oklch#tailwind
Reading 0:00
15s threshold

Moving from tailwind.config.js to Tailwind CSS v4's CSS-first @theme tokens and oklch() color scales feels much cleaner—until Lighthouse flags failing WCAG AA (4.5:1) contrast in dark mode or colors clip unexpectedly on sRGB displays.

1. High-Chroma OKLCH Colors Clip Outside sRGB

An accent like oklch(0.65 0.25 260) renders vividly on Display P3 laptops, then undergoes gamut mapping on standard sRGB monitors, subtly shifting perceived lightness and dropping contrast on thin UI text.

2. Dark Mode Needs Higher Lightness, Not Just Inverted Backgrounds

@import "tailwindcss" ;

@theme { --color-brand-500 : oklch ( 0.58 0.19 255 ) ; --color-surface-bg : oklch ( 0.99 0.004 255 ) ; --color-surface-fg : oklch ( 0.21 0.015 255 ) ; }

Enter fullscreen mode

Exit fullscreen mode

A L=0.58 brand accent easily clears 4.5:1 on white ( L=0.99 ), but drops below 3.5:1 on a dark surface ( L=0.16 ).…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.