CSS Reference ยท Functions
The color-mix() function
Reference for the CSS color-mix() function: mix two colors in a chosen color space, set percentages, control hue paths, and build hover shades and tints.
The color-mix() function blends two colors in a color space you name and returns the result as a color. It replaces preprocessor helpers such as mix() and lets you derive hover states, tints and borders from a single brand color at runtime, even when that color comes from a custom property.
.button { background: var(--brand); } .button:hover { background: color-mix(in oklch, var(--brand), black 20%); }
Try it
The same red and blue give different midpoints depending on the space. The last swatch shows a partly transparent red made by mixing with transparent.
Syntax
color-mix(in <color-space>, <color> [<percentage>], <color> [<percentage>]) color-mix(in <polar-space> [shorter | longer | increasing | decreasing] hue, ...)
Syntax and arguments
| Argument | Description |
|---|---|
in <color-space> |
The space where mixing happens: srgb, srgb-linear, display-p3, lab, oklab, xyz and others, or a polar one: hsl, hwb, lch, oklch. |
| hue method | For polar spaces only: which way around the wheel the hue travels (shorter is the default). |
<color> |
Any color, including currentcolor, custom properties and system colors. |
<percentage> |
The share of that color. If you give one, the other gets the remainder. If you give none, each is 50%. |
If the percentages add up to less than 100%, the result is multiplied by that sum and becomes partly transparent; if they add up to more than 100%, they are scaled down to total 100%.
Usage notes
- The space matters.
srgbmixes can look dull;oklabandoklchusually give the most natural midpoints. Always write the space explicitly. - Mixing with black or white.
color-mix(in oklab, var(--c), white 30%)gives a tint, and withblacka shade, with predictable lightness. - Transparency. Mixing with
transparentyields translucent versions of a color without needing its channel values. - Dynamic. Mixes are computed from the current values of custom properties and
currentcolor, so they react to themes. - Not animated as a function. Transitions work on the resulting colors; the mixture itself is not interpolated step by step.
Browser support
Supported in current versions of all major browsers. Check a compatibility table for older ones, and put a plain fallback color before the declaration.
Related
oklch()โ a good mixing spacelight-dark()rgb()- Lessons: Modern color, Gradients