π What is color-mix()?
color-mix() is a modern CSS color function that allows you toblend two colors together in any supported color space. It works similarly to mixing paint β producing a new color based on percentages.
Note
β Great for theming, dynamic UIs, hover effects, and tonal palettes.
π― Syntax
color-mix Syntax
color-mix(in <color-space>, <color1> <percentage>?, <color2> <percentage>?)If percentages are not provided, CSS assumes a 50/50 mix.
π Simple Example
Mixing Red and Blue
.box {
background: color-mix(in srgb, red 50%, blue 50%);
}This produces a purple-like color.
π¨ Supported Color Spaces
| Color Space | Why Use It? |
|---|---|
| srgb | Standard for web colors |
| srgb-linear | More accurate blending |
| display-p3 | Wide-gamut colors (brighter) |
| lab | Human-perceptual blending |
| oklab | Better perceptual accuracy |
Note
π― Example With Custom Percentages
Weighted Mix
.button {
background: color-mix(in srgb, #ff0000 80%, white 20%);
}Produces a lighter red β great for soft backgrounds or hover states.
π Example β Creating Theme Shades
Theme Shades Example
.primary-light {
background: color-mix(in srgb, var(--primary) 30%, white 70%);
}
.primary-dark {
background: color-mix(in srgb, var(--primary) 40%, black 60%);
}Use this to generate light/dark variants of your theme colors dynamically.
π¨ Example β Glassmorphism Borders
Glass Border
.glass {
border: 1px solid color-mix(in srgb, white 70%, transparent);
}Creates a frosted translucent border effect.
π§ͺ Example β Better Hover Effects
Hover Effect
button {
background: var(--primary);
transition: 0.3s;
}
button:hover {
background: color-mix(in oklab, var(--primary) 80%, white 20%);
}Slightly brightens the color on hover using perceptually correct mixing.
π Example β Dynamic Color Contrast
Contrast Mix
.text {
color: color-mix(in oklab, black 70%, white 30%);
}Useful for dark mode β ensures readable text.
π§ How color-mix() Distributes Colors
Total percentage does not need to equal 100%. If it's less or more, CSS normalizes it automatically.
Auto-Normalization
color-mix(in srgb, red 20%, blue 20%); /* becomes 50/50 */π Mixing Transparency
Mixing Opacity
background: color-mix(in srgb, rgba(255, 0, 0, 0.8), transparent 30%);Produces a transparent red tint.
π± color-mix() + CSS Variables
Variable Mix
:root {
--brand: #ff5722;
}
.card {
background: color-mix(in srgb, var(--brand) 60%, white 40%);
}Enables dynamic theming across components.
β οΈ Common Mistakes
- β Forgetting to specify color space β inconsistent results
- β Expecting exact hex output (color-mix outputs computed colors)
- β Mixing incompatible spaces without understanding differences
- β Using too much black β quickly turns muddy
- β Forgetting percentages default to 50/50
Note
π₯ Summary
color-mix() is a powerful CSS function that allows developers to blend colors directly in the browser. Whether you're creating themes, hover effects, gradients, or responsive color palettes,color-mix() gives you precise creative control.
Learn more βMDN Docs π