HEX vs RGB vs HSL: CSS Color Formats Explained
By Durim Hoti · Last updated
Design tools hand you HEX codes, CSS examples use rgb(), and color guidelines talk about hue and lightness. They are three ways of writing down the same thing, and each is convenient for a different job.
One color, three notations
- HEX (
#1d4ed8): red, green and blue as two hexadecimal digits each, from 00 to ff. - RGB (
rgb(29, 78, 216)): the same three channels as decimal numbers from 0 to 255. - HSL (
hsl(224, 76.3%, 48%)): hue as an angle on the color wheel (224° is blue), saturation (0% is grey) and lightness (0% black, 100% white).
In CSS, the first two lines below are exactly the same color. The HSL line uses the rounded values the converter shows; converted back, it is one step off in the green channel, which you can't see. The section on problems explains why.
.button {
background: #1d4ed8;
background: rgb(29 78 216);
background: hsl(224 76.3% 48%);
}Convert a color step by step
- Open the HEX to RGB converter.
- Type or paste the HEX code, with or without #. Codes with 3, 4, 6 or 8 digits work.
- Read RGB, RGBA, the plain channel values, the normalized HEX and HSL.
- Click Copy next to the format you need.
- Going the other way, use RGB to HEX, or pick a color visually with the color picker.
Example: converting #1d4ed8 by hand
#1d4ed8 → 1d | 4e | d8
1d = 1 × 16 + 13 = 29 (red)
4e = 4 × 16 + 14 = 78 (green)
d8 = 13 × 16 + 8 = 216 (blue)
→ rgb(29, 78, 216)
The converter normalizes the HEX code to capital letters. Letter case doesn't matter in CSS: #1d4ed8 and #1D4ED8 are the same color.
Building lighter and darker shades with HSL
In HEX or RGB, a lighter version of a color means changing all three channels. In HSL you change one number. These are hsl(224 76% L) for different lightness values:
| Lightness | HEX | Swatch |
|---|---|---|
| 30% | #123187 | |
| 40% | #1842B4 | |
| 48% | #1D4FD7 | |
| 60% | #4B75E7 | |
| 75% | #8FA9F0 | |
| 90% | #D2DCF9 |
The 48% row is our blue, rounded to whole percent: #1D4FD7 instead of #1D4ED8. Check text on the light and dark shades with the contrast checker.
Common problems
Converting to HSL and back changes the color
HSL values are fractions that get rounded for display. The converter shows hsl(224, 76.3%, 48%) for #1D4ED8; typing that HSL back in gives #1D4FD8. The difference is one step in the green channel, invisible to the eye but enough to fail an exact match. Keep the HEX code as the source of truth.
A color with transparency
Modern CSS writes alpha after a slash: rgb(29 78 216 / 50%). As HEX that is #1D4ED880; the last pair, 80, is the alpha (128 of 255).
An 8-digit code from Android looks wrong
Android often writes alpha first (#AARRGGBB), CSS last (#RRGGBBAA). The RGBA to HEX converter handles both orders.
The converter rejects my code
HEX codes have 3, 4, 6 or 8 digits from 0–9 and a–f. A 5- or 7-digit code is usually a typo or a missing character.
What to do next
Colors in a minified stylesheet are kept exactly as written; see how to minify HTML, CSS and JavaScript. For another kind of encoding, see what Base64 is.
FAQ
- Which color format should I use in CSS?
- All three work in every current browser. HEX is compact and common in design tools; RGB is handy when you compute channels; HSL is easiest for making a color lighter, darker or less saturated by hand.
- Are HEX and RGB exactly the same color?
- Yes. HEX is just the three RGB numbers written in base 16, so the conversion is exact in both directions. HSL is computed with fractions, and rounding its values can shift the color slightly.
- What does an 8-digit HEX code mean?
- The last two digits are the alpha (opacity). #1d4ed880 is the blue at 80 in hex, which is 128 of 255, about 50% opacity.
- What does #fff mean?
- It is shorthand: each digit is doubled, so #fff is #ffffff (white) and #1a3 is #11aa33. Only codes whose pairs repeat can be shortened.
- Is the conversion done on a server?
- No. The converters calculate the values in your browser.
Try it and keep reading
- Open the HEX to RGB converterConvert HEX color codes to RGB and RGBA.
- What Is Base64? How Encoding Works, with ExamplesSee how Base64 turns 3 bytes into 4 characters, what padding means, and when to use it.
- How to Minify HTML, CSS and JavaScript SafelyMake HTML, CSS and JavaScript smaller, see the real savings, and avoid what breaks.