HSL Colors
Table of Contents + โ
In the last lesson you learned about RGB Colors. There you mixed red, green, and blue numbers to make a color. Now letโs look at a way that feels much more natural to us. It is called HSL.
๐จ What is HSL?
HSL is another way to write a color in CSS. But instead of mixing light like RGB does, it describes a color the way your eyes think about it.
HSL stands for Hue, Saturation, and Lightness. Those are just three simple ideas about a color.
- Hue is the color itself. Like red, blue, or green.
- Saturation is how strong or how grey the color looks.
- Lightness is how bright or how dark the color is.
So with RGB you ask โhow much red, green, and blue?โ With HSL you ask โwhich color, how strong, and how bright?โ That second question is much closer to how we talk about colors in real life.
๐ผ๏ธ A real-life way to picture it
Think about choosing paint for a wall in a shop.
- First you pick the base color from a color wheel. A color wheel is a ring that shows all the colors going around in a circle. That choice is the Hue.
- Then you decide if you want it bright and full, or soft and washed out. That is the Saturation.
- Last you make it lighter or darker, like adding white or black. That is the Lightness.
You do not think in red-green-blue numbers when you pick paint. You think โI want a soft light blue.โ HSL lets you write a color that same way.
โ๏ธ The syntax
You write HSL with the hsl() function. Inside it you put three values in order: hue, then saturation, then lightness.
Here is the basic shape of it.
color: hsl(0, 100%, 50%);Letโs walk through each part.
- The first value is the hue. It is an angle on a color wheel from
0to360.0is red,120is green, and240is blue. - The second value is the saturation. It is a percent from
0%to100%.0%is fully grey and100%is the full strong color. - The third value is the lightness. It is a percent from
0%to100%.0%is black,100%is white, and50%is the normal color.
So hsl(0, 100%, 50%) means hue 0 (red), full strength, normal brightness. That gives you pure red.
๐งฉ A simple example
Letโs make a box and give it a nice blue background with HSL.
.box { width: 200px; height: 100px; background-color: hsl(210, 80%, 55%); color: hsl(0, 0%, 100%);}Now read those two colors out loud.
- The background is hue
210. That sits between blue and cyan, so it is a calm sky blue. Saturation80%keeps it fairly strong. Lightness55%keeps it bright but not washed out. - The text color is
hsl(0, 0%, 100%). The hue does not matter here because saturation is0%, so the color is grey. Lightness100%pushes that grey all the way to white.
What you see
A medium-sized sky blue box with clean white text sitting on top of it.
๐ Why HSL is so handy
Here is the real reason people like HSL. It makes small changes to a color very easy.
Say you have a button. You want a darker shade of the same color when someone hovers over it. With RGB you would have to guess new red, green, and blue numbers. With HSL you keep the hue and saturation the same. You only lower the lightness.
This example shows a button that gets darker on hover by changing just the lightness.
.btn { background-color: hsl(140, 70%, 45%);}
.btn:hover { background-color: hsl(140, 70%, 35%);}See how the only thing that changed was 45% to 35%? Same green hue, same strength, just a bit darker. That is hard to do cleanly in RGB but very easy in HSL.
Quick tip
Keep the hue and saturation fixed and only move the lightness. That is the simple way to build matching light and dark shades of one color.
๐ HSL vs RGB at a glance
Both write the same colors on screen. They just describe them in different ways.
| Idea | RGB asks | HSL asks |
| The color | How much red, green, blue? | Which hue on the wheel? |
| How strong | Hidden in the mix | The saturation percent |
| Lighter or darker | Change all three numbers | Change one lightness percent |
๐ง Adding see-through color with HSLA
Sometimes you want a color you can partly see through. Like a faint layer on top of a photo. For that there is hsla(). The extra a stands for alpha, which just means how solid the color is.
This box uses a red color that is only half solid, so whatever is behind it shows through.
.overlay { background-color: hsla(0, 100%, 50%, 0.5);}The first three values are the same hue, saturation, and lightness as before. The fourth value is the alpha. It runs from 0 (fully see-through) to 1 (fully solid). So 0.5 means half see-through.
Good to know
Modern CSS also lets you write this as hsl(0 100% 50% / 0.5) with spaces and a slash. The older comma style still works everywhere, so either one is fine.
โ ๏ธ Common Mistakes
A few small things trip up beginners with HSL. Watch for these.
- Forgetting the
%signs. Saturation and lightness must have a percent, like60%. Writinghsl(200, 60, 40)will not work. - Putting a
%on the hue. The hue is an angle, not a percent. Write200, not200%. - Mixing up lightness and saturation. Lightness near
0%gives black and near100%gives white. If your color keeps coming out black or white, check the lightness first.
โ Best Practices
Keep these habits and HSL will stay easy to use.
- Use HSL when you need matching shades, like hover states or light and dark versions of one color.
- Change only the lightness to go darker or lighter. Keep hue and saturation steady.
- Reach for
hsla()when you want a soft, see-through layer instead of a fully solid color.
๐งฉ What Youโve Learned
Here is the quick recap of what we covered.
- โ HSL describes a color as Hue, Saturation, and Lightness, the way humans think about color.
- โ
Hue is an angle from
0to360, while saturation and lightness are percents from0%to100%. - โ You make a color darker or lighter by changing only the lightness.
- โ
hsla()adds an alpha value from0to1to make the color see-through.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What do the three letters in HSL stand for?
Why: HSL means Hue (the color), Saturation (how strong it is), and Lightness (how bright or dark it is).
- 2
Which value in HSL is written as an angle from 0 to 360, not a percent?
Why: The hue is an angle on the color wheel from 0 to 360. Saturation and lightness use percents.
- 3
You want the same color but a little darker. What do you change?
Why: Keep hue and saturation the same and lower the lightness percent. That gives a darker shade of the same color.
- 4
In hsla(0, 100%, 50%, 0.5), what does the 0.5 do?
Why: The fourth value is alpha. It runs from 0 (fully see-through) to 1 (fully solid), so 0.5 is half see-through.
๐ Whatโs Next?
Now you can write colors the way your eyes think about them. Next we move from colors to sizes, and learn the different ways CSS measures things on a page.