CSS Colors Demystified: HEX, RGB, HSL and When to Use Each

The Three Color Systems Every Web Developer Needs

Color is one of the most fundamental aspects of web design, yet the variety of color formats in CSS can be confusing. Should you use HEX codes, RGB values, or HSL notation? Each format has distinct advantages, and understanding when to use each will make you a more effective developer.

HEX Colors: The Classic Standard

HEX (hexadecimal) colors are the most widely used color format on the web. A HEX code is a six-digit string prefixed with #, where each pair of digits represents the red, green, and blue channels in base-16 (hexadecimal) notation.

/* Standard 6-digit HEX */
color: #FF5733;    /* Red: FF, Green: 57, Blue: 33 */

/* Shorthand 3-digit HEX (when each pair has identical digits) */
color: #F00;       /* Same as #FF0000 — pure red */

/* 8-digit HEX with alpha channel */
color: #FF573380;  /* 50% transparent (#80 = 128/255) */

When to Use HEX

  • When copying colors from design tools (Figma, Photoshop, Sketch all default to HEX)
  • For brand colors and style guides (HEX codes are compact and easy to share)
  • When you need maximum browser compatibility (HEX has been supported since CSS1)

HEX Limitations

HEX codes are not human-readable. Looking at #3B82F6, you cannot intuitively tell what color it is. Adjusting brightness or saturation requires converting to another format, making changes, and converting back.

RGB and RGBA: The Additive Color Model

RGB defines colors by specifying the intensity of red, green, and blue light, each on a scale of 0-255. RGBA adds an alpha channel (0-1) for transparency.

/* RGB */
color: rgb(59, 130, 246);

/* RGBA with 50% transparency */
color: rgba(59, 130, 246, 0.5);

/* Modern CSS allows spaces instead of commas */
color: rgb(59 130 246);
color: rgb(59 130 246 / 0.5);

When to Use RGB

  • When working with transparency (RGBA is more intuitive than 8-digit HEX)
  • When programmatically generating or manipulating colors (the 0-255 scale maps directly to byte values)
  • For canvas operations and image processing (pixel data is stored as RGB)

RGB Limitations

Like HEX, RGB is not intuitive for humans. Want to make rgb(59, 130, 246) slightly brighter? You would need to increase all three values proportionally — not straightforward.

HSL: The Human-Friendly Format

HSL stands for Hue, Saturation, and Lightness. It describes colors the way humans actually think about them — making it the most intuitive format for design work and dynamic color manipulation.

/* HSL */
color: hsl(217, 91%, 60%);

/* HSLA with transparency */
color: hsla(217, 91%, 60%, 0.5);

/* Modern syntax */
color: hsl(217 91% 60% / 0.5);

Understanding HSL Components

  • Hue (0-360) — The color angle on the color wheel. 0°/360° = red, 120° = green, 240° = blue.
  • Saturation (0-100%) — How vivid the color is. 0% = gray, 100% = fully saturated.
  • Lightness (0-100%) — How light or dark. 0% = black, 50% = normal, 100% = white.

Why HSL Is Powerful

HSL makes common color operations trivial:

  • Darken a color — Decrease the lightness: hsl(217, 91%, 40%)
  • Lighten a color — Increase the lightness: hsl(217, 91%, 80%)
  • Desaturate — Decrease saturation: hsl(217, 50%, 60%)
  • Create a complementary color — Add 180° to the hue: hsl(37, 91%, 60%)
  • Build a color palette — Keep saturation and lightness constant, vary the hue

With CSS custom properties, HSL becomes even more powerful for theming:

:root {
  --primary-h: 217;
  --primary-s: 91%;
  --primary-l: 60%;
  --primary: hsl(var(--primary-h), var(--primary-s), var(--primary-l));
  --primary-light: hsl(var(--primary-h), var(--primary-s), 80%);
  --primary-dark: hsl(var(--primary-h), var(--primary-s), 40%);
}

Newer CSS Color Formats

HWB (Hue, Whiteness, Blackness)

HWB is similar to HSL but uses whiteness and blackness instead of saturation and lightness. Some designers find it more intuitive: hwb(217 10% 20%).

LAB and LCH

LAB and LCH are perceptually uniform color spaces — meaning equal numerical changes produce equal perceived color differences. They support a wider gamut than sRGB and are ideal for creating truly uniform gradients and accessible color palettes.

OKLCH

OKLCH is the latest improvement, fixing some issues with LCH. It is becoming the recommended color space for modern CSS due to its perceptual uniformity and wide gamut support: oklch(60% 0.15 217).

Color Accessibility

Choosing colors is not just about aesthetics — it is about accessibility. The Web Content Accessibility Guidelines (WCAG) require minimum contrast ratios between text and background colors:

  • WCAG AA — 4.5:1 for normal text, 3:1 for large text
  • WCAG AAA — 7:1 for normal text, 4.5:1 for large text

Use our Color Contrast Checker to verify your color combinations meet these requirements.

Quick Reference: Which Format to Use

Scenario Best Format Why
Brand colors from design files HEX Compact, universal
Transparency needed RGBA or HSLA Clear alpha syntax
Dynamic theming / CSS variables HSL Easy to derive variants
Programmatic color generation RGB or HSL Numeric manipulation
Accessibility-focused design OKLCH Perceptually uniform

Converting Between Formats

Need to convert between HEX, RGB, HSL, and other formats? Our Color Picker & Converter handles all conversions instantly, letting you work in whichever format suits your current task.