HSL and alpha compositing
Definition and purpose
HSL expresses sRGB colors with hue, saturation and lightness. Alpha controls how a painted color blends with what is behind it.
Hue changes
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> #a { color: hsl(0 100% 50%); } #b { color: hsl(240 100% 50%); } </style> </head> <body> <div id="a">Sample A</div> <div id="b">Sample B</div> </body></html>Changing hue from 0 to 240 turns red to blue.
Syntax and mental model
hsl(120 100% 50%) is bright green. Hue wraps around a circle; zero saturation gives gray. hsl(120 100% 50% / .5) affects that color only. In contrast, opacity composites the element and its contents as a group.
Alpha versus opacity
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> #a { background: hsl(0 100% 50% / .5); } #b { background: red; opacity: .5; } </style> </head> <body> <div id="a">Sample A</div> <div id="b">Sample B</div> </body></html>A changes the paint alpha; B composites its entire contents.
Common mistakes
HSL lightness is not perceptual luminance. Equal lightness values do not guarantee equal contrast. Use a translucent background rather than element opacity when text must stay solid.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Named, hex and RGB colors. Next lesson: Current color and system colors.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HSL and alpha compositing — knowledge check
Five questions on hsl and alpha compositing.
What does zero saturation produce?
What is hue 360 equivalent to?
Which affects the whole element group?
What is HSL lightness not?
Which keeps text opaque over a translucent background?