hsl()
Writes a colour through its hue on the colour wheel, saturation, and lightness. It makes it easy to lighten, darken, or mute a colour.
Example
#:root {
--hue: 262;
}
.btn {
background: hsl(var(--hue) 83% 58%);
color: #fff;
}
.btn:hover {
background: hsl(var(--hue) 83% 46%);
}
.btn.soft {
background: hsl(var(--hue) 83% 58% / 0.15);
color: hsl(var(--hue) 90% 80%);
}<div style="display: flex; flex-wrap: wrap; gap: 12px;">
<button class="btn" type="button" style="padding: 10px 18px; border: 0; border-radius: 10px; font: 600 15px system-ui, sans-serif; cursor: pointer; transition: background-color 0.2s;">Save changes</button>
<button class="btn soft" type="button" style="padding: 10px 18px; border: 0; border-radius: 10px; font: 600 15px system-ui, sans-serif;">Preview</button>
</div>Hover over the first button. Then click “Edit” and change --hue to, say, 160 or 20: all the shades shift at once.
Definition and usage
#The hsl() function describes a colour the way a person thinks about it. The first value is the hue, an angle on the colour wheel: 0 is red, 120 green, 240 blue, and 360 red again. The second is saturation: 0% gives grey, 100% the most vivid colour. The third is lightness: 0% is always black, 100% always white, and 50% is the pure colour.
Its advantage over rgb() shows in a palette. The same hue with a different lightness gives a lighter or darker variant, for example hsl(262 83% 58%) for a button and hsl(262 83% 45%) for its hover state. You add transparency after a slash: hsl(262 83% 58% / 0.4).
The older comma syntax, hsl(262, 83%, 58%), and the hsla() function still work. The hue can have an angle unit, for example 262deg or 0.73turn, and a number without a unit means degrees.
A weakness of HSL: 50% lightness doesn’t look equally light in every hue. Yellow hsl(60 100% 50%) looks much lighter to the eye than blue hsl(240 100% 50%). If the colours in a palette should look equally light, oklch() is a better choice.
Syntax
#hsl(<hue> <saturation> <lightness> [/ <alpha>]?)Colour variants
#base colour
The brand purple: hue 262, strong saturation, and medium lightness. In hexadecimal this is almost exactly#7c3aed.lightness 35%
The same hue and saturation, lower lightness. You get a darker version of the same colour, good for the hover state.lightness 82%
Higher lightness gives a pastel shade, for example for a label background or a subtle highlight.saturation 25%
Lower saturation dulls the colour towards grey. Useful for backgrounds and borders that shouldn’t draw attention.hue 160
Changing only the hue moves the colour around the wheel, here to sea green.alpha 0.4
Transparency after a slash. The background checkerboard shows through the purple.
Colour variants in practice
#hsl(262 83% 58%)
#7c3aed..demo { background: hsl(262 83% 58%); }hsl(262 83% 35%)
.demo { background: hsl(262 83% 35%); }hsl(262 83% 82%)
.demo { background: hsl(262 83% 82%); }hsl(262 25% 58%)
.demo { background: hsl(262 25% 58%); }hsl(160 83% 40%)
.demo { background: hsl(160 83% 40%); }hsl(262 83% 58% / 0.4)
.demo { background: hsl(262 83% 58% / 0.4); }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Create variants of a single colour (hover state, border, background) by changing only the lightness. The palette will stay consistent.
- Keep the hue in a variable, for example
--hue: 262. A new colour theme is then a change of one number. - Don’t compare the lightness of different hues by the L value alone. For palettes of equal lightness use
oklch().
Related entries
#- rgb()Writes a colour as a mix of red, green, and blue light, with optional transparency. It also covers hex notation.
- oklch()Writes a colour through lightness, chroma, and hue in a space matched to the eye. Colours of the same lightness look equally light.
- color-mix()Mixes two colours in given proportions and in a chosen colour space. It creates lighter, darker, and semi-transparent variants.
- colorSets the text colour and the default colour of borders, decorations and SVG icons that use currentcolor.
- var()Inserts the value of a CSS variable, that is, a custom property written as --name. It also lets you provide a fallback value.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.