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.

On this page

Example

#
CSS
: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%);
}
Preview

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

#
Syntax
hsl(<hue> <saturation> <lightness> [/ <alpha>]?)

Colour variants

#
  • hsl(262 83% 58%)

    base colour

    The brand purple: hue 262, strong saturation, and medium lightness. In hexadecimal this is almost exactly #7c3aed.
  • hsl(262 83% 35%)

    lightness 35%

    The same hue and saturation, lower lightness. You get a darker version of the same colour, good for the hover state.
  • hsl(262 83% 82%)

    lightness 82%

    Higher lightness gives a pastel shade, for example for a label background or a subtle highlight.
  • hsl(262 25% 58%)

    saturation 25%

    Lower saturation dulls the colour towards grey. Useful for backgrounds and borders that shouldn’t draw attention.
  • hsl(160 83% 40%)

    hue 160

    Changing only the hue moves the colour around the wheel, here to sea green.
  • hsl(262 83% 58% / 0.4)

    alpha 0.4

    Transparency after a slash. The background checkerboard shows through the purple.

Colour variants in practice

#

hsl(262 83% 58%)

The brand purple: hue 262, strong saturation, and medium lightness. In hexadecimal this is almost exactly #7c3aed.
.demo { background: hsl(262 83% 58%); }

hsl(262 83% 35%)

The same hue and saturation, lower lightness. You get a darker version of the same colour, good for the hover state.
.demo { background: hsl(262 83% 35%); }

hsl(262 83% 82%)

Higher lightness gives a pastel shade, for example for a label background or a subtle highlight.
.demo { background: hsl(262 83% 82%); }

hsl(262 25% 58%)

Lower saturation dulls the colour towards grey. Useful for backgrounds and borders that shouldn’t draw attention.
.demo { background: hsl(262 25% 58%); }

hsl(160 83% 40%)

Changing only the hue moves the colour around the wheel, here to sea green.
.demo { background: hsl(160 83% 40%); }

hsl(262 83% 58% / 0.4)

Transparency after a slash. The background checkerboard shows through the purple.
.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

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.