Sub topic 2
rgb() and hsl(): channels, hue, and alpha
This chapter assumes you know how to write a CSS rule, how selectors apply it, and how hex colors such as #1a1a1a work. Here you will learn the two functional color notations, rgb() and hsl(), and how to add transparency to either one.
How rgb() describes a color
The rgb() function takes three channels: red, green, and blue. Each channel runs from 0 to 255, or from 0% to 100%. A hex color is the same data in base 16, so #ff8000 equals rgb(255 128 0).
Modern CSS separates the channels with spaces. The older form with commas still works, but you can't mix the two styles in one value.
.card {
color: rgb(26 26 26);
background-color: rgb(255 255 255);
}Raising all three channels together moves the color toward white. Lowering them moves it toward black. This makes rgb() hard to adjust by hand. To get a slightly lighter blue, you have to change several numbers at once.
How hsl() describes a color
The hsl() function uses hue, saturation, and lightness.
- Hue is an angle on the color wheel, from 0 to 360. Red is near 0, green near 120, and blue near 240.
- Saturation is a percentage. 0% is gray and 100% is the most vivid version of the hue.
- Lightness is a percentage. 0% is black, 50% is the pure color, and 100% is white.
.card {
border: 1px solid hsl(220 15% 80%);
}Each number does one job. To make a color lighter, you raise only the lightness. To get a related color, you rotate only the hue. This is why hsl() suits palettes and hover states.
flowchart LR
A[rgb: red, green, blue] --> C[Same color on screen]
B[hsl: hue, saturation, lightness] --> CThe block above is only a sketch. Here is the diagram in the required format.
Adding alpha
Alpha is the opacity of the color. It runs from 0 (fully transparent) to 1 (fully opaque), or from 0% to 100%. In modern syntax you put it after a slash, inside the same function.
.card {
box-shadow: 0 2px 8px rgb(0 0 0 / 0.15);
}
.overlay {
background-color: hsl(220 60% 20% / 50%);
}The shadow is black at 15% opacity, so it darkens whatever lies under it without hiding it. Using alpha in a color is different from the opacity property. opacity fades the whole element, including its text and children. Alpha in a color fades only that one color.
Common errors
- Mixing commas and spaces, as in
rgb(0, 0 0). The declaration is invalid and the browser ignores it. - Using the slash with the old comma syntax. In the older form you wrote
rgba(0, 0, 0, 0.15). Prefer the slash form for new code. - Forgetting the
%on saturation and lightness inhsl(). Older syntax requires the percent signs, so keep them. - Relying on alpha for text. Semi-transparent text color changes with the background, so its contrast changes too. The contrast chapter covers how to check this.
Exercise
You have background-color: hsl(10 80% 50%);. Write a version of this color that is 40% opaque, and say which single number you would change to make it lighter.
Show answer
background-color: hsl(10 80% 50% / 0.4);. To make it lighter, raise the lightness, which is the third number (50%).
Exercise
What is the difference between color: rgb(0 0 0 / 0.5) on a paragraph and opacity: 0.5 on the same paragraph?
Show answer
The alpha value fades only the text color. The opacity property fades the whole element, including its background, border, and any child elements.