Tutorials CSS Color Functions
Ch 50 / 60
Chapter 50 Intermediate 10 min read CSS

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.

What you'll learn
hex, rgb(), rgba() — classic colour formats
hsl() — intuitive palette control
Alpha channel and transparency
oklch() — perceptually uniform colour
color-mix() — blend any two colours
Relative colour syntax — derive variants

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 /:

rgb(38 77 228 / 80%)
` R — red
0–255 or 0–100%
G — green
0–255 or 0–100%
B — blue
0–255 or 0–100%
/ alpha
0–1 or 0–100%
rgb(255 0 0)
rgb(0 128 0)
rgb(0 0 255)
rgb(255 165 0)
rgb(128 0 128)
#264de4
rgb(255 255 255)
rgb(15 23 42)
Alpha channel — transparent on checkered background
alpha 1
alpha .75
alpha .5
alpha .25
alpha 0
CSS — hex, rgb() and rgba()
/* ── 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.

Hue — 0° to 360° around the colour wheel
0° red 60° yellow 120° green 180° cyan 240° blue 300° pink
Lightness scale — hsl(220 80% L%) — same hue, varying lightness
10%
20%
35%
50%
65%
75%
85%
92%
96%
99%
Saturation scale — hsl(220 S% 50%) — same hue, varying saturation
0%
20%
40%
60%
80%
100%
Full colour palette — one hue, 10 lightness steps
CSS — hsl() palette patterns
/* 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().

hsl() — unequal perceived lightness
H:0 L:50%
H:60 L:50%
H:120 L:50%
H:240 L:50%

Same L:50% — but yellow looks far brighter than blue

oklch() — equal perceived lightness ✓
H:25
H:90
H:145
H:260

Same L:60% — all appear equally bright to the eye

oklch() palette — vary lightness at hue 260 (blue)
L:96%
L:90%
L:80%
L:68%
L:55%
L:42%
L:30%
L:20%
L:12%
CSS — oklch() syntax and patterns
/* 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 oklch, #264de4 [%], white) — tints
90%
75%
60%
40%
20%
10%
color-mix(in oklch, #264de4 [%], black) — shades
90%
75%
60%
40%
20%
10%
Blending two brand colours
blue
50/50
25/75
green
CSS — color-mix() and relative color syntax
/* 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
Free Resource
CSS3 Cheat Sheet — Color Functions & Palettes
Complete reference covering all CSS color functions, oklch palettes and color-mix patterns.
Download PDF

Try It Yourself

All colour functions in one editor — edit the --hue variable to shift entire palettes:

Live Editor
✎ Editor
👁 Preview
Featured Course — Enroll Now
Modern Web Development With AI
Master HTML, CSS, JavaScript, React and Node.js with real projects and placement support.
120+
Chapters
40 hrs
Content
10 Real
Projects
4.9 ⭐
Rating
HTML5CSS3JavaScriptReact.jsNode.jsTailwind CSSAI Tools

Quick Check — Chapter 50

Questions & Answers
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.
"Perceptually uniform" means equal numerical steps produce equal-looking visual changes to the human eye.

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.
Sass has tint/shade helper functions that mix a colour with white or black. 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.
The colour space determines how the interpolation between the two colours happens. Mixing the same two colours in different spaces produces very different results:

color-mix(in srgb, blue, red) — interpolates through RGB channels → produces a muddy greyish purple
color-mix(in hsl, blue, red) — interpolates through hue → goes through magenta/pink
color-mix(in oklch, blue, red) — interpolates perceptually → produces a vibrant purple

For 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.
Relative color syntax (CSS Color Level 5) lets you derive a modified version of an existing colour by unpacking its components and recombining them with adjustments:

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 colour
oklch(from var(--brand) calc(l + .15) c h) — 15% lighter
oklch(from var(--brand) l calc(c * .5) h) — half chroma (muted)
hsl(from var(--brand) h s calc(l - 10%)) — 10% darker in HSL

This 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.
Multiple Choice Questions
Question 1 of 5
In hsl(220 80% 50%), what does the 220 represent?
Question 2 of 5
How do you write an rgba() colour with 50% opacity in modern CSS Color Level 4 syntax?
Question 3 of 5
Why does oklch() produce more predictable palettes than hsl()?
Question 4 of 5
What does color-mix(in oklch, blue 30%, white) produce?
Question 5 of 5
What does oklch(from var(--brand) calc(l - .1) c h) produce?
Chapter Summary
  • 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() and hsla() as separate functions are legacy — use rgb(r g b / a) and hsl(h s l / a).
~10 min read
Intermediate level
Chapter 50 of 60

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel