Stop Hardcoding Button Text Colors: contrast-color() Is Baseline
October 7, 2026 | 8 min read | CSS
By Syed · Frontend Engineer & Creator of UITweak
Every design system eventually grows the same ugly little function. Someone adds user-pickable theme colors, a marketing page ships category badges in eleven brand colors, and suddenly you need to know whether the label on top should be black or white. So you write it: relative luminance, the WCAG coefficients, a 0.179 threshold from a blog post, and a unit test nobody trusts. Then you run it in a useEffect, cache the result in a CSS custom property, and it breaks the moment someone changes that variable at runtime. contrast-color() deletes all of that. Pass it any background color and the browser returns black or white, whichever contrasts more, live.
30-second version: color: contrast-color(var(--bg)) resolves to black or white with the best contrast against --bg, and it recalculates whenever the variable changes. Baseline since April 2026 (Chrome 147, Safari 26, Firefox 146). Gate the fancy usage with @supports and keep a hardcoded default outside it.
Recipe 1: Readable Badges on CMS-Driven Colors
The core use case. A CMS editor adds a new category with a new brand color tomorrow, and the badge text is still readable without anyone touching the code:
.badge {
background: var(--badge-bg, #6b7280);
color: contrast-color(var(--badge-bg, #6b7280));
padding: 0.25rem 0.75rem;
border-radius: 999px;
}The fallback value inside var() matters twice here: it is the background when the variable is unset, and it is the color the contrast is computed against. Light badge colors get near-black text automatically, dark ones keep white. No color pairs to maintain, no build step, no effect hook.
Recipe 2: Theme-Proof Buttons
Buttons are where this pays off hardest, because the background is the thing that changes: theme switches, hover states, user-picked accents. Pair it with color-mix() and the hover state recolors itself too:
.btn-primary {
--btn-bg: var(--brand, #4f46e5);
background: var(--btn-bg);
color: contrast-color(var(--btn-bg));
}
.btn-primary:hover {
/* darken the bg; the text flips itself if needed */
--btn-bg: color-mix(in oklch, var(--brand, #4f46e5) 82%, black);
}Reassigning the variable on hover is the trick. The text color is not a second thing you have to remember; it follows the background wherever it goes. A user who picks a pale yellow brand color in your theme settings gets dark button text automatically. That is the exact case the old JS helper failed, because it computed once at build time. For the hover motion itself, our button hover effects guide has 10 copy-paste recipes (lift, glow, shine) that pair with this pattern.
Try it now: design the button itself in our free Tailwind Button Studio: radius, padding, shadow, the works, then paste the output into your stylesheet and add the two contrast-color() lines from Recipe 2 for the text. You get a hand-designed button whose label stays readable on any brand color you throw at it.
Recipe 3: Borders and Icons That Stay Visible
The function works anywhere a color is accepted, so the same pattern fixes borders on tinted cards and icons sitting on dynamic backgrounds:
.card {
--card-bg: var(--surface);
background: var(--card-bg);
/* visible edge regardless of theme */
border: 1px solid contrast-color(var(--card-bg));
}
.icon-btn svg {
/* inherits the computed black-or-white */
fill: currentColor;
}
.icon-btn {
background: var(--icon-bg);
color: contrast-color(var(--icon-bg));
}The currentColor trick is doing the quiet work in the second rule: set the text color with contrast-color() once, and the SVG inherits it. One declaration keeps the whole control legible.
The Two Limits You Should Know
First, it returns black or white only. No grays, no brand tints. If pure black text feels harsh on your light surfaces, soften it yourself:
.badge {
background: var(--badge-bg);
/* near-black instead of pure black, same auto logic */
color: color-mix(in oklch, contrast-color(var(--badge-bg)), transparent 12%);
}Second, it takes a color, not a gradient or an image. On a photo background you still need an overlay or a text shadow. Think of it as the replacement for the luminance helper, not for every contrast problem you have.
The Fallback (One @supports Block)
Older browsers ignore the declaration, so the rule to remember is: put a sane hardcoded color first, then override inside the query. Unsupported browsers keep the default, which you choose to look right on your most common background:
.badge {
background: var(--badge-bg, #6b7280);
color: #111827; /* default: readable on light badges */
}
@supports (color: contrast-color(red)) {
.badge {
color: contrast-color(var(--badge-bg, #6b7280));
}
}If you need a smarter CSS-only fallback for the in-between browsers, relative color syntax can approximate it: oklch(from var(--badge-bg) ...) derives a contrasting lightness from the background itself. (New to oklch? Our oklch colors guide is the primer.) It is more code than most teams need; the hardcoded default plus the @supports upgrade covers the realistic cases.
Quick Recap
contrast-color(any-color)returns black or white, whichever contrasts more, and it is live: it recalculates when the variable changes.- Put the background in a variable and derive the text from the same variable. One source of truth, no pairs to maintain.
- Reassign the variable on hover or theme switch and the text follows on its own.
- Black or white only. Soften with
color-mix()if pure black feels harsh. - Fallback pattern: hardcoded color first,
@supports (color: contrast-color(red))override second.
The accessibility guarantee, stated plainly
contrast-color() does not guess. It computes the WCAG contrast ratio of your background against black and against white, and returns whichever is higher. Here is the part worth knowing: for any opaque color, one of black or white always reaches a ratio of about 4.6 to 1 or better. That means contrast-color() effectively guarantees WCAG AA for normal text on any flat, opaque background, with no manual checking. Two boundaries to keep in mind. First, the guarantee assumes an opaque background. On a translucent color over a busy photo, the computed ratio is against the color value, not the final blended pixels, so verify those cases by eye. Second, contrast is necessary but not sufficient for accessibility. Color alone must never be the only signal. Keep your focus rings, underlines, and icons doing their jobs alongside the text color.
Debugging: it picked the "wrong" color
When the result surprises you, the cause is in the input, not the function. One, the var() fallback and the background disagree. If the background uses var(--bg, blue) but the text computes contrast-color(var(--bg, gray)), an unset variable gives you a blue background with text chosen for gray. Keep one fallback value and reuse it in both places. Two, the background is a gradient or an image. contrast-color() accepts a color, nothing else. Passing a gradient is invalid, the declaration is dropped, and your fallback text color stays in place. Three, the @supports override is losing to source order. The enhancement must come after the default, or the default wins the tie. Four, you are testing in a browser without support and forgot. Chrome 147, Safari 26, and Firefox 146 are the floors. Anything older keeps the hardcoded color from the fallback pattern, which is the design working as intended, not a bug.
Where it belongs in a design system
I treat contrast-color() as token plumbing, not a per-component trick. The pattern: every surface token gets a companion text token derived from it, defined once in the token file.
:root {
--surface-brand: #4f46e5;
--on-surface-brand: contrast-color(#4f46e5);
}Components reference --on-surface-brand and never call the function directly. This keeps the magic in one place and makes the fallback story trivial: older browsers get a hardcoded companion value defined next to it. One more opinion: do not use it for body text on page backgrounds. Body text colors are stable and chosen deliberately. Automatic contrast is for the dynamic surfaces, badges, buttons, and tags, whose backgrounds change at runtime. Use the tool where the background moves.
Frequently Asked Questions
Which browsers support it?
Baseline newly available since April 10, 2026: Chrome and Edge 147 (April 2026), Firefox 146 (December 2025), Safari 26 (September 2025). All three engines pass the web platform tests for it, so the edge cases agree with each other.
What happened to color-contrast()?
It was renamed. The old color-contrast() list form (pick from a list of colors) never shipped in any browser and was cut from the spec. contrast-color() is the replacement, and it only resolves to black or white. Use the new name.
Does it update when a CSS variable changes?
Yes. It is computed live like any other CSS value. Change the variable on hover, in a media query, or from JavaScript, and the text color re-resolves immediately. This is the whole point: the build-time helper could never do this.
Can I get a gray instead of pure black or white?
Not from the function itself. Mix the result toward transparent with color-mix(), as shown in the limits section, to get a near-black or off-white that keeps the automatic behavior.