Colour
contrast-color()
Newly availablesince Apr 2026
Replaces: Computing in JavaScript whether text on a dynamic background should be black or white. The browser picks the readable one.
See it working
The syntax
.tag { background: var(--tag-colour);
color: contrast-color(var(--tag-colour)); } Edit the demo’s CSS
This is the stylesheet running the demo above. Change a value and watch it. Break it on purpose: delete the modern line and you are looking at the fallback.
For browsers without it
Set a readable text colour by hand first and let contrast-color() replace it inside @supports, exactly as the demo does. Never rely on it alone for a colour the user can change.
Before you ship it
Black or white is not always readable
MDN warns that WCAG AA cannot be met on every background with only black and white text: a mid-tone such as #2277d3 is given black, which fails for small text. Use it on colours you keep clearly light or clearly dark, and check the pair. Source