I'm using color-mix to take a button's background-color (set via a custom property) and make it lighter on hover. I expected browsers that don't support color-mix to simply ignore the hover state and use the non-hover background-color, but instead the button was turning transparent on hover. I added an explicit fallback background-color in the hover state styles but this had no effect:
.button {
background: var(--buttonColor);
&:hover, &:focus {
background-color: var(--buttonColor);
background-color: color-mix(in srgb, var(--buttonColor) 80%, white);
}
}
After trying multiple variations of this, it seems the issue is that using a custom property inside color-mix() somehow makes browsers that don't support color-mix() think that they do, and their failed attempt at interpreting it results in the background-color becoming transparent (discovered by looking at which rules are used/discarded in dev tools). Because of this, my fallback is being ignored in the browsers that need it.
As a side note, the same issue was also happening with border-color, though this was defaulting to white rather than transparent.
After writing most of this out, I realised I can get around it by wrapping my color-mixes inside @supports (background-color: color-mix(in srgb, black 50%, white)), but decided to still post it in the hopes that:
- it helps someone else who's struggling with the same thing, and
- someone can shed some light on why this is happening so that I'm aware of the root issue in future
It's not about
color-mix()but the use of CSS variables.Any property that contain
var()is consider as valid even if it contains non-supported featuresIn your case, the property will fail at computed-value time and fallback to initial (transparent background)