CSS accepts the same color in several notations. #3b82f6, rgb(59 130 246) and hsl(217 91% 60%) all describe the same blue. They differ in how readable they are and how easy it is to derive related colors, which matters most when you build a palette or a theme.
HEX
color: #3b82f6; /* red 3b, green 82, blue f6 */
color: #fff; /* shorthand for #ffffff */
color: #3b82f680; /* 8 digits: last pair is alpha (80 ≈ 50%) */
Each pair is one channel in hexadecimal, 00 to ff (0 to 255). HEX is compact and what design tools copy by default, but it's hard to read: you can't tell how light or saturated #3b82f6 is, or make it 10% darker in your head.
RGB
color: rgb(59 130 246);
color: rgb(59 130 246 / 50%); /* with alpha */
color: rgb(59, 130, 246); /* legacy comma syntax, still valid */
The same three channels in decimal. RGB is handy when values come from code, like canvas pixel data or an image's average color, but it's still not intuitive for adjustments: making a color "a bit warmer" changes all three numbers.
HSL
color: hsl(217 91% 60%);
- Hue: the angle on the color wheel, 0–360 (0 red, 120 green, 240 blue).
- Saturation: 0% is gray, 100% is fully vivid.
- Lightness: 0% is black, 50% is the pure color, 100% is white.
HSL maps to how people describe colors, which makes variations easy:
:root {
--brand-h: 217;
--brand: hsl(var(--brand-h) 91% 60%);
--brand-dark: hsl(var(--brand-h) 91% 45%);
--brand-light: hsl(var(--brand-h) 91% 92%);
--brand-muted: hsl(var(--brand-h) 30% 60%);
}
Changing --brand-h re-themes everything that uses it.
Converting in JavaScript
HEX to RGB:
function hexToRgb(hex) {
let h = hex.replace("#", "");
if (h.length === 3) h = [...h].map((c) => c + c).join("");
return [0, 2, 4].map((i) => parseInt(h.slice(i, i + 2), 16));
}
hexToRgb("#3b82f6"); // [59, 130, 246]
RGB to HSL:
function rgbToHsl(r, g, b) {
r /= 255; g /= 255; b /= 255;
const max = Math.max(r, g, b), min = Math.min(r, g, b), d = max - min;
const l = (max + min) / 2;
let h = 0, s = 0;
if (d) {
s = d / (1 - Math.abs(2 * l - 1));
if (max === r) h = ((g - b) / d) % 6;
else if (max === g) h = (b - r) / d + 2;
else h = (r - g) / d + 4;
h = (h * 60 + 360) % 360;
}
return [Math.round(h), Math.round(s * 100), Math.round(l * 100)];
}
rgbToHsl(59, 130, 246); // [217, 91, 60]
RGB back to HEX:
const rgbToHex = (r, g, b) => "#" + [r, g, b].map((v) => v.toString(16).padStart(2, "0")).join("");
In the browser you can also let CSS do the parsing: set a color on an element and read getComputedStyle(el).color, which returns rgb(...) for sRGB colors.
Comparison
| HEX | RGB | HSL | |
|---|---|---|---|
| Example | #3b82f6 |
rgb(59 130 246) |
hsl(217 91% 60%) |
| Readability | Low | Medium | High |
| Easy to adjust | No | No | Yes |
| Alpha | #rrggbbaa |
/ 50% |
/ 50% |
| Typical source | Design tools | Code, canvas | Hand-written themes |
The limit of HSL, and OKLCH
HSL's lightness isn't perceptual. hsl(60 100% 50%) (yellow) and hsl(240 100% 50%) (blue) have the same "lightness", but yellow looks far brighter. A palette generated by changing only the hue in HSL ends up with uneven contrast, which is a problem for accessibility.
OKLCH, supported in all current browsers, uses perceptual lightness:
color: oklch(62% 0.19 255); /* lightness, chroma, hue */
Colors with the same L look similarly bright, so swapping hues keeps contrast consistent. OKLCH can also express colors outside sRGB on wide-gamut displays. It's a good choice for design systems; HEX and HSL remain fine for everyday work.
Modern CSS can also mix and adjust colors without converting by hand:
background: color-mix(in oklch, var(--brand) 80%, white);
border-color: oklch(from var(--brand) calc(l - 0.1) c h); /* relative color syntax */
Contrast still matters
Whatever the format, check text contrast: WCAG asks for at least 4.5:1 for normal text and 3:1 for large text. Browser DevTools show the ratio in the color picker.
These formats are also what you'll write in gradients and shadows, where HSL and OKLCH make it easy to derive lighter and darker stops from one base color.