# contrast-color()

> The `contrast-color()` CSS function returns black or white, whichever has the higher contrast with the given color.

## How do I pick black or white text automatically in CSS?

The function that takes a color and returns 'white' or 'black', depending on which one has a better contrast with it. Useful for automatically choosing a readable text color on top of a dynamic background.

- Topic: Colors
- Type: function
- Browser support: Chrome 147+, Chrome Android 147+, Edge 147+, Firefox 146+, Firefox Android 146+, Safari 26+, Safari iOS 26+

## Example

Wrong:

```css
.badge {
  background: var(--bg);
  color: contrast-color(var(--bg));
}
```

Right:

```css
.badge {
  background: var(--bg);
  color: white;
}
@supports (color: contrast-color(red)) {
  .badge {
    color: contrast-color(var(--bg));
  }
}
```

With `var()` an unsupported value fails only at computed time, so `@supports` guards the `contrast-color()` line.

## Common mistakes

- Expecting it to return a brand color, while it only picks black or white.

## Key points

- Where support matters, `contrast-color()` sits inside `@supports (color: contrast-color(red))`, because a plain fallback line does not help when the value uses `var()`.
- The input is the same value as the background, often one shared custom property.
- Text color is picked by `contrast-color()`, not by a JavaScript luminance function.

## Interview questions

### Does contrast-color() guarantee accessible contrast?

No. It picks whichever of black or white contrasts more, but on mid-tone backgrounds even the better one may not reach the contrast you need.

### Can I use contrast-color() with a custom property?

Yes, like `contrast-color(var(--bg))`. Use the same property for the background so the pair always matches.

## Related cards

- [Relative Colors](https://wonderium.dev/css/relative-colors.md)
- [color-mix()](https://wonderium.dev/css/color-mix.md)
- [light-dark()](https://wonderium.dev/css/light-dark.md)
- [Variables](https://wonderium.dev/css/variables.md)
- [WCAG](https://wonderium.dev/a11y/wcag.md)

---

- Level: [CSS](https://wonderium.dev/css.md)
- HTML version: https://wonderium.dev/css/contrast-color
- Last updated: 2026-11-25
