contrast-color() CSS Function
The contrast-color() function returns white or black, whichever has more contrast against a given background color.
Description
The contrast-color() CSS function takes a color and returns either white or black, whichever contrasts more with it. Its main use is choosing a readable text color automatically for a background color that may change, for example a brand color set in a custom property, a user-chosen theme color, or color-coded tags and badges.
The browser compares the contrast of white and black against the given color and returns the one with the higher contrast; if both are equal, it returns white. For very light and very dark colors the result easily meets accessibility guidelines, but mid-tone colors are a weak spot. A medium blue, for example, may get black text that is hard to read at small sizes, because neither black nor white contrasts strongly with it. Use contrast-color() with clearly light or dark backgrounds, and check the result for important text.
The returned value is a normal <color>, so it can be used with any color property, and it works well with var(). Browsers may improve the contrast algorithm in future, so do not rely on a specific threshold.
:root {
--brand: #0f766e;
}
.button {
background-color: var(--brand);
color: contrast-color(var(--brand)); /* white on a dark teal */
}
.tag {
background-color: var(--tag-color);
color: contrast-color(var(--tag-color));
}
Interactive Demo
Syntax
contrast-color() = contrast-color( <color> )
Values
- <color>Any valid color, typically the background color the text will appear on. The function returns white or black, whichever has the greater contrast with it (white if they are equal).
Example
Browser Support
The following information will show you the current browser support for the CSS contrast-color() function. Hover over a browser icon to see the version that first introduced support for this CSS function.
Desktop





Tablets & Mobile





