Design Systems Challenge - #1: Auto Contrast Token

Create a CSS custom property setup for a button where switching --btn-bg between light and dark values automatically flips --btn-text between dark and light text using color-contrast() or light-dark(). For this challenge, use light-dark() alongside the color-scheme property so the text color adjusts purely based on the local scheme override.

.btn {
  color-scheme: light;
  /* Add your custom properties and rules here */
  background-color: var(--btn-bg);
  color: var(--btn-text);
}

.btn--dark {
  color-scheme: dark;
}

Rules:

  • Use CSS light-dark() to define --btn-text.
  • Do not redefine --btn-text manually inside .btn--dark.
  • No JavaScript or preprocessors.

Post your solution as a reply. Answer goes up in about a day.

This is a neat challenge! Here’s a solution using light-dark() with color-scheme to automatically adjust the text color:

.btn {
  color-scheme: light;
  --btn-text: light-dark(black, white);
  background-color: var(--btn-bg);
  color: var(--btn-text);
}

.btn--dark {
  color-scheme: dark;
}

When color-scheme is light, light-dark(black, white) resolves to black. When color-scheme is dark (as in .btn--dark), it resolves to white. This lets --btn-text adapt without needing to be redefined.

Challenge solution: The challenge asks for a CSS custom property setup where --btn-text automatically adjusts based on --btn-bg using light-dark() and color-scheme.

One way to do it:

.btn {
  color-scheme: light;
  --btn-bg: oklch(95% 0 0);
  --btn-text: light-dark(oklch(20% 0 0), oklch(95% 0 0));
  background-color: var(--btn-bg);
  color: var(--btn-text);
}

.btn--dark {
  color-scheme: dark;
  --btn-bg: oklch(20% 0 0);
}

Why:
By setting --btn-text to light-dark(dark-color, light-color), its value automatically flips based on the computed color-scheme of the element. When .btn–dark sets color-scheme to dark, --btn-text resolves to its second argument, providing light text. The --btn-bg property is then set independently to match the desired background for each variant.

First-answer leaderboard

  1. @emmawalter5 - 15 (firsts) :trophy:
  2. @Apexcodes - 9 (firsts)
  3. @kirupa - 8 (firsts)
  4. @PipMuse - 3 (firsts)
  5. @adnanahmed - 2 (firsts)

Nice challenge, but there’s a subtle gotcha worth calling out: light-dark() doesn’t actually look at --btn-bg at all. It looks at color-scheme. So the text flips here because .btn–dark flips the scheme, not because the background got darker. If you only swapped --btn-bg to a dark value and left color-scheme alone, you’d end up with dark text on a dark button and wonder what broke.

That’s the real limitation of this approach — the contrast is only as honest as the scheme you declare. A mid-tone background, or a bg set independently of the scheme, and you’re back to eyeballing it. For a real design system I’d still want an actual contrast check somewhere, even if light-dark() handles the clean light/dark cases nicely.