CSS Color Functions
CSS has evolved far beyond hex codes. Modern CSS provides a rich
set of colour functions — from the familiar
rgb() and
hsl() to the
perceptually-uniform oklch(),
the powerful color-mix(), and
the new relative colour syntax. Each solves different problems:
hsl() makes palette generation
intuitive, oklch() ensures
consistent perceived brightness across hues, and
color-mix() lets you derive
tints and shades entirely in CSS — no preprocessor needed.
Hex, rgb() and rgba()
The oldest and most widely used colour formats. Hex codes are
shorthand for RGB values. The modern
rgb() syntax (CSS Color Level
4) no longer requires commas and allows the alpha channel as a
fourth value with /:
0–255 or 0–100%
0–255 or 0–100%
0–255 or 0–100%
0–1 or 0–100%
/* ── Hex codes ── */
.el { color: #264de4; } /* 6-digit hex */
.el { color: #264de480; } /* 8-digit hex — last 2 = alpha (80 = 50%) */
.el { color: #f00; } /* 3-digit = shorthand for #ff0000 */
.el { color: #f00f; } /* 4-digit = #ff0000ff (fully opaque) */
/* ── rgb() — legacy comma syntax ── */
.el { color: rgb(38, 77, 228); }
.el { color: rgba(38, 77, 228, 0.5); } /* rgba = separate function (old) */
/* ── rgb() — modern space syntax (CSS Color 4) ── */
.el { color: rgb(38 77 228); } /* no commas */
.el { color: rgb(38 77 228 / .5); } /* alpha with / */
.el { color: rgb(38 77 228 / 50%); } /* alpha as % */
/* ── Percentages for channel values ── */
.el { color: rgb(15% 30% 89%); } /* same as rgb(38 77 228) */
/* ── Common patterns ── */
.overlay { background: rgb(0 0 0 / .5); } /* 50% black overlay */
.glass { background: rgb(255 255 255 / .15); } /* frosted glass */
.shadow { box-shadow: 0 4px 16px rgb(0 0 0 / .1); }
.brand-dim { color: rgb(38 77 228 / .6); } /* muted brand colour */
hsl() — Intuitive Palette Control
hsl() is far more intuitive
for generating palettes. The three axes — hue (colour wheel
position), saturation (intensity), lightness (brightness) — map
directly to design thinking. To create a colour palette, fix the
hue and vary lightness.
/* hsl(hue saturation lightness) */
.el { color: hsl(220 80% 50%); } /* blue — modern syntax, no commas */
.el { color: hsl(220, 80%, 50%); } /* legacy comma syntax — also valid */
.el { color: hsl(220 80% 50% / .8); } /* with alpha */
/* ── Generate a full palette from one hue ── */
:root {
--hue: 220; /* change this ONE value to shift the entire palette */
--sat: 80%;
--blue-50: hsl(var(--hue) var(--sat) 97%);
--blue-100: hsl(var(--hue) var(--sat) 93%);
--blue-200: hsl(var(--hue) var(--sat) 85%);
--blue-300: hsl(var(--hue) var(--sat) 75%);
--blue-400: hsl(var(--hue) var(--sat) 62%);
--blue-500: hsl(var(--hue) var(--sat) 50%); /* base */
--blue-600: hsl(var(--hue) var(--sat) 40%);
--blue-700: hsl(var(--hue) var(--sat) 30%);
--blue-800: hsl(var(--hue) var(--sat) 20%);
--blue-900: hsl(var(--hue) var(--sat) 12%);
}
/* ── Common HSL patterns ── */
.muted { color: hsl(220 30% 60%); } /* low saturation = muted */
.vivid { color: hsl(220 100% 55%); } /* max saturation = vivid */
.disabled { color: hsl(220 10% 70%); } /* desaturated = disabled */
.hover-dark { background: hsl(220 80% 42%); } /* darker shade on hover */
/* ── Colour wheel positions ── */
/* 0/360 = red · 60 = yellow · 120 = green
180 = cyan · 240 = blue · 300 = magenta */
.red { background: hsl(0 80% 55%); }
.yellow { background: hsl(60 90% 50%); }
.green { background: hsl(120 60% 45%); }
.blue { background: hsl(220 80% 50%); }
oklch() — Perceptually Uniform Color
oklch() is the modern gold
standard. Unlike hsl(), it is
perceptually uniform — equal steps in lightness
(L) produce equal-looking changes to the human eye. It also
unlocks wide-gamut P3 colours invisible to
hsl().
/* oklch(lightness chroma hue)
L: 0 (black) → 1 or 100% (white)
C: 0 (grey) → 0.4 max (vivid) — most colours below 0.3
H: 0 → 360 (colour wheel, different from HSL) */
.blue { color: oklch(55% .24 264); } /* vivid blue */
.green { color: oklch(60% .2 145); } /* vivid green */
.red { color: oklch(55% .23 25); } /* vivid red */
.muted { color: oklch(60% .06 264); } /* same hue, lower chroma = muted */
.el { color: oklch(55% .24 264 / .8); } /* with alpha */
/* ── Palette using oklch + custom properties ── */
:root {
--hue: 264; /* change one value → whole palette shifts */
--color-50: oklch(96% .04 var(--hue));
--color-100: oklch(90% .08 var(--hue));
--color-200: oklch(80% .14 var(--hue));
--color-400: oklch(65% .21 var(--hue));
--color-500: oklch(55% .24 var(--hue)); /* base */
--color-700: oklch(38% .22 var(--hue));
--color-900: oklch(20% .14 var(--hue));
}
/* ── lch() — same concept, older (Lab-based, pre-oklch) ── */
.el { color: lch(50% 40 264); } /* similar but oklch is preferred */
color-mix() — Blend Two Colors
color-mix(in colorspace, color1 percentage, color2
percentage)
blends two colours in a specified colour space. This replaces
Sass/LESS tint/shade functions entirely in native CSS:
/* color-mix(in colorspace, color1 pct, color2 pct) */
/* ── Tints (mix with white) ── */
:root {
--brand: #264de4;
--brand-50: color-mix(in oklch, var(--brand) 10%, white);
--brand-100:color-mix(in oklch, var(--brand) 20%, white);
--brand-200:color-mix(in oklch, var(--brand) 40%, white);
--brand-300:color-mix(in oklch, var(--brand) 60%, white);
--brand-400:color-mix(in oklch, var(--brand) 80%, white);
--brand-500:var(--brand); /* base */
--brand-700:color-mix(in oklch, var(--brand) 80%, black);
--brand-900:color-mix(in oklch, var(--brand) 40%, black);
}
/* ── Hover shade — darken by mixing with black ── */
.btn { background: var(--brand); }
.btn:hover { background: color-mix(in oklch, var(--brand) 85%, black); }
/* ── Semi-transparent version ── */
.card-bg { background: color-mix(in oklch, var(--brand) 15%, transparent); }
/* ── Colorspace matters — try different ones ── */
.el { color: color-mix(in srgb, blue, red); } /* muddy purple */
.el { color: color-mix(in oklch, blue, red); } /* vibrant mix */
.el { color: color-mix(in hsl, blue, red); } /* goes through magenta */
/* ── Relative color syntax (CSS Color 5) ── */
/* from a colour, derive a modified version */
.el { color: oklch(from var(--brand) l c h); } /* same colour, unpacked */
.el { color: oklch(from var(--brand) calc(l + .15) c h); } /* lighter by .15 */
.el { color: oklch(from var(--brand) l calc(c * .5) h); } /* half chroma = muted */
.el { color: hsl(from var(--brand) h s calc(l - 10%)); } /* 10% darker */
Color Function Reference
| Function | Syntax | Best for | Gamut |
|---|---|---|---|
| #hex |
#264de4 or
#264de480
|
Quick colour references | sRGB |
| rgb() |
rgb(38 77 228 / .8)
|
Programmatic, RGBA overlays | sRGB |
| hsl() |
hsl(220 80% 50% / .8)
|
Palette generation, intuitive adjustments | sRGB |
| oklch() |
oklch(55% .24 264)
|
Perceptually uniform palettes, P3 colours | Wide (P3+) |
| lch() | lch(50% 40 264) |
Older equivalent to oklch | Wide |
| color-mix() |
color-mix(in oklch, blue 40%, red)
|
Tints, shades, blending any two colours | Any |
| color() |
color(display-p3 0.15 0.3 0.9)
|
Explicit colour space access | P3, a98, etc. |
| Relative color |
oklch(from var(--x) calc(l+.1) c h)
|
Derive lighter/darker/muted variants | Any |
Try It Yourself
All colour functions in one editor — edit the
--hue variable to shift entire
palettes:
Quick Check — Chapter 50
rgb() mixes red, green and
blue channel intensities — there's no intuitive way to "make
this colour lighter" or "make a 10-step palette." You'd have
to know the exact R, G, B values for each shade.hsl() maps directly to how
designers think:— Hue: which colour on the wheel (0° red, 120° green, 240° blue…) — change this once to switch the whole palette hue
— Saturation: how vivid vs grey — increase to make more vivid, decrease for muted/disabled
— Lightness: how bright vs dark — adjust this one axis to create all 10 palette steps
With a fixed hue and saturation, you can generate an entire 10-step colour scale by varying lightness from 95% (near-white) to 10% (near-black) — all from a single base colour defined by one hue value.
The problem with hsl(): two colours at the same lightness (e.g.,
hsl(60 80% 50%) yellow and
hsl(240 80% 50%) blue)
look dramatically different in perceived brightness. Yellow
appears much brighter. This makes it nearly impossible to
create a palette where all colours at the same "level" look
equally balanced.oklch() is perceptually uniform:
oklch(60% .2 60) yellow
and oklch(60% .2 260) blue
actually appear the same perceived brightness to human vision.
A 10-step oklch palette with equal lightness steps at
different hues produces truly balanced, harmonious results.Additionally, oklch accesses wide-gamut P3 colours (colours visible on modern displays that sRGB/hsl cannot reach) by using higher chroma values.
color-mix() does the same
natively in the browser:Tint (lighter version):
Sass:
tint($blue, 50%)CSS:
color-mix(in oklch, #264de4 50%, white)Shade (darker version):
Sass:
shade($blue, 30%)CSS:
color-mix(in oklch, #264de4 70%, black)The advantage of color-mix() over Sass tint/shade is it works with custom properties at runtime:
--btn-hover: color-mix(in oklch, var(--brand) 85%,
black)This means the hover state automatically darkens whatever colour is in
--brand —
which can be changed dynamically (user theme selection, dark
mode, etc.) and the hover derivative updates automatically
with it. Sass compiles to static values; color-mix() is live.
color-mix(in srgb, blue, red)
— interpolates through RGB channels → produces a muddy greyish
purplecolor-mix(in hsl, blue, red)
— interpolates through hue → goes through magenta/pinkcolor-mix(in oklch, blue, red)
— interpolates perceptually → produces a vibrant purpleFor most colour mixing use cases,
in oklch produces the most
visually pleasing results because it interpolates through a
perceptually uniform space — the midpoint looks like a natural
blend rather than a muddy average. This is why Tailwind, Radix
and other modern design systems are moving to oklch as their
primary colour space.
oklch(from <origin-color> <lightness>
<chroma> <hue>)The
from keyword unpacks
the origin colour into its channel variables (l, c, h for
oklch). You can then use those variables directly or modify
them with calc():oklch(from var(--brand) l c h)
— exact same colouroklch(from var(--brand) calc(l + .15) c h)
— 15% lighteroklch(from var(--brand) l calc(c * .5) h)
— half chroma (muted)hsl(from var(--brand) h s calc(l - 10%))
— 10% darker in HSLThis means you can define just one base colour and derive all variants (hover, active, disabled, tint, shade) purely from it — no hardcoded hex values, no preprocessor — and they all update automatically when the base changes.
hsl(220 80% 50%), what
does the 220 represent?
color-mix(in oklch, blue 30%, white)
produce?
oklch(from var(--brand) calc(l - .1) c h)
produce?
-
hex codes:
#264de4(6-digit),#264de480(8-digit with alpha),#f00(3-digit shorthand). -
rgb(): modern syntax uses spaces not commas.
Alpha goes after
/:rgb(38 77 228 / .5). Accepts 0–255 or 0–100% for channels. - hsl(): H = hue angle (0°–360°), S = saturation (0–100%), L = lightness (0–100%). Intuitive for palettes — fix H and S, vary L for 10 palette steps.
- oklch(): perceptually uniform — equal L looks equally bright across all hues. Channels: L (0–1), C (chroma 0–0.4), H (0–360). Preferred for design systems. Accesses P3 wide-gamut colours.
-
color-mix():
color-mix(in oklch, color1 %, color2). Replaces Sass tint/shade. Works with custom properties at runtime. Colour space matters: oklch produces most vibrant results. -
Relative colour syntax:
oklch(from var(--brand) calc(l+.1) c h)derives a modified version of any colour — lighter, darker, muted — with no hardcoded values. -
Modern syntax for all functions: spaces instead
of commas, alpha via
/.rgba()andhsla()as separate functions are legacy — usergb(r g b / a)andhsl(h s l / a).
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions and stay updated with new tutorials.