---
title: "How to have the browser pick a contrasting color in CSS"
description: "CSS `contrast-color()` takes one color and returns black or white, whichever contrasts more, so a single variable like `--button-color` can drive both background and text, including hover states built with relative color syntax."
url: "https://swe.quest/articles/how-to-have-the-browser-pick-a-contrasting-color-in-css-9cfb1d29a0334ca0"
canonical: "https://webkit.org/blog/16929/contrast-color/"
source: "WebKit"
published: "2025-05-13"
difficulty: "medium"
kind: "How-to"
reading_minutes: 12
tags: ["css","accessibility","browsers","frontend"]
---

# How to have the browser pick a contrasting color in CSS

A post on [WebKit](https://swe.quest/sources/webkit.md), published 2025-05-13, 12 min read, How-to. Original: [https://webkit.org/blog/16929/contrast-color/](https://webkit.org/blog/16929/contrast-color/)

## Summary

CSS \`contrast-color()\` takes one color and returns black or white, whichever contrasts more, so a single variable like \`--button-color\` can drive both background and text, including hover states built with relative color syntax. The current spec uses the WCAG 2 contrast algorithm, which often picks the perceptually worse option on mid-tones (black on #317CFF scores 5.45:1 against 3.84:1 for white, though white is clearly more readable). APCA, a WCAG 3 candidate, scores contrast perceptually and accounts for font size and weight. The function only picks the better of two options, not a sufficient one, so it should be paired with careful palette design and \`prefers-contrast\` overrides. The CSSWG limited it to black/white so the algorithm can be swapped later without breaking sites.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [CSS](https://swe.quest/tags/css.md) (Web & mobile)
- [Accessibility](https://swe.quest/tags/accessibility.md) (Web & mobile)
- [Browsers](https://swe.quest/tags/browsers.md) (Web & mobile)
- [Frontend](https://swe.quest/tags/frontend.md) (Web & mobile)

## Prerequisites

- CSS custom properties ([easy posts on CSS](https://swe.quest/tags/css.md?difficulty=easy))
- CSS media queries ([easy posts on CSS](https://swe.quest/tags/css.md?difficulty=easy))
- color contrast basics

## Keywords

contrast-color(), WCAG 2, APCA, WCAG 3, Safari, prefers-contrast, Relative Color Syntax

## Related posts

- [There are many themes, but this one is yours](https://swe.quest/articles/there-are-many-themes-but-this-one-is-yours-54d3fb5afa0c1ab8.md): Earendil · 2026-10-09 · Medium · 12 min read · Accessibility, Developer tools, Algorithms
  Pi's new default system theme queries the terminal for its foreground, background and 16 ANSI colors, keeps their hue and chroma, and recomputes lightness so that around 60 UI color roles meet per-surface contrast rules.
- [Customizing Color Fonts on the Web](https://swe.quest/articles/customizing-color-fonts-on-the-web-74940ba780f012e8.md): WebKit · 2022-05-25 · Medium · 7 min read · CSS, Browsers, Frontend
  WebKit (Safari 15.4+) supports the CSS \`@font-palette-values\` at-rule, which lets authors select a color font's predefined CPAL palettes by index and override individual palette colors, then apply them with the \`font-palette\` property.
- [Introducing Adaptive Dark Mode](https://swe.quest/articles/introducing-adaptive-dark-mode-b5085c9f9c7b77a6.md): WebKit · 2026-11-03 · Medium · 5 min read · Browsers, CSS, Accessibility, Privacy
  Safari 27.3 beta adds Adaptive Dark Mode, which automatically darkens light websites that don't declare dark mode support via color-scheme.
- [Unlocking inclusive design: how Primer’s color system is making GitHub.com more inclusive](https://swe.quest/articles/unlocking-inclusive-design-how-primer-s-color-system-is-making-github-com-more-438d95888d234cb3.md): GitHub Engineering · 2023-05-11 · Medium · 6 min read · Accessibility, Design systems, CI/CD, Frontend
  GitHub's Primer design system fixed hundreds of color contrast violations across light and dark modes and all themes by changing its foundational color tokens (Primer Primitives) rather than individual components.
- [Add wide gamut P3 and alpha transparency to your color picker in HTML](https://swe.quest/articles/add-wide-gamut-p3-and-alpha-transparency-to-your-color-picker-in-html-2bf169620936554b.md): WebKit · 2025-05-07 · Medium · 4 min read · Browsers, Frontend, CSS
  \`\<input type="color"\>\` now supports two new optional attributes, \`alpha\` and \`colorspace\`, added to the HTML standard with WHATWG and first shipped in Safari 18.4.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
