CSS color recipe

contrast-color() Labels that pick black or white for themselves

contrast-color(var(--bg)) resolves to white or black, whichever contrasts more with the background. One rule then labels a tag of any color, with fallbacks worked out ahead of time for browsers without it.

See it run
Baseline
Newly available
First versions
Chrome 147 · Firefox 146 · Safari 26
Tested
3 of 3 engines

Live example

The recipe, running

Left: the CSS below, as your browser runs it. Right: the same CSS as a browser without the feature reads it, so you see exactly what the fallback gives.

Turn on JavaScript to see whether your browser supports it; the examples work either way.

As written, in your browser
In a browser without it

The CSS

Copy it as it is

This is the exact CSS the example runs. Fallbacks come first; the modern values follow, so each browser keeps the last line it understands.

contrast-color.css

/* Fallback: the label color worked out ahead of time, per background. */
.tag { padding: 6px 14px; border-radius: 999px; background: var(--bg); color: var(--fallback-ink); }

@supports (color: contrast-color(red)) {
  .tag { color: contrast-color(var(--bg)); }
}

How it works

What each part does

  1. The browser decides. The CSS Color 5 draft leaves the contrast algorithm to the browser and warns against relying on WCAG 2 alone. The table below shows what each engine chose for these backgrounds.

  2. Only black or white. It will not find a brand-colored label that passes; for that, choose the color yourself.

  3. The fallback per tag. Each tag carries --fallback-ink, the one of black and white with more WCAG 2 contrast on its background.

Browser support

Where it runs

First version of each browser with the feature, from the web-features project and MDN’s browser-compat-data. “No” means not supported yet.

  • contrast-color()Newly availableBaseline newly available since 10 April 2026: every core browser has it, but older versions in use may not.
First supporting version by browser
Browsercontrast-color()
Chrome147
Chrome for Android147
Edge147
Firefox146
Firefox for Android146
Safari26
Safari on iOS26

Support reviewed on against web-features 98723fd and browser-compat-data 3b28abe.

Tested

Tested in three engines

Engines this recipe was run in
EngineVersionSupportedAs writtenFallback
Chromium147.0.7727.15YesPassPass
Firefox148.0.2YesPassPass
WebKit26.4YesPassPass

What each engine chose

Label color chosen by contrast-color() for each background
BackgroundBlack textWhite textFallbackChromiumFirefoxWebKit
#3B82F65.71:13.68:1BlackBlackBlackBlack
#EF44445.58:13.76:1BlackBlackBlackBlack
#22C55E9.22:12.28:1BlackBlackBlackBlack
#F59E0B9.78:12.15:1BlackBlackBlackBlack
#8B5CF64.96:14.23:1BlackBlackBlackBlack
#0F172A1.18:117.85:1WhiteWhiteWhiteWhite

All three engines chose the same label color for every background, and it matched the fallback.

Run on in Chromium 147.0.7727.15, Firefox 148.0.2 and WebKit 26.4, with Playwright.

Accessibility

Measured, not assumed

Whichever of black and white has more WCAG 2 contrast never falls below 4.58:1 on any background, so that choice always clears AA for body text but never guarantees AAA.

References

Go to the source