Simple CSS Demo

What does contrast-color() do?

It looks at one background color and chooses either black or white text.

1. Custom property demo

The card background is stored in --card-bg. When that background changes, the text color updates too.

Live card

Blue background

My background uses --card-bg, and my text color uses contrast-color(var(--card-bg)).

2. The mid-tone problem

Mid-tone colors are not very dark or very light. The browser still only gets to choose black or white, so the result can feel a little harsh or awkward.

Mid-tone example

Green

This is where I would tell the class: “Do not trust the function blindly.”

Clearer example

Purple

Darker backgrounds usually make the black-or-white choice feel more obvious.

Clearer example

Yellow

Lighter backgrounds usually make black text the easier choice.

3. Fallback with @supports

The CSS starts with a simple fallback text color. Then @supports checks if the browser understands contrast-color().

Your browser does not support contrast-color(), so this demo is using the fallback text colors.