← Back to HTML & CSS, page 4

html-css-0076 · Medium mid

What do color-mix() and oklch() add to CSS color, and how do you use them with a fallback?

color-mix() blends two colors in a chosen color space, so shades can be derived from one brand color. oklch() describes color by lightness, chroma and hue with more even perceived brightness than hsl(). Provide a plain fallback value before the newer declaration.

Full answer

Two newer parts of CSS color make theming easier.

color-mix() mixes two colors in a chosen color space: color-mix(in srgb, #0a6cff 30%, white) gives a lighter tint made of 30 percent blue and 70 percent white. You can build hover and disabled shades from one brand color without picking each by hand, and the result updates when the brand variable changes.

oklch() describes color by lightness, chroma (how colorful) and hue. Its lightness matches how people see brightness better than hsl() does, so two colors with the same lightness number look about equally bright. That makes accessible palettes easier to plan. It can also express colors outside the old sRGB range on screens that support them. Another new feature, relative color syntax, such as oklch(from var(--brand) calc(l + 0.1) c h), computes a variant of an existing color, but it is newer still.

Because these are newer, give a fallback. A normal declaration before the new one works, because browsers drop declarations they do not understand and keep the earlier value. A feature query is also possible: @supports (color: color-mix(in srgb, red, blue)).

The button gets a lighter blue on hover, made from the brand variable. The panel uses a 10 percent tint where color-mix() is understood and a plain light blue otherwise. The last box mixes red and blue equally and gives a purple.

When you use any of these, check text contrast on the final color, not only on the base color.

html
<style>
  :root { --brand: #0a6cff; }
  .btn {
    padding: 0.5rem 1rem; border: 0; color: #fff;
    background: #0a6cff;
    background: var(--brand);
  }
  .btn:hover {
    background: #3d8bff;
    background: color-mix(in srgb, var(--brand) 80%, white);
  }
  .tint {
    background: #e6f0ff;
  }
  @supports (color: color-mix(in srgb, red, blue)) {
    .tint { background: color-mix(in srgb, var(--brand) 10%, white); }
  }
  .oklch { background: oklch(0.6 0.2 260); color: #fff; padding: 0.5rem; }
  .purple { background: color-mix(in srgb, red 50%, blue 50%); padding: 0.5rem; }
</style>
<button class="btn" id="btn" type="button">Enrol</button>
<div class="tint" id="tint">Tinted panel</div>
<div class="oklch" id="ok">oklch color</div>
<div class="purple" id="mix">Half red, half blue</div>

Checked in Chrome 156 only. The fallback lines were not run in an older browser. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • color-mix(in srgb, A 30%, B) blends two colors by percentage.
  • oklch() uses lightness, chroma and hue.
  • Derived shades update when the base variable changes.
  • Write a normal value first, then the newer one, or use @supports.
  • Check contrast of the final color.

Common mistakes

  • Using color-mix() with no fallback, so older browsers show no background at all.
  • Choosing hover colors by eye in several files instead of deriving them from one variable.
  • Assuming equal hsl() lightness means equal visual brightness. It does not, which oklch() improves.

Follow-ups

  • How do you give a fallback for a CSS function the browser does not know?
  • Why is oklch useful for building accessible palettes?

Tips

  • Say that you provide a normal value first and the new value after it, as a simple fallback.
  • Explain oklch in one line: lightness, chroma, hue, with more even brightness than hsl.