color-mix()

Mixes two colours in given proportions and in a chosen colour space. It creates lighter, darker, and semi-transparent variants.

On this page

Example

#
CSS
: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%);
}
Preview

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

#
Syntax
color-mix(in <color space>, <color> <percentage>?, <color> <percentage>?)

Common mixes

#
  • #7c3aed 60%, white

    lighter variant

    Purple with 40% white added. This is how light shades for backgrounds and highlights are made.
  • #7c3aed 70%, black

    darker variant

    30% black darkens the colour, for example for a button’s hover state.
  • #7c3aed 40%, transparent

    semi-transparent

    Mixing with transparency gives the same purple at 40% opacity. The checkerboard shows through the result.
  • #7c3aed, #0ea5e9

    half and half

    Without percentages the colours mix equally. The result lies halfway between purple and sky blue.
  • in srgb

    greyish middle

    Pink and green mixed in sRGB give a grey-brown colour, because the midpoint between them falls close to grey.
  • in oklch

    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

Purple with 40% white added. This is how light shades for backgrounds and highlights are made.
.demo {
  background: color-mix(in srgb, #7c3aed 60%, white);
}

#7c3aed 70%, black

30% black darkens the colour, for example for a button’s hover state.
.demo {
  background: color-mix(in srgb, #7c3aed 70%, black);
}

#7c3aed 40%, transparent

Mixing with transparency gives the same purple at 40% opacity. The checkerboard shows through the result.
.demo {
  background:
    color-mix(in srgb, #7c3aed 40%, transparent);
}

#7c3aed, #0ea5e9

Without percentages the colours mix equally. The result lies halfway between purple and sky blue.
.demo {
  background: color-mix(in srgb, #7c3aed, #0ea5e9);
}

in srgb

Pink and green mixed in sRGB give a grey-brown colour, because the midpoint between them falls close to grey.
.demo {
  background: color-mix(in srgb, #f43f5e, #10b981);
}

in oklch

The same colours mixed in OKLCH. The browser travels around the colour wheel, so you get a saturated gold instead of grey.
.demo {
  background: color-mix(in oklch, #f43f5e, #10b981);
}

More examples

#
A border and background computed from the text colour
CSS
.tag {
  border: 1px solid color-mix(in srgb, currentColor 40%, transparent);
  background: color-mix(in srgb, currentColor 12%, transparent);
}
Preview

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 oklch or in oklab. In srgb the intermediate colours can turn out greyish, as in the demonstration above.
  • Mixing with transparent is 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

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.