CSS color-mix() Function Output Fix
In this tutorial, you'll learn about CSS color. We cover key concepts, practical examples, and best practices.
The Problem
The color-mix() function mixes two colors in a specified color space. Unexpected results occur when the wrong color interpolation method is used, when mixing across different color spaces, or when the percentage weights do not sum to expected values.
Quick Fix
Step 1: Use the correct color interpolation method
/* Wrong — default interpolation may produce muddy results */
.element {
background: color-mix(in srgb, red 50%, blue 50%);
/* Result: a muddy purple */
}
/* Right — use perceptual interpolation */
.element {
background: color-mix(in oklab, red 50%, blue 50%);
/* Result: a vibrant purple */
}
Step 2: Understand percentage behavior
/* Wrong — percentages do not need to sum to 100% */
.element {
background: color-mix(in srgb, red 75%, blue 75%);
/* Percentages are normalized: 75/150 = 50% each */
}
/* Right — use complementary percentages */
.element {
background: color-mix(in oklab, red 75%, blue 25%);
/* 75% red, 25% blue — no normalization needed */
}
Step 3: Mix colors from different formats
/* Wrong — mixing hex and hsl without specifying */
.element {
background: color-mix(in srgb, #ff0000, hsl(240, 100%, 50%));
/* Works but uses srgb interpolation */
}
/* Right — use oklab for consistent perceptual mixing */
.element {
background: color-mix(in oklab, #ff0000, hsl(240, 100%, 50%));
}
Step 4: Use color-mix for theming
:root {
--primary: #6366f1;
--bg: #f8fafc;
}
/* Wrong — hardcoded hover color */
.button {
background: var(--primary);
}
.button:hover {
background: #4f46e5;
}
/* Right — mix with black/white for variants */
.button {
background: var(--primary);
}
.button:hover {
background: color-mix(in oklab, var(--primary), black 15%);
}
.button:active {
background: color-mix(in oklab, var(--primary), black 30%);
}
Step 5: Mix with transparency
/* Wrong — alpha channel may not blend as expected */
.element {
background: color-mix(in srgb, rgba(255,0,0,0.8), blue);
}
/* Right — use oklab for better alpha blending */
.element {
background: color-mix(in oklab, rgba(255,0,0,0.8), blue);
}
/* Or handle alpha separately with oklch */
.element {
background: color-mix(in oklch, red 50%, blue 50%);
}
Prevention
- Prefer
oklaboroklchcolor spaces for perceptual color mixing - Use complementary percentages (e.g., 70% and 30%) instead of over-100% values
- Use
color-mixwith custom properties for dynamic theming - Test mixed colors in multiple color spaces to find the best result
- Use browser DevTools color picker to preview mixed values
Common Mistakes with color mix
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world CSS code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro