Sub topic 4
Mixing colors with color-mix()
This chapter assumes you know basic CSS syntax and selectors, you can declare and read custom properties with var(), and you can write oklch() colors as covered in the previous chapter. Here you will learn the syntax of color-mix(), the rules for its percentages, and the mistakes people make most often when they first use it.
What color-mix() does
color-mix() is a CSS function that blends two colors and returns one color. The blend happens in a color space you name. You can use the result anywhere CSS accepts a color: background, border-color, box-shadow, gradient stops, and custom properties.
Before color-mix(), producing a lighter or darker variant of a brand color meant one of three things:
- Hand-picking a second hex value and keeping it in sync with the first.
- Running a preprocessor such as Sass.
- Computing the value in JavaScript.
color-mix() moves that work into the browser, so the variant updates automatically when the base color changes.
Syntax
The general form is:
color-mix(in <color-space>, <color> [<percentage>], <color> [<percentage>])There are four parts:
- The keyword
in, followed by a color space such asoklch,oklab,srgb, orhsl. This part is required. - A first color, with an optional percentage.
- A second color, with an optional percentage.
- Commas between the parts, as shown above.
Here is the simplest useful case:
.card {
background: color-mix(in oklch, var(--brand) 15%, white);
}This reads as "mix 15% of --brand with the rest white, calculated in OKLCH." The result is a light tint of the brand color. You did not write a percentage for white, so CSS assigns it the remaining 85%.
The first argument must begin with in. This is the most common syntax error:
.broken {
/* Invalid: the color space is missing, so the declaration is dropped */
background: color-mix(var(--brand) 15%, white);
}When a declaration is invalid, the browser ignores it and the property falls back to whatever came before. There is no console error, so if a color silently fails to apply, check the in first.
How percentages work
The percentages are where color-mix() behaves in less obvious ways. The rules cover every combination, so you can predict the result without trial and error.
| You write | Resulting weights | Alpha effect |
|---|---|---|
| No percentages | 50% and 50% | None |
| One percentage, such as `30%` | 30% and 70% | None |
| Two percentages that sum to 100% | As written | None |
| Two percentages that sum to more than 100% | Scaled down proportionally to total 100% | None |
| Two percentages that sum to less than 100% | Scaled up proportionally to total 100% | Alpha is multiplied by the original sum |
| Both `0%` | Invalid | The declaration is dropped |
Two examples show the less obvious rows.
.a {
/* 80% + 60% = 140%, scaled to roughly 57% and 43% */
background: color-mix(in oklch, red 80%, blue 60%);
}
.b {
/* 20% + 30% = 50%, scaled to 40% and 60%, then alpha is multiplied by 0.5 */
background: color-mix(in oklch, red 20%, blue 30%);
}The second example is a handy trick. When the percentages sum to less than 100%, the browser mixes in the ratio you gave, then makes the result partly transparent. Percentages that sum to 50% produce a color at half opacity. Most people do not expect this, so use it deliberately or avoid it.
Negative percentages are invalid. Values above 100% are not an error, but they are rescaled as shown in the table.
The diagram below shows how the browser resolves the weights.
Tints, shades, and tones
Mixing a color with white, black, or gray gives you the usual palette variations. Since the base color is a custom property, every variant follows it.
:root {
--brand: oklch(62% 0.19 255);
--brand-tint: color-mix(in oklch, var(--brand) 20%, white);
--brand-shade: color-mix(in oklch, var(--brand), black 25%);
--brand-tone: color-mix(in oklch, var(--brand), gray 40%);
}The three variants are:
- Tint: the brand color mixed with white, so it is lighter.
- Shade: the brand color mixed with black, so it is darker. The black has a percentage, and the brand color takes the remaining 75%.
- Tone: the brand color mixed with gray, so it is less saturated.
White, black, and gray all have zero chroma, which means they have no hue of their own. In OKLCH their hue is treated as missing, so the mix keeps the hue of your brand color. This is one reason mixing in OKLCH usually looks cleaner than mixing in sRGB, where the channels blend independently and the hue can drift. The next chapter compares interpolation spaces in detail.
The gray keyword is a mid-gray in sRGB. If you want the neutral to have the same lightness as your brand color, write an explicit one such as oklch(62% 0 0).
Mixing with transparent
You can mix with transparent to make a color partly see-through without writing its alpha by hand.
.overlay {
background: color-mix(in oklch, var(--brand) 40%, transparent);
}This yields the brand color at about 40% opacity. The color channels are weighted by alpha during interpolation (the spec calls this premultiplied interpolation), so the transparent side contributes no color. The brand hue, chroma, and lightness stay intact and only the alpha drops.
If you only want to change alpha on one color, the oklch(… / 0.4) form from the earlier chapter is more direct. color-mix() with transparent is useful when the source color is a custom property you did not define yourself. Relative color syntax, covered later, is another way to do the same thing.
Hover and state colors
A very common use is a hover state. The color is derived from the same token as the resting state, so the two cannot drift apart.
.button {
--button-bg: oklch(60% 0.15 250);
background: var(--button-bg);
color: white;
}
.button:hover {
background: color-mix(in oklch, var(--button-bg), black 15%);
}
.button:active {
background: color-mix(in oklch, var(--button-bg), black 30%);
}If a designer changes --button-bg, the hover and active colors follow. Compare that with the previous approach of hand-picking three hex values for each button color.
Transitions work with color-mix() as well. The browser interpolates between the two computed colors:
.button {
transition: background-color 150ms ease;
}Nesting mixes
The inputs to color-mix() can themselves be color-mix() calls. This lets you build a three-way blend:
.badge {
background: color-mix(
in oklch,
color-mix(in oklch, var(--brand) 50%, var(--accent)) 70%,
white
);
}The inner mix blends the brand and accent colors equally. The outer mix then takes 70% of that result and 30% white. Nesting works but reads poorly past two levels. If you need more than that, give the intermediate colors their own custom property names.
Using custom properties
Custom properties and color-mix() combine well, with one caveat. A custom property holds a token stream until something uses it. If you write:
:root {
--brand: oklch(62% 0.19 255);
--brand-light: color-mix(in oklch, var(--brand) 20%, white);
}then --brand-light is stored as the text of the function, with var(--brand) substituted. It is resolved wherever var(--brand-light) is used. This has a useful consequence: if a component overrides --brand locally, any element using --brand-light inside that component sees the override only if --brand-light is computed there. Custom properties are inherited as already-substituted values, so --brand-light defined on :root keeps the root --brand, even when a child overrides --brand.
.theme-green {
--brand: oklch(65% 0.17 150);
}Elements inside .theme-green will have the green --brand. However, var(--brand-light) still gives the blue tint, because it was substituted on :root. To make the derived colors follow, define them on the element that overrides the base:
.theme-green {
--brand: oklch(65% 0.17 150);
--brand-light: color-mix(in oklch, var(--brand) 20%, white);
}Alternatively, skip the intermediate token and write the color-mix() call directly in the rule that needs it. This tripped up many people on their first theme, so check it whenever a derived color refuses to change.
Common errors
- Missing
in. The declaration is invalid and silently ignored. - Percentages that sum below 100%. The result becomes semi-transparent. This is correct behavior, but surprising if you did not intend it.
- Both percentages at 0%. The function is invalid.
- Expecting a mix to be "halfway" in the same way as in another color space. The midpoint of red and blue is a different color in
srgbthan inoklch. The choice of space changes the answer, which is the subject of the next chapter. - Assuming a tint or shade is accessible. A tint made from 15% brand and 85% white is light, but you still need to check the contrast of any text you put on it. Chapter 10 covers this.
- No fallback. Browsers that predate
color-mix()drop the whole declaration. See the browser support chapter for the@supportspattern. A short version is below.
.card {
background: #e8f0fe;
background: color-mix(in oklch, var(--brand) 15%, white);
}The first declaration is a plain hex value. Browsers that understand color-mix() use the second one, and older browsers ignore it and keep the hex.
A worked example: a small button set
This stylesheet combines the ideas above. One base color drives a solid button, an outline button, and a soft button, each with a hover state.
:root {
--brand: oklch(55% 0.2 255);
}
.btn {
padding: 0.5rem 1rem;
border: 2px solid var(--brand);
border-radius: 0.375rem;
font: inherit;
cursor: pointer;
transition: background-color 150ms ease;
}
.btn-solid {
background: var(--brand);
color: white;
}
.btn-solid:hover {
background: color-mix(in oklch, var(--brand), black 15%);
border-color: color-mix(in oklch, var(--brand), black 15%);
}
.btn-outline {
background: transparent;
color: var(--brand);
}
.btn-outline:hover {
background: color-mix(in oklch, var(--brand) 10%, transparent);
}
.btn-soft {
background: color-mix(in oklch, var(--brand) 15%, white);
color: color-mix(in oklch, var(--brand), black 40%);
border-color: transparent;
}
.btn-soft:hover {
background: color-mix(in oklch, var(--brand) 25%, white);
}Change --brand on the first line and all three button styles update, including every hover state. In the soft button, the text color is a darkened brand color. That is a deliberate step to raise the contrast against the light background, though you should still verify the ratio.
Exercise
What color does color-mix(in oklch, red 20%, blue 30%) produce, in terms of the mix ratio and opacity?
Show answer
The percentages sum to 50%, which is below 100%, so they are scaled up to 40% red and 60% blue. The result's alpha is multiplied by the original sum of 0.5, so the color is a red-blue blend that is 50% opaque.
Exercise
A teammate writes background: color-mix(var(--brand) 20%, white); and the background never changes. What is wrong, and how do you fix it?
Show answer
The required color space is missing. A color-mix() call must begin with in <color-space>, so the declaration is invalid and the browser ignores it. Fix it with background: color-mix(in oklch, var(--brand) 20%, white);.
Exercise
You define --brand-light: color-mix(in oklch, var(--brand) 20%, white); on :root, then set --brand to green inside .theme-green. Elements in .theme-green still show the blue tint. Why, and what are two ways to fix it?
Show answer
A custom property is resolved where it is declared, so --brand-light was computed on :root using the blue --brand, and children inherit that computed value. One fix is to redeclare --brand-light inside .theme-green. The other is to skip the intermediate token and write color-mix(in oklch, var(--brand) 20%, white) directly in the rule that uses it.