Core answer: CSS colors come in HEX (#3B82F6), RGB (rgb(59,130,246)), and HSL (hsl(217,91%,60%)) — three notations for the same sRGB space. Convert HEX↔RGB by pairs of hex digits; use HSL for systematic palettes (hover = lightness −8%). Alpha variants (#3B82F680, rgba, hsla) add opacity. Modern CSS adds oklch() for perceptually uniform palettes.
The three classic notations
| Notation | Example | Best for |
|---|---|---|
| HEX | #3B82F6 | compact copy-paste, design tools |
| RGB | rgb(59 130 246) | channel math, JS manipulation |
| HSL | hsl(217 91% 60%) | human reasoning, palette building |
Building a palette from one color (the HSL workflow)
Start: primary hsl(217 91% 60%).
- Hover: L 60% → 52%
- Active/pressed: L 46%
- Subtle background: hsl(217 91% 96%)
- Border: hsl(217 50% 85%)
- Text-on-primary: white at L 60% passes contrast; below L 55% it stops passing — check.
One hue, six roles. This is exactly how Tailwind's 50–900 scales are constructed.
Contrast and accessibility (non-negotiable)
WCAG 2.1 ratios: normal text ≥ 4.5:1, large text (18pt+/14pt bold+) ≥ 3:1, UI components ≥ 3:1. Quick reference on white:
- #767676 is the lightest gray passing 4.5:1
- #595959 passes 7:1 (AAA)
- Yellow (#FACC15) on white fails everything — pair with dark text
Worked examples
Example 1 — HEX→RGB. #10B981: 10=16, B9=185, 81=129 → rgb(16,185,129) — this site's emerald.
Example 2 — Semi-transparent overlay. 60% black scrim: rgba(0,0,0,0.6) or #00000099 (99 hex = 153 ≈ 60%).
Example 3 — Programmatic darkening (JS). Multiply RGB channels by 0.9, clamp to 0–255 — quick but shifts saturation; prefer HSL lightness −6% for cleaner results.
Example 4 — Status palette. success hsl(160 84% 39%) / warning hsl(38 92% 50%) / danger hsl(0 84% 60%) / info hsl(217 91% 60%) — evenly spaced hues, consistent S/L for visual balance.
Common mistakes and myths
- Pure black on pure white — 21:1 contrast causes eye strain in long reading; #1a1a1a on #fafafa reads softer and passes AAA.
- Color-only meaning — 8% of men have red-green deficiency; pair color with icons/text (never a red/green-only status dot).
- Saturation everywhere — professional UIs saturate accents and desaturate surfaces; 100%-saturated backgrounds scream.
- Ignoring dark-mode math — inverting lightness naively (L → 100−L) breaks hue relationships; design dark palette separately with elevated surfaces at L 10–18%.
- Trusting eyedropper across displays — sRGB vs Display-P3 shifts saturated colors; specify color spaces when it matters (CSS: color(display-p3 …)).