color-mix()
Mixes two colours in given proportions and in a chosen colour space. It creates lighter, darker, and semi-transparent variants.
Example
#:root {
--brand: #7c3aed;
}
.btn {
border: 1px solid color-mix(in oklch, var(--brand), white 35%);
background: var(--brand);
}
.btn:hover {
background: color-mix(in oklch, var(--brand), black 20%);
}
.btn.soft {
background: color-mix(in oklch, var(--brand) 18%, transparent);
color: color-mix(in oklch, var(--brand), white 50%);
}<div style="display: flex; flex-wrap: wrap; gap: 12px;">
<button class="btn" type="button" style="padding: 10px 18px; border-radius: 10px; color: #fff; font: 600 15px system-ui, sans-serif; cursor: pointer; transition: background-color 0.2s;">Save</button>
<button class="btn soft" type="button" style="padding: 10px 18px; border-radius: 10px; font: 600 15px system-ui, sans-serif;">Cancel</button>
</div>Hover over the first button. Then click “Edit” and change --brand to any colour: the border, the hover state, and the soft variant recalculate by themselves.
Definition and usage
#The color-mix() function works like mixing paints on a palette. You specify the colour space in which the mixing happens and two colours with optional percentages: color-mix(in srgb, #7c3aed 70%, white) is 70% purple and 30% white, which is a lighter purple.
The percentages are flexible. If you give one, the other colour gets the rest up to 100%, and without percentages the colours mix half and half. Mixing with transparent gives transparency: color-mix(in srgb, #7c3aed 40%, transparent) is the same purple at 40% opacity. When the percentages add up to less than 100%, the result also becomes partly transparent.
The colour space changes the result. In srgb mixing works like overlaying lights on a screen, but the intermediate colours can be greyish. In oklch or oklab the transitions are more natural and keep their saturation. It is worth always specifying the space, because older browsers without it will drop the whole declaration.
You gain the most when combining it with variables. From a single brand colour you can derive a whole family: a background (color-mix(in oklch, var(--brand) 15%, transparent)), a border, and a hover state (color-mix(in oklch, var(--brand), black 20%)), without writing further colour codes.
Syntax
#color-mix(in <color space>, <color> <percentage>?, <color> <percentage>?)Common mixes
#lighter variant
Purple with 40% white added. This is how light shades for backgrounds and highlights are made.darker variant
30% black darkens the colour, for example for a button’s hover state.semi-transparent
Mixing with transparency gives the same purple at 40% opacity. The checkerboard shows through the result.half and half
Without percentages the colours mix equally. The result lies halfway between purple and sky blue.greyish middle
Pink and green mixed in sRGB give a grey-brown colour, because the midpoint between them falls close to grey.vivid middle
The same colours mixed in OKLCH. The browser travels around the colour wheel, so you get a saturated gold instead of grey.
Common mixes in practice
##7c3aed 60%, white
.demo {
background: color-mix(in srgb, #7c3aed 60%, white);
}#7c3aed 70%, black
.demo {
background: color-mix(in srgb, #7c3aed 70%, black);
}#7c3aed 40%, transparent
.demo {
background:
color-mix(in srgb, #7c3aed 40%, transparent);
}#7c3aed, #0ea5e9
.demo {
background: color-mix(in srgb, #7c3aed, #0ea5e9);
}in srgb
.demo {
background: color-mix(in srgb, #f43f5e, #10b981);
}in oklch
.demo {
background: color-mix(in oklch, #f43f5e, #10b981);
}More examples
#.tag {
border: 1px solid color-mix(in srgb, currentColor 40%, transparent);
background: color-mix(in srgb, currentColor 12%, transparent);
}<style>
.tag { display: inline-block; padding: 4px 12px; border-radius: 999px; font-weight: 600; }
</style>
<div style="display: flex; flex-wrap: wrap; gap: 8px;">
<span class="tag" style="color: #a78bfa;">HTML</span>
<span class="tag" style="color: #38bdf8;">CSS</span>
<span class="tag" style="color: #fbbf24;">JavaScript</span>
</div>Each label sets only color. The border and background come from the text colour thanks to the currentColor keyword.
Browser support
#Widely available · since 2023
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- When mixing two different hues, choose
in oklchorin oklab. Insrgbthe intermediate colours can turn out greyish, as in the demonstration above. - Mixing with
transparentis the simplest way to get a semi-transparent version of a colour from a variable, even when the variable holds a hex code. - You can give a percentage on either colour:
color-mix(in oklch, var(--brand), white 30%)is 70% brand colour and 30% white.
Related entries
#- oklch()Writes a colour through lightness, chroma, and hue in a space matched to the eye. Colours of the same lightness look equally light.
- rgb()Writes a colour as a mix of red, green, and blue light, with optional transparency. It also covers hex notation.
- hsl()Writes a colour through its hue on the colour wheel, saturation, and lightness. It makes it easy to lighten, darken, or mute a colour.
- var()Inserts the value of a CSS variable, that is, a custom property written as --name. It also lets you provide a fallback value.
- colorSets the text colour and the default colour of borders, decorations and SVG icons that use currentcolor.
- background-colorSets the background colour of an element.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.