Sub topic 2
rgb() and hsl(): channels, hue, and alpha
This chapter assumes you know basic CSS selectors, how a declaration like color: red; works, and the hex notation from the previous chapter. It covers the two functional color notations, rgb() and hsl(), and how to add transparency to either one.
Why functional notation exists
Hex notation such as #1a1a1a is compact, but you can't easily read it or adjust it by hand. Functional notation writes each part of the color as a separate number. You can see what each number does, and you can change one without redoing the whole value.
CSS gives you two main functions for this:
rgb()describes a color by how much red, green, and blue light it contains.hsl()describes a color by its hue, its saturation, and its lightness.
Both produce colors in the sRGB color space, the standard space used by most screens. They are two ways of writing the same range of colors.
rgb(): red, green, and blue channels
A channel is one component of a color. rgb() takes three channels: red, green, and blue. Each channel is a number from 0 to 255, where 0 means none of that light and 255 means the maximum.
.example {
color: rgb(26 26 26);
background-color: rgb(255 255 255);
}The first color is a dark gray, and the second is white. Hex is the same data written in base 16: #1a1a1a is rgb(26 26 26), because 1a in hex is 26 in decimal.
Mixing channels works like mixing colored lights:
| Value | Result |
|---|---|
| `rgb(0 0 0)` | Black (no light) |
| `rgb(255 255 255)` | White (all light) |
| `rgb(255 0 0)` | Pure red |
| `rgb(255 255 0)` | Yellow (red plus green) |
| `rgb(128 128 128)` | Mid gray (equal channels give gray) |
When all three channels are equal, the result is always a shade of gray. That fact helps when you debug a color that looks slightly tinted.
You can also write channels as percentages, where 100% equals 255:
.example {
background-color: rgb(100% 50% 0%);
}Use one style or the other within a single function. Percentages are rarely used in practice, but they appear in some design tools.
Modern syntax versus legacy syntax
Older CSS required commas, and it used separate functions for transparency:
.legacy {
color: rgb(26, 26, 26);
background-color: rgba(0, 0, 0, 0.5);
}Current browsers accept both forms. The modern form uses spaces between channels and a slash before alpha. In the modern form, rgba() is just an alias for rgb(), so you no longer need it. You will still see the legacy form in older code, and it still works.
A common error is mixing the two forms. rgb(26 26 26, 0.5) is invalid, and the browser will discard the whole declaration. Pick the comma form or the space form, and don't combine them.
hsl(): hue, saturation, and lightness
The hsl() function describes a color the way people tend to think about it. It takes three values:
- Hue is the position on the color wheel, written as an angle from 0 to 360 degrees. The
degunit is optional. - Saturation is how vivid the color is, from
0%(gray) to100%(full color). - Lightness is how bright the color is, from
0%(black) to100%(white), with50%giving the purest version of the hue.
.example {
border: 1px solid hsl(220 15% 80%);
}This is a pale, slightly bluish gray. The hue 220 is blue, the low saturation 15% keeps the color muted, and the high lightness 80% makes it light.
Reading the hue wheel
Hue is a circle, so 0 and 360 are the same color. These landmarks are worth memorizing:
| Hue | Color |
|---|---|
| 0 | Red |
| 60 | Yellow |
| 120 | Green |
| 180 | Cyan |
| 240 | Blue |
| 300 | Magenta |
| 360 | Red again |
Because hue is an angle, a value outside the range wraps around. hsl(-60 100% 50%) and hsl(300 100% 50%) are the same magenta, and hsl(480 100% 50%) is the same as hsl(120 100% 50%).
Why hsl() is easier to adjust
With rgb(), making a color lighter means changing all three channels in a coordinated way. With hsl(), you change one number. Here is a small palette built from one hue:
.swatch-dark {
background-color: hsl(220 60% 25%);
}
.swatch-base {
background-color: hsl(220 60% 50%);
}
.swatch-light {
background-color: hsl(220 60% 85%);
}Only the lightness changes. All three swatches are recognizably the same blue. This makes hsl() a good choice for hover states, borders, and tinted backgrounds.
Edge cases in hsl()
- When saturation is
0%, the hue has no effect.hsl(0 0% 50%)andhsl(200 0% 50%)are the same gray. - When lightness is
0%or100%, the result is black or white, whatever the hue and saturation are. - Equal lightness does not mean equal perceived brightness.
hsl(60 100% 50%)(yellow) looks far brighter thanhsl(240 100% 50%)(blue), even though both have50%lightness. This matters for contrast, which a later chapter covers, and it is one reasonoklch()exists.
Alpha: adding transparency
Alpha is a fourth value that sets opacity. It runs from 0 (fully transparent) to 1 (fully opaque). You can also write it as a percentage, where 50% equals 0.5. In modern syntax, you put alpha after a slash:
.card {
color: #1a1a1a;
background-color: rgb(255 255 255);
border: 1px solid hsl(220 15% 80%);
box-shadow: 0 2px 8px rgb(0 0 0 / 0.15);
}The box shadow is black at 15% opacity. A solid black shadow looks harsh, while a mostly transparent one lets the background show through and looks natural on any surface.
The slash form works for hsl() as well:
.overlay {
background-color: hsl(220 60% 20% / 0.7);
}
.faint {
background-color: hsl(220 60% 20% / 30%);
}Hex also supports alpha through 4-digit and 8-digit forms (#0003, #00000026). The last pair of digits is the alpha value, from 00 to ff. Functional notation is easier to read, since / 0.15 is clearer than 26.
Alpha on a color versus opacity on an element
Alpha inside a color and the opacity property look similar but behave differently.
- A color with alpha changes only that one color. Text and child elements stay fully opaque.
- The
opacityproperty makes the whole element and everything inside it transparent.
.panel-a {
background-color: rgb(0 0 0 / 0.5);
color: white;
}
.panel-b {
background-color: rgb(0 0 0);
color: white;
opacity: 0.5;
}In .panel-a, the background is half transparent and the white text stays crisp. In .panel-b, the background and the text both fade. If you want a translucent background behind readable text, use alpha in the color.
Common errors
- Mixing commas and spaces in one function, such as
rgb(255, 0, 0 / 0.5). Usergb(255 0 0 / 0.5)orrgba(255, 0, 0, 0.5). - Forgetting the
%signs inhsl(). Saturation and lightness need them.hsl(220 15 80)is invalid in most browsers. - Going past 255 in
rgb(). Values above the range are clamped to 255, sorgb(300 0 0)renders the same asrgb(255 0 0). This hides typos. - Using alpha on text, then wondering why it looks weak. Faded text often fails contrast requirements.
- Using
opacitywhen you wanted a translucent background only.
Choosing between rgb(), hsl(), and hex
| Situation | Good choice | Reason |
|---|---|---|
| Copying a value from a design tool | Hex or `rgb()` | It matches the source exactly |
| Building shades of one color | `hsl()` | You change only lightness or saturation |
| Shadows and overlays | `rgb()` or `hsl()` with alpha | The slash syntax is readable |
| Neutral grays | Any | Equal channels or `0%` saturation |
There is no single right answer. Many teams use hex for fixed brand values and hsl() for derived shades. The next chapters build on this by storing these values as custom properties so you write each one only once.
Exercise
You want a button background that is the same blue as hsl(220 60% 50%) but darker for the pressed state. Which value changes, and write a plausible result. Also, what does rgb(0 0 0 / 0.15) mean?
Show answer
Change only the lightness, for example to hsl(220 60% 35%). The hue (220) and saturation (60%) stay the same, so it remains the same blue. The value rgb(0 0 0 / 0.15) is black (all channels 0) at 15% opacity, which is a common choice for soft shadows.
Exercise
A card uses opacity: 0.6 so its background looks see-through, but the text inside is now too faint. How do you fix it while keeping the see-through background?
Show answer
Remove opacity from the card and put alpha in the background color instead, for example background-color: rgb(255 255 255 / 0.6). Alpha in a color affects only that color, so the text stays fully opaque.