Modern Color Functions
CSS color has moved beyond hex codes and rgb(). The newer functions oklch() and color-mix() solve a problem that older color models never could: they let you build palettes where every step looks evenly spaced to the human eye, and they let you derive an entire color ramp from a single seed value without leaving your stylesheet.
Everything on this page feeds directly into the Tokenized Color System and Dark Mode assignment, where you will wire your generated ramp into semantic tokens and implement dark mode on your portfolio.
The Problem with RGB and HSL
If you have ever tried to build a color scale in hsl() by stepping the lightness channel in even increments, you have probably noticed that the results do not look even. A 50% lightness blue and a 50% lightness yellow do not appear equally bright. That is because HSL describes how screens mix light, not how human vision perceives the result.
The demo below shows this in action. Both rows use the same lightness values at 30%, 50%, and 70%. The top row uses HSL, where the steps look uneven and the yellow appears far brighter than the blue at the same lightness. The bottom row uses OkLCH, where equal lightness values actually look equally bright. Use the hue slider to sweep through different colors and watch how HSL breaks down while OkLCH stays consistent:
This inconsistency makes it nearly impossible to build a reliable color system in HSL. When your brand color, success green, and warning red all have different perceived brightness at the same lightness value, your buttons, badges, and status indicators look inconsistent. Users notice, even if they cannot articulate why.
Perceptual Uniformity
A color space is perceptually uniform when equal numeric steps produce equal perceived differences to the human eye. If you increase lightness by 10 units, the color should look 10 units brighter regardless of the hue. That is exactly what the Oklab color space delivers, and it is what CSS adopted as the foundation for oklch() and oklab().
The History Behind Oklab
The CIE Lab color space, developed in 1976, was the first widely adopted attempt at perceptual uniformity. It separated lightness from two chromatic axes, making it easier to compare colors the way humans see them. But decades of use revealed that Lab still had problems. Hue shifts appeared when interpolating between certain colors, and the blue region was noticeably less uniform than the rest.
In 2020, Björn Ottosson published a refined model called Oklab that corrected these issues. It keeps the same structure (one lightness channel plus two chromatic axes) but uses an improved transformation that produces more even results across all hues, particularly in the blue and purple regions. CSS adopted Oklab and its cylindrical sibling OkLCH as native color functions.
oklch(): Lightness, Chroma, and Hue
The oklch() function uses three channels: lightness, chroma, and hue. If you have used HSL before, the shape is familiar, but the channels are perceptually calibrated.
/* oklch(lightness chroma hue) */
.brand {
color: oklch(0.65 0.2 250);
}
-
Lightness (L):
0(black) to1(white). Unlike HSL, a value of0.5looks like medium brightness on every hue. -
Chroma (C): How vivid the color is, starting at
0(gray). Most sRGB colors stay below0.3. Think of it as the distance from the gray center of the color wheel. -
Hue (H): An angle from
0to360, just like HSL, but equal angular steps produce roughly equal perceived shifts.
Use the demo below to experiment with each channel and see how the color changes. Pay attention to how the lightness slider produces a smooth, even gradient regardless of the hue:
You can write lightness as a percentage (65% instead of 0.65) and chroma as a percentage of a reference maximum (50% instead of a raw decimal). The decimal form is more common in documentation and tooling, but both are valid CSS.
oklab(): The Cartesian Sibling
Where oklch() uses cylindrical coordinates (lightness, chroma, hue angle), oklab() uses Cartesian coordinates (lightness, a axis, b axis). Both describe the same underlying Oklab color space. The a axis goes from green (negative) to red (positive), and the b axis goes from blue (negative) to yellow (positive).
/* oklab(lightness a b) */
.accent {
color: oklab(0.65 -0.1 0.15);
}
Use oklch() when you are thinking about hue and saturation directly, which is most of the time for palette work. Use oklab() when you need to interpolate between two very different colors and want the straightest possible path through the color space, because Cartesian interpolation avoids the unexpected bright or desaturated midpoints that can happen with cylindrical interpolation.
color-mix(): Deriving Colors from a Seed
The color-mix() function blends two colors in a specified color space. This is the tool that makes palette generation practical in pure CSS, because you can derive tints and shades from a single seed without hardcoding every value.
/* color-mix(in <space>, <color> <percentage>, <color>) */
.tint {
background: color-mix(in oklch, #2563eb 80%, white);
}
The first argument is the interpolation space. Always use oklch (or oklab) unless you have a specific reason not to, because mixing in srgb produces muddy, uneven results. The percentage controls how much of the first color to keep. 80% means 80% seed, 20% white. If you omit percentages entirely, the function defaults to a 50/50 blend.
The demo below mixes the same two hues at 50/50 in three different spaces. OkLCH keeps chroma and hue more even through the blend, while sRGB and HSL often go gray or shift hue in the middle. Drag either hue slider and compare the three results side by side.
The most common recipe is mixing your seed with white to create tints (lighter steps) and with black to create shades (darker steps):
:root {
--seed: oklch(0.55 0.2 260);
/* Tints (mix seed with white) */
--blue-200: color-mix(in oklch, var(--seed) 30%, white);
--blue-300: color-mix(in oklch, var(--seed) 50%, white);
--blue-400: color-mix(in oklch, var(--seed) 75%, white);
/* Base */
--blue-500: var(--seed);
/* Shades (mix seed with black) */
--blue-600: color-mix(in oklch, var(--seed) 80%, black);
--blue-700: color-mix(in oklch, var(--seed) 60%, black);
--blue-800: color-mix(in oklch, var(--seed) 40%, black);
}
Because the mixing happens in OkLCH, the lightness steps are perceptually even. Step 300 actually looks like it sits halfway between 200 and 500. The demo below lets you pick any seed hue and watch the ramp generate in real time. It also shows hover, active, and disabled state variants derived from the base step:
State Recipes
Once you have a base color, you can use color-mix() to derive predictable hover, active, and disabled states without defining separate values for each one:
.button {
--btn-bg: var(--blue-500);
background: var(--btn-bg);
}
.button:hover {
/* 15% darker */
background: color-mix(in oklch, var(--btn-bg) 85%, black);
}
.button:active {
/* 30% darker */
background: color-mix(in oklch, var(--btn-bg) 70%, black);
}
.button:disabled {
/* Desaturate by mixing toward gray */
background: color-mix(in oklch, var(--btn-bg) 40%, oklch(0.75 0 0));
cursor: not-allowed;
}
This works for any seed color. If you later swap --blue-500 for a green or a purple, all the state variants update automatically because they are derived, not hardcoded. This is the kind of systematic thinking that separates a fragile color system from a resilient one.
You cannot decompose a custom property into individual channels in standard CSS today. Mixing with black or white lets you darken or lighten any stored color without knowing its internal values. The Relative Color Syntax choose topic covers an emerging alternative that can adjust individual channels, but color-mix() is the reliable, widely supported tool available right now.
Gamut and Fallbacks
Not every oklch() value fits inside the sRGB gamut that standard monitors display. If you set a very high chroma, the browser automatically maps the color to the nearest displayable value. You will not see an error, but the result may look slightly different from what you specified. For reliable sRGB output, keep chroma below about 0.3.
If you need to support older browsers that do not understand oklch() at all, use the standard cascade fallback pattern:
.brand {
/* Fallback for browsers without oklch support */
color: #2563eb;
color: oklch(0.55 0.25 260);
}
As of 2025, oklch() and color-mix() are supported in all major evergreen browsers, so this fallback is primarily a safety net for unusual environments.
More on Wide Gamut and Display P3
Modern displays (especially Apple devices) support the Display P3 color gamut, which is roughly 50% larger than sRGB. When you write a high chroma value in oklch(), you may be describing a P3 color that looks more vivid on a capable display but gets clamped on an sRGB screen. The Wide Gamut and Display P3 choose topic explores this in depth, including how to use color-gamut media queries to serve different values to different screens.
From Seed to System
The workflow you will follow from here forward looks like this: start with one seed color expressed in oklch(). Use color-mix(in oklch, ...) to derive a full ramp of tints and shades, plus interactive state variants. Store every step as a custom property so your entire palette updates when the seed changes. In the Tokenized Color System and Dark Mode assignment, you will take these raw ramp values and map them to semantic token names like --color-surface and --color-text, then wire them into light-dark() for automatic theme switching.
The Apostle Paul taught the Corinthians that "there are diversities of gifts, but the same Spirit" (1 Corinthians 12:4). A well designed color system follows a similar principle. Every shade in your palette is a different expression of the same underlying seed, unified by consistent derivation rules rather than isolated choices. When you change the seed, the entire family moves together.
Check Your Understanding
A student just completed a learning activity about modern CSS color functions. They learned how oklch() works with its three channels (lightness, chroma, hue), how color-mix(in oklch, ...) blends a seed color with white to create tints and with black to create shades, and how to derive hover, active, and disabled state variants using color-mix(). They also saw interactive demos showing how HSL lightness is not perceptually uniform while oklch lightness is.\n They were given a partially completed color ramp with some steps already filled in and asked to complete the missing ones by following the established pattern. Review their code and give direct feedback. Check that they kept the interpolation space as oklch in every color-mix() call. For tints, the seed percentage should decrease as the step number gets lower (less seed means lighter), following the pattern of 200 at 30% and 400 at 75%, so 300 should be somewhere in between. For shades, the seed percentage should decrease as the step number gets higher (less seed means darker), following the pattern of 600 at 80% and 800 at 40%, so 700 should be somewhere in between. The hover state should mix the seed with about 85% seed and 15% black, the active state around 70% seed and 30% black, and the disabled state should mix toward a neutral gray like oklch(0.75 0 0) rather than just mixing with black. Watch for common mistakes like mixing in srgb instead of oklch, confusing percentage direction, or forgetting the percentage sign. Guide them back to the reading concepts rather than giving direct answers.
/**
* Fill in the missing color-mix() values.
*
* Some steps are already done for you. Look at the
* pattern they follow, then complete the gaps.
*
* Tints: mix the seed with white. Lower step numbers
* use less seed (lighter result).
*
* Shades: mix the seed with black. Higher step numbers
* use less seed (darker result).
*
* States: hover is slightly darker than 500, active is
* noticeably darker, and disabled mixes toward a
* neutral gray to remove saturation.
*/
:root {
--seed: oklch(0.55 0.2 260);
/* --- Tints --- */
--blue-200: color-mix(in oklch, var(--seed) 30%, white);
--blue-300: /* fill in: between 30% and 75% */
--blue-400: color-mix(in oklch, var(--seed) 75%, white);
/* --- Base --- */
--blue-500: var(--seed);
/* --- Shades --- */
--blue-600: color-mix(in oklch, var(--seed) 80%, black);
--blue-700: /* fill in: between 80% and 40% */
--blue-800: color-mix(in oklch, var(--seed) 40%, black);
/* --- State variants --- */
--btn-hover: /* about 85% seed, rest black */
--btn-active: /* about 70% seed, rest black */
--btn-disabled: /* about 40% seed, rest neutral gray */
}