Favorites

contrast-color() CSS Function

contrast-color()
CSS FunctionThis function is supported in some modern browsers, but not all.
Updated 21st November 2026

The contrast-color() function returns white or black, whichever has more contrast against a given background color.

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.

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

The text colour picks black or white automatically.
Demo Code
HTML Code
<div class="demo-element">The text colour picks black or white automatically.</div>
CSS Code
.demo-element {
	width: 260px;
	padding: 18px;
	border-radius: 8px;
	font-size: 18px;
	font-weight: bold;
	background-color: #ffd166;
	color: contrast-color(#ffd166);
}

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

<div class="wrap">
<h1>CSS contrast-color() examples</h1>
<div class="grid">
<div class="card">
<div class="sw" style="--bg: #fef3c7">Text</div>
<div class="label">--bg: #fef3c7</div>
</div>
<div class="card">
<div class="sw" style="--bg: #0f172a">Text</div>
<div class="label">--bg: #0f172a</div>
</div>
<div class="card">
<div class="sw" style="--bg: #16a34a">Text</div>
<div class="label">--bg: #16a34a</div>
</div>
<div class="card">
<div class="sw" style="--bg: #facc15">Text</div>
<div class="label">--bg: #facc15</div>
</div>
<div class="card">
<div class="sw" style="--bg: #7c3aed">Text</div>
<div class="label">--bg: #7c3aed</div>
</div>
<div class="card">
<div class="sw" style="--bg: #e2e8f0">Text</div>
<div class="label">--bg: #e2e8f0</div>
</div>
</div>
<p class="note">Each swatch uses color: contrast-color(var(--bg)).</p>
</div>
/* Base demo layout */
* { box-sizing: border-box; }

body {
margin: 24px;
font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial;
background: #f5f7fb;
color: #102a43;
}

.wrap {
max-width: 860px;
margin: 0 auto;
}

h1 {
font-size: 20px;
margin: 0 0 16px;
}

.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 16px;
}

.card {
background: #fff;
border-radius: 12px;
padding: 16px;
box-shadow: 0 6px 18px rgba(16, 42, 67, 0.08);
}

.label {
font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
font-size: 12px;
color: #486581;
margin-top: 10px;
word-break: break-word;
}

.note {
font-size: 13px;
color: #486581;
margin-top: 14px;
}

/* contrast-color() examples */
.sw {
height: 80px;
border-radius: 8px;
display: grid;
place-items: center;
font-weight: 700;
}

.sw {
background-color: var(--bg);
color: #000; /* fallback */
color: contrast-color(var(--bg));
}
Open in editor

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.

This function is supported in some modern browsers, but not all.

Desktop

Chrome
Edge
Firefox
Opera
Safari

Tablets & Mobile

Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-
Supported Unknown Not supported

Looks like you're using an ad blocker, and that's fine! Ads help keep CSS Portal's generators and references free, so if the site's useful to you, visit our Support page to see other ways to help.