2.5 · Color

Readable text on any color: contrast-color()

Let the browser pick black or white text for any background.

contrast-color(var(--bg)) returns black or white, whichever contrasts more with the color you pass in. Buttons, tags and swatches whose background comes from user data or a theme stay readable without a lookup table.

It only chooses between black and white, and it doesn’t guarantee a WCAG contrast ratio for every color. Mid-tones can still be borderline, so treat it as a strong default rather than a compliance check.

Before
// Compute luminance in JS
const l = 0.2126 * r + 0.7152 * g + 0.0722 * b
el.style.color = l > 0.5 ? 'black' : 'white'
/* or a hand-picked text color per theme */
Now
.tag {
  background: var(--bg);
  color: contrast-color(var(--bg));
}