Live card
Blue background
My background uses --card-bg, and my text color uses
contrast-color(var(--card-bg)).
Simple CSS Demo
It looks at one background color and chooses either black or white text.
The card background is stored in --card-bg. When that
background changes, the text color updates too.
Live card
My background uses --card-bg, and my text color uses
contrast-color(var(--card-bg)).
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
This is where I would tell the class: “Do not trust the function blindly.”
Clearer example
Darker backgrounds usually make the black-or-white choice feel more obvious.
Clearer example
Lighter backgrounds usually make black text the easier choice.
The CSS starts with a simple fallback text color. Then
@supports checks if the browser understands
contrast-color().