Skip to content
Tutorials Center
My learning

Topics

Lesson 23 of 139 ยท Values and Units

Modern Color: oklch() and color-mix()

Learn modern CSS color with oklch(), wide-gamut display-p3, color-mix(), relative color syntax and light-dark(), with honest notes on browser support.

For about twenty years, every CSS color was a mix of red, green and blue in one fixed color space called sRGB. Hex, rgb() and hsl() are all just different ways of writing an sRGB color. That worked, but it had two limits: sRGB can't show the vivid colors that modern screens can display, and its numbers don't match how bright colors actually look to people.

Modern CSS fixes both. In this lesson you'll learn oklch(), the color format many design systems now use, how to reach wide-gamut colors, and three tools that let CSS compute colors for you: color-mix(), relative color syntax and light-dark(). Each section says plainly how well the feature is supported.

The trouble with hsl()

In Colors you saw that hsl() makes it easy to change a hue. But look what happens when you keep saturation and lightness the same and only change the hue:

Same lightness, different brightness

In the first row, the yellow and cyan glow while the blue looks almost dark. HSL's "lightness" is a formula on RGB numbers, not a measure of what your eye sees. In the second row, every swatch has about the same visual weight. That's what a perceptual color space gives you.

oklch(): lightness, chroma, hue

oklch() is built on the Oklab color space, designed so that equal steps in its numbers look like equal steps to people. It takes three values:

CSS
color: oklch(0.62 0.19 260);          /* a strong blue */
color: oklch(62% 0.19 260 / 0.5);     /* same, with 50% alpha */
  • L, lightness: from 0 (black) to 1 (white), or 0% to 100%. Unlike HSL, it matches perceived brightness, so all colors with L 0.7 look equally light.
  • C, chroma: how colorful. 0 is gray; most usable colors sit between about 0.05 and 0.25. There's no fixed maximum: how high you can go depends on the hue and the screen.
  • H, hue: an angle in degrees, roughly: red around 25, orange 60, yellow 95, green 145, cyan 195, blue 260, purple 305.

Because lightness is reliable, oklch is excellent for building palettes. Keep the hue, step the lightness, and every shade is evenly spaced. Accessible contrast also becomes predictable: text with L below about 0.45 on a background above about 0.9 will almost always pass.

oklch(), along with oklab(), lab() and lch(), is supported in all major browsers since 2023. If you need to support older browsers, write a hex or rgb() fallback on the line before.

oklab(L a b) is the same color space with rectangular coordinates (a: green to red, b: blue to yellow) instead of chroma and hue. It's useful for mixing, but harder to write by hand, so most people author in oklch.

Wide-gamut colors

sRGB covers only part of the colors that recent phones, laptops and monitors can show. Many of these screens support the Display P3 gamut, which includes noticeably more vivid reds, greens and oranges.

You can reach those colors in two ways:

CSS
.accent {
  color: oklch(0.65 0.29 145);              /* high chroma: beyond sRGB */
  background: color(display-p3 1 0.2 0.1);  /* explicit P3 coordinates */
}

When a color is outside what the screen can show, the browser maps it to the closest color it can display, so nothing breaks; people with better screens just see a richer color. If you want to target wide-gamut screens on purpose, there's a media query:

CSS
@media (color-gamut: p3) {
  .badge { background: oklch(0.7 0.25 30); }
}

color-mix(): blend two colors

color-mix() mixes two colors in a color space you choose:

CSS
color-mix(in oklch, #4f46e5 70%, white)    /* 70% indigo, 30% white */
color-mix(in srgb, red, blue)              /* 50/50 by default */
color-mix(in oklab, var(--brand) 40%, transparent)   /* a see-through brand color */

The percentages say how much of each color goes in. If you give only one, the other gets the rest. Mixing with white makes tints, with black makes shades, and with transparent gives you a translucent version of any color, even one stored in a variable. The color space changes the result: oklab and oklch usually give the most natural blends, while srgb matches how older tools mixed colors.

Tints and shades with color-mix()

Pick any color and the whole palette updates: seven shades from one value, with no build step. (The demo stores the color in a custom property, which you'll learn about two lessons from now.)

color-mix() works in all major browsers since 2023.

Relative color syntax

Relative color syntax lets you take an existing color, break it into channels, and build a new color from them. You write from <color> first, and the channel names (l, c, h for oklch, r, g, b for rgb, and so on) become variables you can use or change:

CSS
:root { --brand: oklch(0.55 0.2 265); }

.button {
  background: var(--brand);
  /* the same color, but lighter */
  --brand-hover: oklch(from var(--brand) calc(l + 0.1) c h);
  /* the complementary hue */
  --brand-opposite: oklch(from var(--brand) l c calc(h + 180));
  /* the same color at 30% alpha */
  --brand-soft: oklch(from var(--brand) l c h / 0.3);
}

The input color can be in any format: rgb(from #3b82f6 r g b / 50%) works too.

Support: relative color syntax reached all major browsers only in 2024 (Safari was last, in version 18), and browsers from before that ignore it. Some earlier Safari versions supported an older draft, which treated the channel values a little differently. Always give a fallback first, as in the next example.

Hover color derived from the base

Hover each button: one rule gives every variant its own matching hover color.

light-dark(): two colors in one value

light-dark(lightValue, darkValue) picks the first color when the element is in a light color scheme and the second in a dark one. It only works when the page says it supports both schemes with the color-scheme property:

CSS
:root {
  color-scheme: light dark;   /* required for light-dark() */
}

body {
  background: light-dark(#ffffff, #0f172a);
  color: light-dark(#1e293b, #e2e8f0);
}

The browser follows the user's system setting. You can also force a scheme on part of the page by setting color-scheme: dark on it, which is how theme toggles work:

light-dark() with a scheme toggle

The checkbox and the scrollbar also switch to their dark look, because color-scheme tells the browser to restyle its built-in controls. light-dark() is supported in all major browsers since 2024. The Dark Mode lesson covers full theming.

Which format should you use?

  • Picking from a design file? Hex is fine. Every format can mix with every other.
  • Building a palette or design tokens? oklch() gives even steps and predictable contrast.
  • Deriving hover, border or tint colors? color-mix() is well supported today; relative color syntax is more precise, with a fallback.
  • Supporting light and dark themes? light-dark() with color-scheme, or custom properties.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why do hsl(60 100% 50%) and hsl(240 100% 50%) look so different in brightness?

    HSL is a simple transform of sRGB, so equal lightness numbers can look very different. oklch's lightness is perceptual.
  2. 2. In oklch(0.7 0.12 145), what does 0.12 control?

    oklch takes lightness, chroma and hue, in that order.
  3. 3. What does color-mix(in oklab, #2563eb 20%, transparent) produce?

    Mixing with transparent keeps the hue and reduces alpha in proportion: 20% of the blue gives a see-through blue.
  4. 4. What is required for light-dark() to switch colors?

    light-dark() reads the used color scheme, which comes from the color-scheme property.
  5. 5. Why write a color-mix() declaration before oklch(from var(--base) ...)?

    An unsupported value invalidates only its own declaration, so the earlier one acts as a fallback.

Summary

  • oklch(L C H / alpha) describes colors by perceived lightness, chroma and hue, which makes palettes and contrast predictable.
  • High-chroma oklch and color(display-p3 ...) reach wide-gamut colors; browsers map them to what each screen can show.
  • color-mix(in oklab, a X%, b) makes tints, shades and translucent versions of any color.
  • Relative color syntax (oklch(from var(--c) calc(l + 0.1) c h)) derives new colors from existing ones; it's newer, so add a fallback.
  • light-dark() picks a color per color scheme and needs color-scheme to be set.

Next: calc(), min(), max() and clamp() โ€” doing math in CSS and building fluid sizes.