Sub topic 4
Mixing colors with color-mix()
This chapter assumes you know basic CSS selectors, custom properties, and the oklch() syntax from the previous chapter. You will learn how color-mix() works, how its percentages are calculated, and which mistakes to watch for.
What color-mix() does
color-mix() is a CSS function that blends two colors and returns a new one. You write the color space first, then the two colors, each with an optional percentage.
.card {
background: color-mix(in oklch, var(--brand) 15%, white);
}This rule takes 15% of --brand and the remaining 85% from white. The result is a light tint of the brand color. The mix happens in the OKLCH space, so the tint keeps the brand's hue and does not drift toward gray or another hue.
The syntax has three parts:
in <color-space>names the space where the blending happens.- The first color, with an optional percentage.
- The second color, with an optional percentage.
Because the colors can be any valid CSS color, you can mix a custom property with a keyword, or a hex value with an oklch() value.
How percentages work
The percentages decide how much each color contributes. The browser applies these rules:
| You write | Result |
|---|---|
| `color-mix(in oklch, red, blue)` | 50% red, 50% blue |
| `color-mix(in oklch, red 30%, blue)` | 30% red, 70% blue |
| `color-mix(in oklch, red 30%, blue 30%)` | Both are scaled up to 100%, so 50% each. The result is also made 60% opaque. |
| `color-mix(in oklch, red 20%, blue 20%)` | Both are scaled up to 100%, so 50% each. The result is also made 40% opaque. |
When one percentage is omitted, it becomes 100% minus the other. When both are given and add up to less than 100%, the browser scales them up to 100% and multiplies the alpha of the result by the original sum. When they add up to more than 100%, the browser scales them down and the alpha stays unchanged.
The alpha reduction is useful when you want a translucent result. It is also a common source of surprise, so prefer one percentage and let the other fill in.
Tints, shades, and hover states
Mixing with white makes a tint. Mixing with black makes a shade. Both are quick ways to build variants from one token.
:root {
--brand: oklch(62% 0.19 255);
}
.button {
background: var(--brand);
}
.button:hover {
background: color-mix(in oklch, var(--brand), black 15%);
}
.badge {
background: color-mix(in oklch, var(--brand) 20%, white);
color: color-mix(in oklch, var(--brand), black 40%);
}The hover rule adds 15% black, so the brand color supplies the other 85%. Changing --brand updates the button, the hover state, and the badge together.
You can also mix with transparent to make a see-through version of a color:
.overlay {
background: color-mix(in oklch, var(--brand) 50%, transparent);
}Interpolation hue and the shorter path
OKLCH stores hue as an angle, so the browser must decide which way around the circle to travel. By default it takes the shorter arc. You can override it with a hue interpolation method placed after the color space.
.swatch-a {
background: color-mix(in oklch, red, blue);
}
.swatch-b {
background: color-mix(in oklch longer hue, red, blue);
}swatch-a passes through purple and magenta. swatch-b goes the long way, through orange, green, and cyan. A later chapter covers increasing hue and decreasing hue.
Common errors
- Forgetting
in. The color space is required.color-mix(red, blue)is invalid, and the declaration is dropped. - Percentages that sum to less than 100%. The result becomes partly transparent. Check the alpha if a mixed color looks washed out against its background.
- Assuming 15% means 15% lighter. The percentage is a share of each color, not a change in lightness. Mixing with white at 85% of the brand gives a different result than raising
lby 0.15. - Mixing in the wrong space. Mixing in
srgbcan give muddy midpoints, whileoklchusually gives cleaner results. The next chapter compares spaces in detail.
Exercise
Write a declaration that makes a background from 25% of var(--brand) and the rest white, mixed in OKLCH. What happens to the result if you write color-mix(in oklch, var(--brand) 25%, white 25%) instead?
Show answer
Use background: color-mix(in oklch, var(--brand) 25%, white);. In the second version the percentages sum to 50%, so they are scaled up to 50% brand and 50% white, and the result's alpha is multiplied by 0.5. The color is half transparent.