Sub topic 2
Working in HSL: hue, saturation, and lightness
This chapter assumes you know basic HTML and CSS, can read hex and RGB colour values, and can edit a stylesheet and view it in a browser. It covers HSL, the colour model the rest of this tutorial uses to build palettes.
Why HSL for palettes
Hex and RGB describe a colour by how much red, green, and blue light it mixes. That matches how screens work, but it is hard to reason about. If you have #2563eb and want a slightly darker version, you cannot tell which channels to change.
HSL describes the same colour with three values that match how people think about it:
- Hue is the position on the colour wheel, from 0 to 360 degrees. 0 is red, 120 is green, 240 is blue.
- Saturation is how intense the colour is, from 0% (grey) to 100% (fully vivid).
- Lightness is how light or dark it is, from 0% (black) to 100% (white). 50% is the pure colour.
Because the three values are separate, you can change one and leave the others alone. That is what makes HSL useful for building palettes.
Writing HSL in CSS
Modern CSS accepts the space-separated form:
.example {
color: hsl(220 80% 50%);
}The older comma form, hsl(220, 80%, 50%), still works in every browser. Pick one style and use it throughout your stylesheet. This tutorial uses the space-separated form.
To add transparency, put a slash and an alpha value at the end:
.overlay {
background: hsl(220 80% 50% / 0.5);
}Changing one value at a time
The palette in this tutorial relies on one habit: hold the hue fixed and vary the lightness. The three blues below share hue 220 and saturation 80%. Only lightness changes.
:root {
--primary-100: hsl(220 80% 94%);
--primary-500: hsl(220 80% 50%);
--primary-700: hsl(220 80% 34%);
}All three read as the same blue, from a pale tint to a deep shade. Doing this in hex would mean recalculating three channels for every step.
The other two values behave like this:
| Change | Effect | Typical use |
|---|---|---|
| Hue | A different colour family | Choosing an accent |
| Saturation down | Duller, closer to grey | Building neutrals |
| Lightness up or down | Lighter tints or darker shades | Building scales |
Neutrals are low-saturation HSL colours
A neutral is a colour with very little saturation. Setting saturation to 0% gives a pure grey. A small amount of saturation, with the hue copied from your primary, gives a grey that looks calm and fits the rest of the page:
:root {
--neutral-50: hsl(220 15% 97%);
--neutral-300: hsl(220 12% 80%);
--neutral-700: hsl(220 15% 28%);
--neutral-900: hsl(220 20% 12%);
}All four neutrals use hue 220, the same as the primary. Later chapters cover why that choice works.
Limits of HSL
HSL is convenient, but it has a known weakness. Lightness is a mathematical value, not a measure of how bright a colour looks. Yellow at hsl(60 100% 50%) looks much brighter than blue at hsl(240 100% 50%), although both have 50% lightness.
This has two consequences:
- Two hues at the same lightness will not have the same contrast against a background. Do not assume they will.
- A lightness scale that looks even in one hue can look uneven in another.
Newer colour spaces such as OKLCH correct this by matching lightness to perception. HSL is still a good starting point because it is supported everywhere and easy to read. Treat its lightness values as a first draft, then check the results.
Common mistakes
- Forgetting the percent signs. In
hsl(220 80 50), the missing%on saturation and lightness makes the value invalid in modern browsers' CSS parsing for the space-separated form. Writehsl(220 80% 50%). - Changing the hue between steps by accident. A scale whose steps drift in hue looks inconsistent. Copy the hue value rather than retyping it.
- Using high saturation on every step. Very light and very dark steps at full saturation look harsh. The palette above lowers saturation a little at the extremes.
Converting an existing colour
If a brand guide gives you a hex value, convert it once and work in HSL from then on. Browser developer tools help here. In the Styles panel of Chrome or Firefox, click the colour swatch next to a value and use the format toggle to switch between hex, RGB, and HSL.
Exercise
You have --primary-500: hsl(220 80% 50%). Write a darker shade of the same blue for hover states, and say which of the three HSL values you changed.
Show answer
For example --primary-700: hsl(220 80% 34%). Only lightness changed, from 50% to 34%. Hue (220) and saturation (80%) stayed the same, so it reads as the same blue, only darker.
Exercise
Why is it risky to assume that hsl(60 100% 50%) and hsl(240 100% 50%) give the same text contrast on a white background, since both have 50% lightness?
Show answer
HSL lightness is not perceptual lightness. Yellow looks far brighter than blue at the same HSL lightness, so yellow has much lower contrast against white. You must measure contrast with the WCAG formula instead of comparing lightness values.