What are color formats?

CSS accepts colors as keywords (tomato), hex (#2b4bff, with optional alpha #2b4bff80), rgb(), hsl(), and modern perceptual spaces such as oklch(), oklab(), lab() and lch(), plus color() for wide-gamut spaces like Display P3. All modern functions accept an alpha channel with a slash: rgb(0 0 0 / 50%).

The formats

  • Named: red, rebeccapurple, transparent, currentColor (the element's text color).
  • Hex: #rgb, #rrggbb, #rrggbbaa.
  • RGB: rgb(43 75 255), rgb(43 75 255 / .5) (the comma syntax and rgba() still work).
  • HSL: hsl(230 100% 58%): hue, saturation, lightness. Easy to reason about but not perceptually uniform.
  • OKLCH: oklch(55% 0.24 265): lightness, chroma, hue. Equal lightness values look equally light, so palettes and tints are predictable, and it can reach wide-gamut P3 colors.
  • color(display-p3 1 0 0): explicitly wide-gamut.

Color functions

  • color-mix(in oklch, var(--brand), white 30%): tints and shades from one variable.
  • Relative color syntax: oklch(from var(--brand) calc(l + .1) c h) derives a lighter version.
  • light-dark(#111, #eee) picks a value based on the color scheme (with color-scheme: light dark).

Practical advice

  • Store brand colors as custom properties.
  • Prefer OKLCH for design systems because generated scales stay consistent.
  • Check contrast: 4.5:1 for body text, 3:1 for large text and UI components.

Code example

html
1<style>
2 :root { --brand: oklch(55% 0.24 265); color-scheme: light dark; }
3 body { font-family: system-ui; display: flex; flex-wrap: wrap; gap: 8px; background: light-dark(#fff8ec, #0b1020); }
4 .s { width: 120px; height: 60px; padding: 6px; font-size: 12px; color: white; border-radius: 8px; }
5</style>
6<div class="s" style="background: tomato">tomato</div>
7<div class="s" style="background: #2b4bff">#2b4bff</div>
8<div class="s" style="background: #2b4bff80">#2b4bff80 (alpha)</div>
9<div class="s" style="background: rgb(43 75 255 / 70%)">rgb / 70%</div>
10<div class="s" style="background: hsl(230 100% 58%)">hsl()</div>
11<div class="s" style="background: var(--brand)">oklch()</div>
12<div class="s" style="background: color-mix(in oklch, var(--brand), white 40%)">color-mix tint</div>
13<div class="s" style="background: oklch(from var(--brand) calc(l - .15) c h)">relative: darker</div>
14<div class="s" style="background: color(display-p3 1 0.2 0.3)">display-p3</div>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.

Open in study app