Hex vs RGB vs HSL: which CSS color format should you use?

Dark navy cover card with the title 'Hex vs RGB vs HSL: which CSS color format should you use?' in white and a green accent bar

In the hex vs RGB vs HSL question, all three formats describe exactly the same set of sRGB colors, so the choice is about how you work, not what you can show. Use hex for fixed brand colors copied from a design file, rgb() when code calculates channel values, and hsl() when you need to derive lighter, darker or muted versions of a color by hand.

Hex vs RGB vs HSL: the short answer

Hex, rgb() and hsl() are three notations for one color space, and none of them reaches colors the others can't. The CSS Color Module Level 4 spec makes sRGB the default color space for CSS, and the legacy color functions all live inside it.

That means #00ff00, rgb(0 255 0) and hsl(120deg 100% 50%) are the same lime green. Pick the format that makes the next edit easiest for you or your team:

You need to... Use Why
Paste a brand color from a design tool Hex Design tools and existing code mostly use it
Set a channel from JavaScript math rgb() Each channel is a plain number from 0 to 255
Make a hover shade or a tint hsl() Change one number (lightness) and keep the hue
Add transparency Any of the three Hex has 8 digits, rgb() and hsl() take / alpha
Build an even scale of shades oklch() Its lightness matches what eyes perceive

How hex color codes work

A hex color is a hash followed by 3, 4, 6 or 8 hexadecimal digits, where each pair is a red, green, blue or alpha channel. Each channel runs from 00 to ff, which is 0 to 255 in decimal, according to the hex color reference.

The short forms double each digit. #123 is the same as #112233, and the spec points out that 3-digit hex can only reach 4096 colors, against roughly 17 million for the 6-digit form. Case doesn't matter, so #00ff00 and #00FF00 are identical.

Hex also handles transparency now. The 8-digit form adds an alpha pair, where 00 is fully transparent and ff is fully opaque. Black at about half opacity is #00000080, as the web.dev color lesson shows. The catch is readability: few people can tell what cc means as a percentage without a calculator.

The same lesson cites the 2022 Web Almanac, which found hex to be the most popular color syntax type. In a shared codebase, that counts for a lot.

How rgb() works, and why rgba() is no longer needed

The rgb() function sets a color by its red, green and blue channels, each a number from 0 to 255 or a percentage from 0% to 100%. Alpha goes after a slash, so semi-transparent red is rgb(255 0 0 / 50%).

According to the rgb() reference, rgba() is an exact alias for rgb(), and the recommendation is to write rgb(). The old comma form, rgb(255, 0, 0, 0.5), still works, so old stylesheets can stay as they are.

rgb() earns its place when a program produces the color: a chart that maps a value to the red channel, or a canvas script reading pixel data. The code already has those numbers, so nothing needs converting.

How hsl() works for tints, shades and themes

The hsl() function sets a color by hue, saturation and lightness, which makes related colors easy to write by hand. Hue is an angle on the color wheel: 0deg is red, 120deg is green and 240deg is blue, and the spec normalizes it to the range [0,360).

Saturation and lightness are percentages. In the hsl() reference, 0% saturation is gray, 0% lightness is black, 100% lightness is white and 50% is the "normal" color. hsla() is an alias here too.

Say your button is a dark green, hsl(120deg 75% 25%). A lighter hover state can be hsl(120deg 75% 50%) and a muted disabled state hsl(120deg 50% 50%). One or two numbers changed and the hue stayed put. Try doing that by eye in hex.

:root {
  --brand: hsl(120deg 75% 25%);
  --brand-hover: hsl(120deg 75% 50%);
  --brand-muted: hsl(120deg 50% 50%);
}

For a complementary color, add or subtract 180 degrees from the hue. One legacy detail trips people up. In the comma syntax, the % signs on saturation and lightness are required, so hsl(120, 75, 25) is invalid while hsl(120 75 25) is fine.

How do hex, RGB and HSL handle transparency?

All three formats support an alpha channel, so transparency is no longer a reason to pick one over another.:

  • Hex: #RRGGBBAA or the short #RGBA, for example #0000ffcc
  • rgb(): rgb(0 0 255 / 80%)
  • hsl(): hsl(240deg 100% 50% / 80%)

The spec confirms that #0000ffcc is the same color as rgb(0 0 100% / 80%), a slightly transparent blue. For alpha you'll edit often, the slash form is easier to read than a hex pair.

Where HSL falls short: lightness is not what you see

HSL lightness is a formula, not a measure of how bright a color looks, so two colors with the same HSL lightness can look very different. The spec gives the classic example: hsl(240deg 100% 50%) (blue) and hsl(60deg 100% 50%) (yellow) both have 50% lightness, yet the yellow clearly looks much lighter.

If you build a palette by rotating hue at a fixed lightness, some swatches will look washed out and others heavy. Text contrast checked on one hue won't carry over to the next. The spec also notes that HSL hue spacing is uneven, with colors bunched up in some areas and spread out in others.

The fix in modern CSS is oklch(). Its L channel is perceived lightness, which the oklch() reference contrasts directly with the L in hsl(). oklch() has been widely available since May 2023. One caution: oklch() can describe colors a screen can't show, so check that your values stay displayable.

What the browser does with your color format

Browsers don't keep your format. When you read a color back through a Web API, such as getComputedStyle(), you get an rgb() string if alpha is exactly 1 and rgba() otherwise, both with commas. Absolute hsl() colors are serialized to rgb() too, and the channel values may be rounded.

Write color: hsl(120deg 75% 25%), log the computed color in DevTools or a test, and you get an rgb(...) value back. Snapshot tests that compare color strings should compare against the rgb() form, or parse both sides first.

Gradients follow a related rule. The spec says browsers interpolate legacy sRGB forms (hex, rgb() and hsl()) in gamma-encoded sRGB space for web compatibility. Newer syntax that doesn't define a space defaults to Oklab. Rewriting two hex gradient stops as hsl() won't make the gradient smoother. You have to ask for an interpolation space.

Which format should you pick?

My take: keep hex for the handful of brand colors you paste from a design file, and write everything derived from them in hsl() or oklch(). Designers get a familiar value to check, and you get readable variants. Switch to oklch() the moment a project needs a generated color scale or dark-mode theming. Even lightness steps save hours of hand-tweaking.

Rules of thumb:

  1. Don't mix formats at random in one file. Pick one per role (brand, derived, generated) so reviews stay simple.
  2. In new code, drop rgba() and hsla(). The slash syntax does the same job.
  3. Leave an old codebase's formats alone unless you have a reason beyond style. The colors are identical, and the diff is review noise.

Reformatting a stylesheet first makes mixed color formats easy to spot; see how to format minified CSS. If you're cleaning up sizes in the same pass, the em vs rem guide covers the other half of most design-token files.

Auditing the colors a stylesheet already uses

Before you standardize on a format, list what's in the file today. The CSS color extractor pulls hex, rgb and rgba values out of pasted CSS in your browser tab and shows how many near-duplicates you have.

It doesn't list hsl() or oklch() values, so it's no help on a stylesheet already written in those formats. The guide on how to extract colors from CSS covers grep patterns and DevTools' CSS Overview for that case. If specificity fights are why the same color appears in five places, CSS specificity explained is the next read.

FAQ

Is hex, RGB or HSL faster in the browser?

None of them is meaningfully faster for you to worry about. All three are notations for the same sRGB colors, and when you read a color back through a Web API the browser hands you an rgb() string whichever format you wrote.

Can hex colors have transparency?

Yes. Hex has 4-digit and 8-digit forms where the last digit or pair is alpha. #00000080 is black at roughly 50% opacity, 00 is fully transparent and ff is fully opaque.

Should I still write rgba() and hsla()?

You don't need to. rgba() is an exact alias of rgb(), and hsla() is an exact alias of hsl(). The modern form puts alpha after a slash, as in rgb(255 0 0 / 50%) or hsl(120deg 75% 25% / 60%).

Is HSL lightness the same as how bright a color looks?

No. HSL lightness is a calculated value, not perceived brightness. Pure blue and pure yellow both sit at 50% lightness in HSL, but the yellow looks far lighter. oklch() was designed so its L channel tracks perceived lightness.

Do I need to switch to oklch()?

Not for a fixed palette that already works. oklch() helps when you generate color scales, build theme variants or care about even gradients. It has been widely available in browsers since May 2023.

Sources