Sub topic 2
Working in HSL: hue, saturation, and lightness
This chapter assumes you can write basic HTML and CSS and that you already know hex (#1a5ee6) and rgb() colour values. It also assumes you can edit a stylesheet and reload it in a browser. By the end you will be able to read and write hsl() values, change one colour property at a time, and generate a ladder of shades from one hue.
Why use HSL for palette work
Hex and RGB describe a colour as amounts of red, green, and blue light. That matches how screens work, but it is hard to reason about. Suppose you want a darker version of #1a5ee6. You cannot tell which of the three channels to lower, or by how much, without a tool.
HSL describes the same colour with three questions you can answer by eye:
- Hue: which colour is it? Red, orange, green, blue, purple?
- Saturation: how vivid is it, or how grey?
- Lightness: how close is it to black or white?
Palette work mostly means keeping one of these steady while you change another. A shade scale holds hue and saturation roughly fixed and moves lightness. An accent colour keeps saturation and lightness similar and moves the hue. HSL lets you do each of those by editing one number.
The three channels
Hue
Hue is an angle on a colour wheel, from 0 to 360 degrees. The wheel is a circle, so 360 is the same as 0.
| Hue angle | Colour |
|---|---|
| 0 | Red |
| 30 | Orange |
| 60 | Yellow |
| 120 | Green |
| 180 | Cyan |
| 240 | Blue |
| 300 | Magenta |
The primary in this tutorial uses hue 220, a blue that leans slightly toward cyan. The accent uses hue 35, an orange. Because hue is an angle, you can find relationships by arithmetic. Adding 180 gives the opposite colour on the wheel. Later chapters use this to derive accents.
Saturation
Saturation is a percentage. At 100% the colour is as vivid as that hue and lightness allow. At 0% it is a pure grey, and the hue no longer matters. Between those values the colour looks muted. Muted colours cover large areas of a page comfortably. Fully saturated colours are tiring in large areas and work best for small elements such as buttons.
Lightness
Lightness is a percentage from black to white. At 0% every colour is black, and at 100% every colour is white, whatever the hue and saturation. The "pure" colour sits at 50%. Values below 50% give darker shades, and values above 50% give lighter tints.
Writing HSL in CSS
Modern CSS uses spaces between the values:
.example {
/* hue saturation lightness */
color: hsl(220 80% 50%);
/* the same colour at 50% opacity: alpha follows a slash */
background: hsl(220 80% 50% / 0.5);
/* hue accepts units: these three are the same angle */
border-color: hsl(220deg 80% 50%);
outline-color: hsl(0.6111turn 80% 50%);
}The older comma syntax, hsl(220, 80%, 50%), still works in every browser. Don't mix the two styles inside one value. Write the percent sign on saturation and lightness, as in 80%. A bare 80 is not accepted in the comma syntax and is easy to mistype.
Hue outside 0 to 360 wraps around. hsl(370 80% 50%) is the same as hsl(10 80% 50%), and hsl(-20 80% 50%) is the same as hsl(340 80% 50%).
Reading the palette from the spine
Here is the palette used throughout this tutorial. Read it as a set of HSL decisions:
:root {
/* Primary: one hue, varied lightness */
--primary-100: hsl(220 80% 94%);
--primary-500: hsl(220 80% 50%);
--primary-700: hsl(220 80% 34%);
/* Neutral: low saturation, slightly tinted toward the primary hue */
--neutral-50: hsl(220 15% 97%);
--neutral-300: hsl(220 12% 80%);
--neutral-700: hsl(220 15% 28%);
--neutral-900: hsl(220 20% 12%);
/* Accent: a different hue, used sparingly */
--accent-500: hsl(35 95% 50%);
}Three patterns show up here:
- The three primary steps share hue 220 and saturation 80%. Only lightness changes (94, 50, 34).
- The neutrals also use hue 220, but with saturation between 12% and 20%. This gives greys with a faint blue tint, so they sit comfortably beside the primary.
- The accent changes hue to 35 and keeps a high saturation, so it stands out from everything else.
You can predict how each colour looks from the numbers, which you cannot do with #1a5ee6.
Converting HSL to RGB yourself
Browsers convert HSL to RGB for you, so you rarely need to. Writing the conversion once makes the model concrete, and you will need RGB values later to compute contrast ratios. This is the conversion given in the CSS Color specification, adapted to JavaScript:
function hslToRgb(h, s, l) {
const hue = ((h % 360) + 360) % 360; // wrap into 0-359
const sat = s / 100;
const light = l / 100;
const k = (n) => (n + hue / 30) % 12;
const a = sat * Math.min(light, 1 - light);
const f = (n) =>
light - a * Math.max(-1, Math.min(k(n) - 3, 9 - k(n), 1));
return [f(0), f(8), f(4)].map((v) => Math.round(v * 255));
}
console.log(hslToRgb(0, 100, 50)); // [255, 0, 0]
console.log(hslToRgb(120, 100, 25)); // [0, 128, 0]
console.log(hslToRgb(220, 0, 50)); // [128, 128, 128]The first line wraps any hue into range, which matches the CSS behaviour described above. The third example shows that at 0% saturation the result is a grey, whatever the hue. Running hslToRgb(220, 80, 50) gives a value within one unit per channel of rgb(26, 94, 230), depending on how rounding falls.
Generating a lightness scale
The most useful thing to do with HSL is produce a family of shades from one hue. This script prints a ready-to-paste block of custom properties:
function makeScale(name, hue, saturation, lightnessByStep) {
return Object.entries(lightnessByStep)
.map(
([step, lightness]) =>
` --${name}-${step}: hsl(${hue} ${saturation}% ${lightness}%);`
)
.join("\n");
}
const steps = { 100: 94, 300: 76, 500: 50, 700: 34, 900: 18 };
console.log(":root {\n" + makeScale("primary", 220, 80, steps) + "\n}");Running it prints a :root block with five --primary-* lines, all with hue 220 and saturation 80%. Lower step numbers are lighter, so 100 is the pale tint and 900 is the dark shade. Chapter 7 covers how to choose the lightness values. For now, notice that the scale is a table of numbers rather than five separate colour decisions.
Where HSL misleads you
HSL is convenient, but its numbers do not match what people see. Know these limits before you rely on it.
Equal lightness is not equal brightness
Yellow and blue at the same HSL lightness look very different:
.swatch-yellow { background: hsl(60 100% 50%); }
.swatch-blue { background: hsl(240 100% 50%); }Both have lightness 50%. The yellow is glaringly bright, and the blue is dark. Measured with the WCAG formula, yellow has a relative luminance near 0.93 and blue near 0.07. Against a white background, yellow text has a contrast ratio of about 1.07:1, which is unreadable. Blue text has about 8.6:1.
This means the lightness number is not a safe guide to contrast. A scale built with the same lightness steps for every hue will produce different readability for each. You must always measure contrast, which Chapter 8 covers.
Saturation behaves differently near black and white
At very high or very low lightness, the available range of colour shrinks. hsl(220 80% 98%) is almost white, and hsl(220 80% 2%) is almost black. Setting saturation to 80% there gives only a faint tint, not a vivid colour. When building the lightest and darkest steps, you may want to lower the saturation yourself to avoid a harsh result. Check the swatches by eye.
Hue shifts appear in a scale
When you darken a blue by lowering lightness alone, it can look slightly purple. When you darken a yellow, it can look olive or brown. Designers often nudge the hue by a few degrees across a scale to compensate. This is a manual correction, so judge the result visually.
HSL is one model among several
Newer CSS supports oklch(), which was designed so that equal lightness steps look more equal to people. It would solve some of the problems above. This tutorial uses HSL because it is supported everywhere, is easy to read, and is good enough once you add contrast checks. If you later move to OKLCH, the same ideas of hue, chroma (similar to saturation), and lightness carry over.
Common mistakes
- Forgetting that hue 0 and 360 are the same. When you animate or compute hues, wrap the result rather than letting it run past 360.
- Changing two channels at once. If you adjust lightness and saturation together, you cannot tell which change produced the effect. Change one, look, then change the next.
- Using saturation 100% for large areas. Full saturation is for small, deliberate elements. Backgrounds and large surfaces usually look better between 10% and 60%.
- Assuming a lightness number guarantees readability. As shown above, it does not. Always check contrast with a calculator.
- Typing
hsl(220, 80, 50)without percent signs. This is invalid and the browser ignores the declaration, so the element falls back to its inherited or default colour.
Practical workflow
- Pick a hue number and fix it for each palette colour.
- Set a saturation that suits the role: high for primary and accent, low for neutrals.
- Vary only lightness to create the steps.
- Look at the result in the browser, then adjust the hue by a few degrees if dark shades drift in tone.
- Measure contrast for any pair that will carry text.
This workflow is the base for the next chapters: choosing the primary, building neutrals, and picking the accent.
Exercise
A teammate writes --brand: hsl(220 80% 50%); and asks for a darker hover colour that is still clearly the same blue. Which channel should you change, and what value would you try? Then explain why a text colour of hsl(60 100% 50%) on a white background is a problem even though its lightness is a middle value.
Show answer
Change only lightness and keep hue 220 and saturation 80%. For example, hsl(220 80% 40%) is a darker hover colour in the same blue. The existing palette uses 34% for its darkest primary step, so hsl(220 80% 34%) also works. Hue 60 is yellow. HSL lightness of 50% does not mean medium perceived brightness for every hue, and yellow has a very high relative luminance (about 0.93). Against white, the contrast ratio is about 1.07:1, far below the WCAG minimum of 4.5:1 for normal text, so the text is nearly invisible.
Exercise
What does hsl(150 0% 40%) produce, and what would hsl(150 0% 40%) look like if you changed the hue to 300?
Show answer
It produces a medium-dark grey. At 0% saturation the hue has no effect, so changing the hue to 300 gives the identical grey, hsl(300 0% 40%).