You Might Also Like
CSS color-mix() Playground — Free HTML CSS JS Snippet
CSS color-mix() Playground · Forms · Plain HTML, CSS & JS · Live preview
What's included
Features
About this UI Snippet
CSS color-mix() Playground — Blending Colors With a Native CSS Function

Blending two colors used to be something CSS genuinely could not do on its own — you either pre-computed the blended hex value in a design tool and hard-coded it, or you reached for a preprocessor function like Sass's mix(), or you wrote JavaScript to interpolate RGB channels manually. The color-mix() function, part of the CSS Color Module Level 5 and supported in Chrome/Edge 111+, Safari 16.2+, and Firefox 113+ (full baseline coverage across evergreen browsers since mid-2023), makes color blending a first-class native CSS operation you can use directly in any property that accepts a color — background, border, box-shadow, even inside another color-mix() call.
How color-mix() works technically
The syntax is color-mix(in <color-space>, <color> [<percentage>], <color> [<percentage>]). The first argument names the color space the interpolation happens in — this demo lets you switch between oklch, oklab, srgb, hsl, and lch live so you can see how dramatically the choice of space changes the result. The two colors that follow can each carry an optional percentage; if you specify a percentage on only one color, the other is inferred as the remainder needed to reach 100%, and if you omit both, the browser splits 50/50. This demo always supplies the first percentage explicitly (driven by the slider) and lets the second color's share be implied, exactly matching how color-mix(in oklch, var(--color-a) 30%, var(--color-b)) is typically written in real stylesheets.
Why color space choice actually matters
This is the part most developers new to color-mix() underestimate. Mixing in srgb (the classic RGB channel-averaging approach, similar to what rgba() blending or most JS color libraries default to) frequently produces muddy, desaturated midpoints when the two input colors are far apart on the color wheel — mixing a saturated blue and a saturated pink in sRGB often passes through a grayish-purple dead zone. Mixing in oklch or oklab — perceptually uniform color spaces designed so that equal numeric steps look like equal visual steps to the human eye — produces much more vivid, visually pleasing gradients between the same two endpoints, because the interpolation path follows how humans actually perceive hue and lightness rather than raw channel math. This is exactly why modern CSS color tooling (and this demo) defaults to oklch: it is widely considered the best general-purpose choice for UI color blending in 2025/2026 work, particularly for building tints, shades, and hover-state variants of a brand color programmatically.
Why this matters for modern UI development
Before color-mix(), generating a lighter or darker variant of a brand color (for hover states, disabled states, or a tint scale) required either a Sass build step, a JavaScript color library shipped to the client, or manually authored hex values for every shade — none of which could respond to a CSS custom property changing at runtime. With color-mix(), you can write background: color-mix(in oklch, var(--brand-color) 85%, white) directly in a stylesheet and get a computed 15%-lightened tint that automatically updates if --brand-color changes (for example, via a theme switcher or user-customizable accent color), with zero JavaScript recomputation needed at all.
How this demo wires it together
The two <input type="color"> pickers and the range slider don't compute any color math themselves — every change handler simply builds a literal color-mix(in <space>, <colorA> <ratio>%, <colorB>) string and hands it to document.documentElement.style.setProperty('--mixed-color', mixFunction). The actual pixel-level blending happens entirely inside the browser's rendering engine when it resolves that custom property against .swatch-result { background: var(--mixed-color, ...); }. This is the idiomatic way to make CSS color functions interactive: pass strings into custom properties, and let CSS do the computation.
Build with AI
Build, Understand, Optimize, and Extend It With AI
Paste this snippet into an AI coding assistant like Claude and ask it to explain, in plain terms, why the oklch and srgb results look so different for the same two input colors — it can walk through how each color space defines "halfway between" two colors and why perceptually uniform spaces avoid the muddy-midpoint problem. You could also ask it to add a third color-mix() layer (mixing the result of one color-mix() with a third color) to build a 3-stop gradient generator, or to add a "copy CSS" button that copies the generated color-mix() declaration to the clipboard. It's also a good prompt for a practical extension: ask it to generate a full tint/shade scale (10 steps from white to black) for whatever Color A is currently selected, using this same color-mix() technique.
Prompt to recreate it
Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:
Build an interactive HTML/CSS/JS playground for the CSS color-mix() function that lets a user blend two colors and compare different interpolation color spaces.
Requirements:
- Two native <input type="color"> pickers for a base Color A and Color B, plus a <select> dropdown offering at least these color-mix() spaces: oklch, oklab, srgb, hsl, lch.
- A range slider controlling the mix percentage, with a live text label showing the current split (e.g. "A 30% / B 70%") that updates on every input event.
- Three swatches displayed side by side: Color A raw, the mixed result, and Color B raw, where the mixed swatch's background is set via a real CSS color-mix(in <space>, <colorA> <ratio>%, <colorB>) function string, applied through a CSS custom property using element.style.setProperty — the blend itself must be computed by the browser's CSS engine, not precalculated in JavaScript.
- A visible code panel that displays the exact literal custom property declaration currently being applied (e.g. "--mixed-color: color-mix(in oklch, #6366f1 30%, #f472b6);"), updating live as any control changes, so the underlying mechanism is never hidden.
- All three controls (color A, color B, space dropdown, ratio slider) must independently and correctly update the mixed swatch and code panel on every change, with no stale state.
- Use a neutral palette for the surrounding UI chrome with rounded swatches, clear labels, and smooth background-color transitions on the swatches.
- No external color-manipulation libraries — the only computation JavaScript performs is string-building the color-mix() function call and setting a CSS custom property.Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.
Step by step
How to Use
- 1Pick two base colorsUse the Color A and Color B native <input type="color"> pickers to choose any two colors. Both swatches on either side of the mixed swatch update immediately to show your raw picks.
- 2Drag the mix ratio sliderThe #mix-ratio range input controls what percentage of Color A is used — the label above updates live to show "A 30% / B 70%", and the middle swatch recomputes using color-mix(in oklch, colorA 30%, colorB).
- 3Switch the color spaceChange the "Color space" dropdown between oklch, oklab, srgb, hsl, and lch while keeping the same two colors and ratio. Watch the middle swatch shift — oklch and oklab typically stay vivid through the midpoint, while srgb and hsl often pass through a duller, grayer blend.
- 4Read the generated CSSThe dark panel at the bottom shows the exact --mixed-color custom property value being set, e.g. --mixed-color: color-mix(in oklch, #6366f1 30%, #f472b6);, which is the literal string handed to style.setProperty in the JS panel.
- 5Inspect the custom property in DevToolsOpen your browser DevTools, select the swatch-result element, and look at the Styles/Computed panel for --mixed-color and background — you will see the browser has resolved the color-mix() function to a final computed color, confirming the blend is happening natively in CSS, not in JavaScript.
- 6Apply the pattern in your own stylesheetDefine a brand color as a custom property (--brand: #6366f1;) and derive hover/tint/shade variants with color-mix(in oklch, var(--brand) 85%, white) or color-mix(in oklch, var(--brand) 80%, black) — these recompute automatically if --brand ever changes at runtime.
Real-world uses
Common Use Cases
Got questions?
Frequently Asked Questions
It specifies the color space in which the interpolation math happens — the two input colors are converted into that space's coordinate system, blended proportionally along each axis, and the result is converted back to a displayable color. Different spaces produce visually different blends between the same two endpoints because they define "halfway between" differently; oklch and oklab are perceptually uniform and tend to produce more vivid midpoints than srgb or hsl.
The unspecified color's percentage is inferred as whatever remainder is needed to reach 100% — color-mix(in oklch, red 30%, blue) is equivalent to color-mix(in oklch, red 30%, blue 70%). If you omit both percentages entirely, the browser defaults to an even 50/50 split, which is what this demo starts with before you move the slider.
Yes — color-mix() has shipped in Chrome/Edge since version 111, Safari since 16.2, and Firefox since 113, all released in the first half of 2023, giving it broad coverage across evergreen browsers by 2025/2026. For any audience still on notably older browsers, feature-detect with @supports (background: color-mix(in oklch, red, blue)) and provide a pre-computed fallback color.
srgb and hsl interpolate by averaging raw channel values (red/green/blue, or hue/saturation/lightness) along a straight numeric line, which does not correspond to how the human eye perceives brightness and saturation changes — the midpoint often looks duller or grayer than either endpoint. oklch and oklab were specifically designed so that equal numeric distance corresponds to roughly equal perceived visual distance, so their midpoints tend to stay vivid and look like a genuine "average" of the two hues rather than a washed-out compromise.
Yes, and this is one of its most powerful applications — you can pass a var(--brand-color) as either color argument, as this demo does with --color-a and --color-b, so the mixed result automatically recomputes whenever the underlying custom property changes, whether from a theme toggle, a user-controlled color picker, or a media-query-driven light/dark mode switch, with no JavaScript recalculation required.