color-mix()

Term side of the Card.
Topic: Colors
Unit title: color-mix()
Categories: Function
Explanation side of the Card

Explanation: The function that blends two colors together based on a specified ratio, creating a new color that is a mix of the two. It's useful for generating gradients, adjusting colors dynamically or creating themes with blended hues.

Browser compatibility:
  • The chrome browser supports this feature starting from version 111.
  • The edge browser supports this feature starting from version 111.
  • The firefox browser supports this feature starting from version 113.
  • The safari browser supports this feature starting from version 16.2.
Availability: 

Example

Wrongcss
:root {
  --brand: #1f5eff;
  --brand-light: #8fb0ff;
}
Rightcss
:root {
  --brand: #1f5eff;
  --brand-light: color-mix(in oklab, var(--brand) 50%, white);
}

The tint is derived from --brand, so changing the brand color updates its variants too.

Common mistakes

  • Reading the percentage as the share of the second color, while it belongs to the color it follows.
  • Percentages that add up to less than 100%, which also lowers the alpha.

Key points

  • The interpolation space is set as the first argument, often in oklab or in oklch.
  • Tints and shades mix a base color with 'white' or 'black' instead of hardcoding extra hex values.
  • Transparent variants mix with transparent, like color-mix(in srgb, var(--brand) 40%, transparent).

Interview questions

Which color space should I use in color-mix()?

oklab gives even, natural blends for most UI work. srgb matches many design tools, and oklch keeps the hue but can pass through vivid colors.

Can color-mix() make a color transparent?

Yes. Mix it with transparent, like color-mix(in srgb, red 30%, transparent), to get red at 30% opacity.

What is the difference between color-mix() and relative color syntax?

color-mix() blends two colors. Relative color syntax changes single channels of one color, like its lightness or alpha.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy