lab()

Term side of the Card.
Topic: Colors
Unit title: lab()
Categories: Function
Explanation side of the Card

Explanation: The color function that defines colors using the CIELAB color space, which separates lightness from two color components: 'a' (from green to red) and 'b' (from blue to yellow). It provides a more perceptually uniform way of defining colors, making it easier to achieve consistent color contrasts.

Browser compatibility:
  • The chrome browser supports this feature starting from version 111.
  • The edge browser supports this feature starting from version 111.
  • The firefox browser supports this feature starting from version 113.
  • The safari browser supports this feature starting from version 15.
Availability: 

Example

Wrongcss
.brand {
  color: lab(0.6 40 -20);
}
Rightcss
.brand {
  color: lab(60% 40 -20);
}

In lab() lightness runs from 0 to 100, so a value under 1 is almost black.

Common mistakes

  • Writing lightness as a value under 1, copied from oklab(), which gives a near-black color.
  • Using commas between channels, which lab() doesn't accept.

Key points

  • Lightness is a percentage or a number from 0 to 100, not 0 to 1 as in oklab().
  • Hue changes use lch() or oklch() instead of guessing 'a' and 'b' values.
  • Colors outside sRGB have a fallback declared before them.

Interview questions

What is the difference between lab() and oklab()?

Both use lightness plus 'a' and 'b' axes. Oklab fixes hue shifts that CIELAB has, especially in blues, so it is usually preferred for UI colors.

What is the difference between lab() and lch()?

They use the same color space. lab() uses 'a' and 'b' axes, while lch() expresses them as chroma and a hue angle.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy