Modern CSS colors: oklch() and color-mix()

By

Tagged: CSS

oklch() gives perceptually uniform colors, and color-mix() blends them. Build palettes and tints with modern CSS color functions in all major browsers.

Modern CSS gives us two tools that work well together for color:

  • oklch() lets you define and adjust source colors using lightness, chroma, and hue
  • color-mix() derives new colors from those sources at runtime

Together they make it much easier to build consistent palettes, hover states, borders, backgrounds, and dark themes.

If CSS color syntax and custom properties are new to you, start with the free CSS course.

Color Converter & Contrast

Convert CSS colors between hex, rgb, hsl, and oklch, then check WCAG 2.1 contrast ratios for text on a background.

Color converter & contrastInteractive · runs in your browser

Color input

Accepts hex, rgb(), hsl(), oklch(), and common named colors.

Could not parse that color. Check the syntax.

Swatch

All formats

FormatValue

WCAG contrast

Set foreground and background colors. WCAG 2.1 ratios: AA normal 4.5:1, AA large 3:1, AAA normal 7:1, AAA large 4.5:1.

Fix the foreground or background color syntax.

Normal body text at 16px.

Large bold text at 18px+.

Contrast ratio:

AA normalAA largeAAA normalAAA large

OKLCH conversions use the published OKLab math from CSS Color Level 4. Everything runs in your browser.

Why hex is difficult to adjust

A hex color like #2563eb is compact, but its channels describe red, green, and blue light:

.button {
  background: #2563eb;
}

You can’t look at that value and know how to make it 15% lighter. Most of the time you end up opening a color picker and pasting in another unrelated hex code.

RGB has the same problem, just written out explicitly:

.button {
  background: rgb(37 99 235);
}

HSL is easier to read, but its lightness channel isn’t perceptually uniform. A yellow and a blue with the same HSL lightness don’t look equally bright.

oklch() fixes that by tying its lightness channel much more closely to how our eyes perceive brightness.

Read an oklch() color

An OKLCH color has three channels:

.button {
  background: oklch(60% 0.2 255);
}

Read those three values as:

  • 60% is lightness, from black at 0% to white at 100%
  • 0.2 is chroma, or how colorful the result is (0 is gray, and higher numbers get more vivid until they pass what the display can show)
  • 255 is the hue angle, wrapping around at 360 degrees

When chroma is 0, the color is pure gray and the hue angle has no visible effect:

.neutral {
  color: oklch(60% 0 250);
}

Changing 250 won’t alter that gray at all because there’s no chroma to carry a hue.

You’ll usually see chroma written as a number between 0 and about 0.4. CSS accepts a percentage too, though 100% chroma maps to 0.4, not 1. I stick to the numeric form since that’s what most OKLCH palettes use.

You can also add an optional alpha channel:

.overlay {
  background: oklch(20% 0.04 255 / 70%);
}

If you want a refresher on CSS color basics first, read the CSS color guide.

Build a lightness scale

Start with one brand color:

:root {
  --brand: oklch(60% 0.2 255);
}

From there, build a small scale that keeps the same general hue:

:root {
  --brand-100: oklch(95% 0.03 255);
  --brand-300: oklch(80% 0.1 255);
  --brand-500: oklch(60% 0.2 255);
  --brand-700: oklch(42% 0.16 255);
  --brand-900: oklch(25% 0.08 255);
}

Notice that chroma drops at the ends. Very light and very dark shades usually need less chroma than the base brand color, because keeping chroma high across every step pushes colors outside what most displays can render.

Then wire the scale into your components:

.notice {
  color: var(--brand-900);
  background: var(--brand-100);
  border: 1px solid var(--brand-300);
}

OKLCH makes these shades much easier to tune by eye and by number, so check each pairing’s contrast before calling the scale done.

Convert an existing color without redesigning everything

You don’t need to throw away your current palette and start over.

Take a color you already use:

:root {
  --brand: #2563eb;
}

Convert it to OKLCH in browser DevTools or a color converter, then store that as your new source token:

:root {
  --brand: oklch(54.6% 0.215 263);
}

The exact numbers depend on your starting color. Keep the converted value as your baseline and adjust one channel at a time.

I follow four steps when migrating a token:

  1. match the current color
  2. adjust lightness for its role
  3. pull back chroma if the color clips
  4. test contrast inside the real component

That keeps your existing visual direction intact while giving you values you can tune predictably.

Create tints with color-mix()

color-mix() blends two colors in the browser at runtime:

.button {
  background: var(--brand);
}

.button:hover {
  background: color-mix(in oklch, var(--brand) 85%, white);
}

That gives you 85% of the brand color and 15% white.

Leave out one percentage and the browser fills in the remainder, so these two declarations produce the same result:

color-mix(in oklch, var(--brand) 85%, white 15%)
color-mix(in oklch, var(--brand) 85%, white)

Leave out both and you get a 50/50 mix:

color-mix(in oklch, var(--brand), white)

Always write out the color space. Mixing in oklch and mixing in srgb take different paths between the same two colors, and even though modern CSS has a default mixing space, writing in oklch makes your intent clear to anyone reading the stylesheet.

Percentages are weights

When only one color has a percentage, the other gets whatever is left to reach 100%:

color-mix(in oklch, var(--brand) 80%, white)

That’s an 80/20 mix.

When neither color has a percentage, each contributes 50%:

color-mix(in oklch, var(--brand), white)

If both percentages add up to more than 100%, the browser scales them back down proportionally, so these weights resolve to the same ratio:

color-mix(in oklch, red 60%, blue 40%)
color-mix(in oklch, red 90%, blue 60%)

Don’t use oversized weights to try to make a color brighter. Keep the pair adding up to 100% whenever you can so the rule stays readable.

Watch out for the opposite case too: when the two percentages add up to less than 100%, the leftover portion turns into alpha transparency, even if both input colors are completely opaque.

Derive component colors from one token

Store your base color once:

:root {
  --accent: oklch(62% 0.18 145);
}

Then derive the related component colors from it:

:root {
  --accent-hover: color-mix(
    in oklch,
    var(--accent) 82%,
    black
  );

  --accent-soft: color-mix(
    in oklch,
    var(--accent) 15%,
    white
  );

  --accent-border: color-mix(
    in oklch,
    var(--accent) 45%,
    white
  );
}

Use those derived tokens across your rules:

.tag {
  color: var(--accent-hover);
  background: var(--accent-soft);
  border: 1px solid var(--accent-border);
}

Update --accent in one place and every related state follows along. This is one of the best uses of CSS variables: a small set of source tokens driving derived component values.

Mix toward the surface, not always white or black

Mixing with white or black is quick, but most components sit on a specific surface color.

Define that surface token explicitly:

:root {
  --surface: oklch(98% 0.01 255);
  --accent: oklch(62% 0.18 145);
}

Then mix your accent toward the surface instead of hardcoding white:

.notice {
  background: color-mix(
    in oklch,
    var(--accent) 12%,
    var(--surface)
  );
}

Now the soft background tracks --surface automatically. When dark mode swaps --surface to a dark shade, the component stops blending toward pure white, which makes themes far more predictable than scattering white and black through your styles.

Mix with transparent carefully

Mixing with transparent gives you a translucent color:

.overlay {
  background: color-mix(
    in oklch,
    var(--brand) 60%,
    transparent
  );
}

That doesn’t always look the same as blending your brand color with the page background, because whatever sits behind the element will bleed through.

If the element sits on a known light background and you want a solid tint, mix with that surface color instead:

.panel {
  --surface: white;
  background: color-mix(
    in oklch,
    var(--brand) 10%,
    var(--surface)
  );
}

Reach for transparent when you want underlying content or gradients to show through, and mix with an explicit surface color when you want an opaque, predictable result.

Create a dark theme

color-mix() pairs well with theme tokens that switch between light and dark modes:

:root {
  --surface: oklch(98% 0.01 255);
  --text: oklch(22% 0.02 255);
  --accent: oklch(60% 0.2 255);
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface: oklch(18% 0.02 255);
    --text: oklch(92% 0.01 255);
    --accent: oklch(72% 0.14 255);
  }
}

Don’t build dark mode by mechanically flipping every lightness value. Dark interfaces usually look better with lower chroma, softer borders, and surfaces picked specifically for dark screens.

Once your theme tokens are set up, derive component colors from them:

.button {
  color: var(--surface);
  background: var(--accent);
  border-color: color-mix(
    in oklch,
    var(--accent) 70%,
    var(--text)
  );
}

The same component rules now adapt automatically to whichever theme is active.

Watch the display gamut

OKLCH can describe colors that standard sRGB displays can’t reproduce, especially when you push chroma high:

.very-vivid {
  color: oklch(70% 0.4 145);
}

Inspect values like this in browser DevTools to see whether they fall outside sRGB. Wide-gamut monitors can render more vivid shades, so make sure your palette still looks right on ordinary displays and pull chroma back if a color clips or shifts unexpectedly.

Gamut mapping can change the final color

When a color falls outside what the user’s screen can show, the browser maps it to the closest displayable value. That mapping reduces chroma and can nudge the final hue slightly from what you asked for.

Because of that, bumping chroma up doesn’t always give you a more vivid result:

.swatch-one {
  background: oklch(70% 0.25 145);
}

.swatch-two {
  background: oklch(70% 0.4 145);
}

On a wide-gamut display, those two values can look noticeably different. On an sRGB display, both may land right next to the same gamut boundary.

Instead of maxing out chroma, pick the lowest value that still delivers the look you want across the screens your visitors use.

Color does not guarantee contrast

Perceptual lightness makes palettes much more consistent, and you still need to check contrast on every real text-and-background pair.

Two colors with very different OKLCH lightness values can still fail readability checks once font size, font weight, transparency, and the rendered display color come into play. Test normal, hover, active, and disabled states in both light and dark themes.

Pay special attention to derived colors. Mixing an accent toward the surface for a hover state can pull contrast below target even when the base accent passes. Check focus rings, disabled labels, and borders that communicate state the same way.

Add a fallback when you need one

Modern browsers support both oklch() and color-mix(), and adding a fallback for older environments takes only a line:

.button {
  background: #2563eb;
  background: oklch(60% 0.2 255);
}

Browsers that don’t understand oklch() ignore the second line and stick with the hex value.

For derived colors, wrap the color-mix() rules in @supports:

.button:hover {
  background: #1d4ed8;
}

@supports (color: color-mix(in oklch, red, blue)) {
  .button:hover {
    background: color-mix(
      in oklch,
      var(--brand) 82%,
      black
    );
  }
}

Always put the fallback first. CSS applies the last valid declaration it understands, so newer browsers pick up the modern color while older ones keep the fallback.

If you’re working with a full set of tokens, group the fallbacks together at the token level:

:root {
  --brand: #2563eb;
  --brand-soft: #dbeafe;
}

@supports (color: oklch(50% 0.1 200)) {
  :root {
    --brand: oklch(54.6% 0.215 263);
    --brand-soft: color-mix(
      in oklch,
      var(--brand) 15%,
      white
    );
  }
}

Your components just read the tokens, keeping all the compatibility logic in one place.

Common mistakes

A few pitfalls come up regularly when switching to oklch() and color-mix():

  • Skipping contrast checks because the lightness numbers look far apart. Always verify text contrast and rendered colors across screens rather than trusting the numbers alone.
  • Keeping the same high chroma at every lightness step. Very light and very dark shades usually need lower chroma to stay inside the display gamut.
  • Mixing with transparent when you wanted a solid tint. Anything behind the element will show through and shift the final color.
  • Generating every UI state with a fixed percentage formula and never inspecting it. A hover, active, focus, disabled, or dark-mode color can be mathematically consistent and still look wrong on screen.
  • Leaving out the mixing color space. Writing in oklch (or another explicit space) makes the interpolation path intentional and visible in the code.

How I would introduce these colors

Don’t convert a whole site in one big pass.

Start with a single accent token and one component. Derive its hover and soft-background states with color-mix(), then test contrast in both light and dark themes.

Once that small setup works well, move your main source colors to oklch() so lightness and chroma become deliberate choices, keeping hex fallbacks underneath if you still need older browser support. That keeps the diff easy to review and lets you confirm derived colors help your design before every component relies on them.

For nesting, :has(), and the rest of the no-build-step list, see 12 CSS features you can use today.

Want me to talk about your product? You can sponsor this site.

Related posts about css: