Loading video player…

Mix Tints With color-mix()

In this lesson, you mixed a pale tint, a shade and a see-through version of Bea’s leaf green from the brand token, --green-50, instead of typing each one by hand. Your hand-typed scale can’t follow a change to the brand green, and it has no see-through step, but mixed tokens do both. You read each result’s lightness in the Computed pane of Chrome DevTools.

Key Points

  • color-mix() takes a color space first, then two colors. The percentage after a color is that color’s share, and the other color gets the rest. So var(--green-50) 80%, white is 80% green and 20% white.
  • Oklab is the space that oklch() is built on. Its first number is the same lightness, and the other two set the hue and how colorful the color is. The Computed pane shows an oklab mix with its lightness first, written from 0 to 1, so 0.6 is the 60% you’d write in oklch().
  • In oklab, even shares give even tints. With the green at lightness 0.5, mixing 80%, 50% and 20% green with white gives lightness 0.6, 0.75 and 0.9. Each step of 30% moves the lightness by the same 0.15.
  • Mixing with black gives a shade. Half green and half black in oklab lands at lightness 0.25, halfway from the green to black.
  • Mixing with transparent turns the percentage into opacity. The result keeps the green’s color. The number after the slash, as in / 0.3, is its opacity, so whatever sits behind a card’s border shows through it.
  • The space you name decides how the colors blend. The same half-and-half shade comes out lighter in srgb, at about 0.31, because srgb averages the red, green and blue numbers the screen stores, and those don’t step evenly in lightness. Oklab and oklch both average the same lightness, so they agree on 0.25. Mixed with black, the whole color matches too. Black has no chroma, so in both spaces the green keeps its hue.
  • If you leave the space out, the browser mixes in oklab, not srgb. Write the space anyway, because older browsers don’t support the form without it.
  • Every mixed token reads the brand token, so changing --green-50 changes the tint, the shade and the see-through green with it.

The Final Tokens

Under the brand green on :root, you added the three mixed primitives and two new roles that point at them. --color-border stays as it was, so the borrow form’s fields keep their solid green:

Language: CSS
:root {
  --green-50: oklch(50% 0.12 140);
  --green-tint: color-mix(in oklab, var(--green-50) 20%, white);
  --green-shade: color-mix(in oklab, var(--green-50) 50%, black);
  --green-glass: color-mix(in oklab, var(--green-50) 30%, transparent);

  --color-border: var(--green-50);
  --color-surface-highlight: var(--green-tint);
  --color-card-border: var(--green-glass);
}

The “Returns week” banner and the seed cards read their new roles:

Language: CSS
.returns-banner {
  background: var(--color-surface-highlight);
}

.seed-card {
  border: 2px solid var(--color-card-border);
}

Bea’s hover colors are still picked by hand. Next, in Derive Hover Shades From a Token, you’ll build those from the brand green too.

00:00 Bea’s leaf green now lives in your stylesheet as an oklch() value, with a scale of lighter and darker steps that you picked by lightness. Now Bea wants a pale green behind the Returns week banner, a see-through green for the borders of the seed cards, and a darker green to go with them.

00:18 Your scale has pale greens, but you typed each one by hand, so they won’t follow if the brand green changes, and none of them is see-through. Instead, you’ll mix them from the green you already have.

00:32 Under the brand green, add a tint. color-mix() takes a color space first, and here that’s oklab, the space that oklch() is built on.

00:42 Its first number is the same lightness. The other two set the hue and how colorful it is. That’s all you need about oklab for now. Then come the two colors, the brand green at 80% and white.

00:55 The percentage is the green’s share, and white gets the rest. Give the banner a role of its own that points at the tint. The banner turns green, a step lighter than the brand green.

01:07 In the Computed pane, its first number is the lightness, written from 0 to 1, so 0.6 is the 60% you know.

01:17 Drop the green to 50%. The lightness lands on 0.75, halfway from the green’s 0.5 to white’s 1.

01:27 Now give the green 20%. Your turn: pause the video and predict the banner’s lightness.

01:39 It’s 0.9. Each step of 30% moves the lightness by the same amount, so in oklab, even shares give even tints. That’s the pale green Bea wanted.

01:53 For the darker green, mix with black instead, and you get a shade. Half green and half black gives a lightness of 0.25, halfway down from the green to black.

02:05 Mix with transparent, and the percentage turns into opacity. Give the seed cards a border role of their own, so the form’s fields keep their solid green.

02:15 The green keeps its color. The number after the slash, 0.3, is its opacity, so whatever sits behind a card’s border shows through it.

02:26 The space you name decides how the colors blend. Here’s the same half-and-half shade three times, once in each space. The sRGB mix comes out lighter, at 0.31.

02:39 It averages the red, green and blue numbers that the screen stores, and those don’t step evenly in lightness. Both oklab and OKLCH average the same lightness, so they agree on 0.25.

02:53 Here, the whole color matches too. Black has no chroma, so in both spaces the green keeps its hue. Watch out: if you leave the space out, the browser mixes in oklab, not sRGB.

03:07 Write the space anyway, because older browsers don’t support the form without it.

03:13 Here’s the rule: color-mix() gives the first color its percentage and the second color the rest, blended in the space you name. Name oklab, and equal shares give equal steps.

03:26 Bea’s tint, shade and see-through green now come from one token. Change the brand green, and all three follow. Her hover colors are still picked by hand, though.

03:37 Next, in Derive Hover Shades From a Token, you’ll build those from the brand green too.

Become a Member to join the conversation.