Colors and Color Spaces

Beginner · 9 min

How CSS describes color, why HSL's lightness lies, and how oklch(), color-mix() and relative colors let you build a palette from one value.

Before thisValues, Units and Math Functions

Academind · 9 min

Colors and Color Spaces

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by AcademindReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A color space is a coordinate system for colors. `rgb()`, `hsl()` and `hwb()` are all views of the sRGB gamut, the range most screens could show in the 1990s.
  • HSL's lightness is not perceived lightness: `hsl(60 100% 50%)` (yellow) is far brighter to the eye than `hsl(240 100% 50%)` (blue), though both say 50%. `oklch()` is built so that equal `L` looks equally light.
  • `oklch(L C H / alpha)` takes lightness (0 to 1), chroma (roughly 0 to 0.4) and a hue angle in degrees. Changing only `H` keeps brightness steady, which is what makes palettes easy.
  • `color-mix(in oklch, var(--brand) 70%, white)` blends two colors in the space you name, and `oklch(from var(--brand) calc(l + 0.1) c h)` derives a new color from an existing one.
  • `color(display-p3 1 0 0)` reaches colors sRGB cannot show. Pair it with a normal fallback and `@media (color-gamut: p3)`.

A color is a point in a space

Every screen mixes red, green and blue light, so the first color syntax CSS had was three numbers: how much of each. rgb(255 0 0) is full red. Hex is the same three numbers in base 16. Because humans don’t think in “how much red”, hsl() came next: a hue angle, a saturation and a lightness. It is the same set of colors with a friendlier set of dials.

That set of colors is called the sRGB gamut, and the three-number system is a color space. Think of a color space as a map with its own grid. Two maps can describe the same place with different coordinates, and some places only exist on the bigger map.

Why HSL’s lightness lies

The L in hsl() sounds like brightness, but it is only the midpoint of the largest and smallest RGB channel. Check it:

.yellow { background: hsl(60 100% 50%); }  /* rgb(255 255 0) */
.blue   { background: hsl(240 100% 50%); } /* rgb(0 0 255)   */

Both say 50%. By the WCAG relative luminance formula, the yellow comes out around 0.93 and the blue around 0.07. Put white text on each and one passes contrast checks while the other fails badly. If you build a palette by rotating H and keeping S and L, the swatches jump in brightness as the hue passes through yellow and blue.

oklch: lightness you can trust

oklch() is a space designed around how we see, so equal L looks equally light:

:root {
  --green: oklch(0.7 0.15 150);
  --red:   oklch(0.7 0.15 30);   /* same L and C, different hue */
  --blue:  oklch(0.7 0.15 250);
}

The three channels are:

  • L, lightness, from 0 (black) to 1 (white). 70% works too.
  • C, chroma, how colorful it is. 0 is gray; real colors top out around 0.37, depending on hue and lightness.
  • H, a hue angle in degrees.

All three swatches above look the same brightness. That is why design systems are moving to it: a dark mode can reuse a hue and move only L, and a text color’s contrast stays stable when you change the brand hue. Alpha uses the slash form shared by all modern color functions: oklch(0.7 0.15 150 / 50%). This is also the syntax Academind’s title means by “modern”: spaces between values and no commas, so rgb(0 0 0 / 0.5) rather than rgba(0, 0, 0, 0.5). The old form still works.

oklch is Baseline, available in every major browser since May 2023. Not every value is displayable: if you ask for more chroma than the screen can produce, the browser maps it back into range for you.

Building palettes with color-mix() and relative colors

Once colors have sensible coordinates, deriving them becomes arithmetic. color-mix() blends two colors in a space you name:

:root { --brand: oklch(0.6 0.2 250); }

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

The space matters because mixing means interpolating coordinates. Mix red and blue in srgb and the midpoint is a flat rgb(128 0 128); mix in oklch and the path follows the hue wheel, so the midpoint stays vivid (about oklch(0.54 0.29 327)). Always write in <space> yourself.

Relative colors go a step further. They take an existing color and let you rewrite one channel:

.button:hover { background: oklch(from var(--brand) calc(l + 0.1) c h); }
.muted        { color: oklch(from var(--brand) l calc(c * 0.3) h); }

After from <color> the color is converted to the function’s channels, which become the keywords l, c and h inside the parentheses. In a browser, the first rule resolves to oklch(0.7 0.2 250) when --brand is oklch(0.6 0.2 250). Relative colors became available across browsers in September 2024, so they are newly available rather than long-settled; a plain fallback line above is cheap insurance.

Wider than sRGB: display-p3

Modern phones and laptops show colors sRGB cannot name, such as a really saturated green. color(display-p3 0 1 0) addresses the Display P3 space directly. Give older displays something to fall back to:

.hero { background: rgb(0 200 80); }

@media (color-gamut: p3) {
  .hero { background: color(display-p3 0.1 0.85 0.4); }
}

oklch() can also express these colors, since it covers a bigger range than any current screen.

What the video covers

Academind’s “Making Sense of OKLCH() & Modern CSS Colors” runs about nine minutes and was published in January 2026. Its description says it shows how to define CSS colors with oklch() and that it is well supported. I could not read a transcript, so the specific examples are not listed here. The HSL comparison, color-mix(), relative colors and the P3 fallback in this lesson are the prose’s own. The runner-up links go further on why OKLCH is perceptually uniform and on relative-color palettes.

Where this fits

Colors are values, so the unit and function rules in values, units and math functions apply to them, including calc() inside a color. The next lesson, backgrounds and gradients, uses these colors as gradient stops, and user preference queries and dark mode uses them to define light and dark themes.

Check yourself

  1. 01You set `hsl(60 100% 50%)` and `hsl(240 100% 50%)` for two buttons and the yellow one looks much brighter. Is something broken?

    No. HSL lightness is a mathematical midpoint of the RGB channels, not a measure of how bright the color looks. Yellow's red and green channels are both at maximum, which the eye reads as very bright. Blue's single channel reads as dark. `oklch()` fixes this because `L` is calibrated to perceived lightness.

  2. 02What does the `from` keyword do in `oklch(from var(--brand) calc(l + 0.1) c h)`?

    It converts the origin color (`--brand`) into oklch channels and exposes them as the keywords `l`, `c` and `h`. The expression then returns a new color built from those values, here the same chroma and hue with 0.1 more lightness.

  3. 03Why does `color-mix()` take `in oklch` rather than just two colors?

    Mixing means interpolating between coordinates, and the answer depends on which coordinates. Mixing red and blue in `srgb` gives a flat, dark purple, while `oklch` travels along the hue wheel and keeps the midpoint vivid. Naming the space makes the result predictable.

  4. 04A client's brand green is a P3 color. How do you use it without breaking older displays?

    Declare a normal sRGB color first, then override it inside `@media (color-gamut: p3) { ... }` with `color(display-p3 r g b)`. Browsers that cannot parse or show it keep the first declaration.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark