contrast-color()
Newly availableSince 202682% of users
A function that automatically selects a text color with the best contrast against a given background color. It helps dynamic themes keep text readable whatever the background.
What's modern
From web-features, MDN's browser data and our snippets, checked dailyModernIn every major browser since April 2026. Widely available around October 2028.
The old way and the modern waySee the snippet: Readable text without manual contrast checks →
Old way 10 lines
/* Hardcode a text color per background */
.badge-blue {
background: #1e40af;
color: white;
}
.badge-yellow {
background: #fde047;
color: black;
}
/* Repeat for every color variant */Modern 4 lines
.badge {
background: var(--bg);
color: contrast-color(var(--bg));
}See how modern your own CSS is.Check your CSS →
Quick example
.dynamic-badge {
background-color: var(--bg-variable);
/* Color is automatically chosen for best contrast */
color: contrast-color(var(--bg-variable));
}Related snippet
Readable text without manual contrast checksColors →contrast-color() browser support
BaselineNewly availableSince 2026 this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.Works in all modern browsers. May need a fallback for older browsers.
Usage82%of people can use this18% use a browser without it.
Browsers
147+
146+
26+
147+Mobile
147+Android
146+Android
26+iOS
Interop 2026 focus area. All major browsers are actively working to fix cross-browser inconsistencies for this feature in 2026. Learn more →