Skip to content

CSS color-mix() Function Output Fix

DodaTech Updated 2026-06-24 3 min read

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 oklab or oklch color spaces for perceptual color mixing
  • Use complementary percentages (e.g., 70% and 30%) instead of over-100% values
  • Use color-mix with 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

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. 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

### What color interpolation method should I use?

Use in oklab or in oklch for the most perceptually uniform results. Use in srgb for backward compatibility. Use in hsl or in hwb when you want to interpolate specifically in hue-saturation or hue-whiteness spaces.

Can I mix more than two colors?

No. color-mix() only accepts two colors. Mix multiple pairs sequentially or use a preprocessor like Sass for multi-color mixing. CSS custom properties can store intermediate results.

Does color-mix work with currentColor?

Yes. currentColor can be used as one of the colors in color-mix(). This is useful for creating transparent or tinted variants of the current text color without JavaScript.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro