Yay for HSLa

Avatar of Chris Coyier
Chris Coyier on (Updated on )

Huge sogging longbottoms? High strength low alloy steel? NOPE, we’re talking Hue, Saturation, Lightness, and alpha, and it’s a way of declaring color in CSS. It looks like this:

#some-element {
  background-color: hsla(170, 50%, 45%, 1);
}

It is similar to RGBa in that you declare three values determining the color and then a fourth value for its transparency level. You can read more about browser support below, but it’s basically any browser that supports rgba() supports hsla() too.

There’s a new syntax for HSL snd RGB colors! Now, there’s no need for commas or the “a” in either function to set an alpha transparency. Instead, we use a forward slash (/) in front of the alpha value. E.g. hsl(170 50% 45% / 1) instead of hsla(170, 50%, 45%, 1). Read more about the change and other updated CSS color features.

  • Hue – Think of a color wheel. Around 0o and 360o are reds 120o are greens, 240o are blues. Use anything in between 0-360. Values above and below will be modulus 360.
  • Saturation – 0% is completely denatured (grayscale). 100% is fully saturated (full color).
  • Lightness – 0% is completely dark (black). 100% is completely light (white). 50% is average lightness.
  • alpha – Opacity/Transparency value. 0 is fully transparent. 1 is fully opaque. 0.5 is 50% transparent.

Let’s dig in.

Why is it cool?

The real appeal of HSLa is that it makes more intuitive sense what changing the values will do to the color. Increasing the second value will increase the saturation of that color. Decreasing the third value will decrease the lightness of that color. That makes creating your own color variations on the fly way easier. I would wager that most of us can’t create nice and consistent color variations like this using the RGBa model.

The HSLa model also makes changing color values programatically much easier. Check out the demo tool emphasizing that.

Why is it less utilized?

This is me making a big assumption, but from what I can tell, HSLa is much less utilized in the wild than RGBa is. If you have any ideas of why that is, please share in the comments. My theory is that HSL values are harder to get your hands on, as far as sampling from the screen or a Photoshop document.

I guess Photoshop uses Hue/Saturation/Brightness model instead. I honestly don’t quite understand the difference, but when I was first attempting this, this is the situation I got in.

Getting HSLa values

There is a little app for Mac called Colors from Matt Patenaude.

You just click the magnifying glass and then anywhere on the screen. It will fill in the color in the box. Then you can select the hsla option and copy it to your clipboard. There are preferences for tweaking the formatting just how you want it to. Works pretty well. My one gripe being that if you close the little floating window, it won’t reopen unless you quit and restart the app. Might be a Snow Leopard thing and the app hasn’t been updated in a while? I dunno.

If anyone has a favorite Windows or Linux tool for capturing color that has HSLa as a feature, let me know in the comments and I’ll update it here.

Browser Support

Firefox 3.0+, Safari 3+, Chrome 6+ (maybe older too?), Opera 10+ (maybe older too?)

As usual IE is left out of the party. Even version 8. I have a feeling version 9 will be supporting it but don’t have any proof yet. Anyone have it installed and can test? For IE, you can just declare a fallback color. IE 8 doesn’t support RGBa either, so just using a hex code is the best bet.

#some-element { 
  background-color: #e2ecf0; /* Fallback */
  background-color: hsla(190, 30%, 94%, 1);
  background-color: hsl(190 30% 94% / 1);
}

My Tool

As a demo of how easy it is to create color variations programatically, I created the HSLa Explorer. There are two slides on the page. The top one controls the hue value and the second one controls the opacity level. It takes those values and builds a grid of 100 color variations, where the saturation and lightness are varied in a matrix.

IE doesn’t like something in JavaScript (in the demo) for some reason, but no matter, IE doesn’t like HSLa anyway. Thanks to Matthieu Sieben for helping improve the demo.

More Tools