Colors With Opacity in CSS: RGBA, HSLA and 8-Digit Hex
2 min read
Every color in CSS can carry an alpha channel — a fourth value that controls transparency. There are three main ways to write it, and each has its fans. Here is how they compare and when to use which.
The three alpha syntaxes
- rgba() — red, green, blue plus alpha. Alpha runs from 0 (invisible) to 1 (opaque).
- hsla() — hue, saturation, lightness plus alpha. The same alpha, with the friendlier hue model.
- 8-digit hex — a normal hex code plus two extra hex digits for alpha, where 00 is invisible and FF is opaque.
/* The same 50% red three ways */
background: rgba(220, 20, 60, 0.5);
background: hsla(348, 83%, 47%, 0.5);
background: #dc143c80;The alpha hex pair works exactly like a color channel: #RRGGBBAA. So #dc143c80 is Crimson at roughly 50% (0x80 ≈ 128/255).
When to use which
- Use hsla() when you are reasoning about the color ("make it a bit more transparent" maps directly to the syntax).
- Use rgba() when your source already gives you channel numbers — for example values from a canvas or design tool.
- Use 8-digit hex when you want one compact token you can copy between CSS and design files, or when the alpha never changes.
Modern browsers support all three. The practical differences are readability and habit, not capability.
The contrast trap
Transparency does not change the perceived lightness in the way beginners expect — a 50% white overlay on a dark photo is much darker than 50% white on a light background. The WCAG contrast ratio is computed against the final composited color, so always check the text-over-background result that users actually see.
The color picker shows every color side by side as HEX, RGB and HSL, and the hex to RGB converter breaks codes into channels you can plug into rgba() directly. For accessible pairings, test the final look in the contrast checker.
Best practice: keep text fully opaque and use transparency for overlays, borders and shadows. Text contrast is hard enough without an alpha channel making it worse.
Alpha is a small addition to the color syntax with a big effect on how interfaces layer and breathe — now you can write it in whichever format your team prefers.