v147 · css · covered on mdn
CSS contrast-color()
A CSS color function that returns black or white — whichever provides greater contrast — against a given color. Useful for ensuring text remains readable regardless of a dynamic background color, without needing JavaScript or precomputed fallbacks.
this feature is documented on MDN
MDN has a full reference page for contrast-color() including syntax, examples, browser compatibility, and the contrast algorithm used. gendn doesn't duplicate that here.
quick reference
| Syntax | contrast-color(<color>) |
|---|---|
| Returns | white or black — max contrast as text on the argument (UA-defined algorithm; see Syntax) |
| Where usable | Anywhere a <color> value is accepted |
| Example | color: contrast-color(var(--bg)); |
| Shipped in | Chrome 147 (desktop + Android) |
| ChromeStatus | 4841046007742464 — CSS contrast-color() |
| Spec | CSS Color Level 5 — contrast-color() (what Chrome 147 ships); extended form in CSS Color Level 6 |
Syntax
Value definition from the CSS Color Module Level 5 editor’s draft §8, quoted verbatim (fetched 2026-11-22). This is the spec the ChromeStatus record and BCD css.types.color.contrast-color cite for the shipped function:
contrast-color() = contrast-color( <color> )
- Argument: one
<color>, treated as a solid background color. Custom properties,currentcolorand relative colors are all<color>values. - Result: resolves to
whiteorblack, whichever produces maximum color contrast for text on that background; if both tie,white. - Algorithm: UA-defined at this level. The spec advises UAs not to simply use the WCAG 2.1 §1.4.3 contrast ratio, yet returned colors should still meet WCAG 2.1 AA for large text.
The Level 6 draft extends the function with foreground/background role keywords, a <target-contrast> and a candidate color list. That extended form is not what Chrome 147 shipped — the ChromeStatus summary describes the single-argument black/white form — so feature-detect it separately before relying on it.
Examples
contrast-color().Source: chrome-platform-showcase — css-contrast-colorReadable text on a themeable background
.badge {
--badge-bg: rebeccapurple;
background-color: var(--badge-bg);
color: contrast-color(var(--badge-bg)); /* resolves to white here */
}
Fallback for browsers without support
Declare a fixed color first; browsers that understand contrast-color() override it, others keep the fallback. Wrap more elaborate alternatives in @supports:
.chip {
background-color: var(--chip-bg);
color: black; /* fallback */
color: contrast-color(var(--chip-bg)); /* Chrome 147+, Firefox 146+, Safari 26+ */
}
@supports not (color: contrast-color(red)) {
.chip { text-shadow: 0 0 2px white; } /* extra legibility for the fallback */
}
Source: css-color-5 §contrast-color; live hue-explorer demo.
Browser compatibility
| Browser | Version | Notes |
|---|---|---|
| Chrome | 147 | Standard track, not experimental, not deprecated |
| Chrome Android | 147 | BCD mirror of Chrome |
| Edge | 147 | BCD mirror of Chrome |
| Firefox | 146 | Firefox for Android: mirror (146) |
| Safari | 26 | Safari on iOS: mirror (26) |
Specifications
| Specification | Section |
|---|---|
| CSS Color Module Level 5 | § contrast-color() |
| CSS Color Module Level 6 (extended form, not shipped in Chrome 147) | § contrast-color() |