Sub topic 3
Writing colors with oklch()
This chapter covers the oklch() function itself: what each of its three values does, how to add transparency, and how to change one value without disturbing the others. It assumes you know basic CSS syntax and selectors, CSS custom properties, and the hex, rgb() and hsl() notations. The previous chapter explained why OKLCH is more perceptually uniform than HSL. Here you write the colors.
The syntax
An oklch() color has three components separated by spaces, plus an optional alpha value after a slash.
:root {
--brand: oklch(62% 0.19 255);
--brand-translucent: oklch(62% 0.19 255 / 0.5);
}The general form is:
color: oklch(L C H / alpha);There are no commas. The legacy rgb(0, 0, 0) and hsl(0, 0%, 0%) forms allow commas, but oklch() only exists in the modern space-separated syntax. A comma makes the whole declaration invalid, and the browser ignores it.
The three components are lightness, chroma and hue.
The three components
| Component | Meaning | Range | Typical values |
|---|---|---|---|
| L (lightness) | How light or dark the color appears | `0%` (black) to `100%` (white), or `0` to `1` | `15%` very dark, `50%` medium, `95%` very light |
| C (chroma) | How colorful the color is | `0` (gray) to about `0.4`, or `0%` to `100%` | `0.02` muted, `0.1` moderate, `0.2` vivid |
| H (hue) | Position on the color wheel | `0` to `360` degrees | `30` red, `90` yellow, `145` green, `255` blue |
Lightness
Lightness is perceptual. A yellow and a blue with the same L look about equally light to a person. This is the main difference from hsl(), where hsl(60 100% 50%) (yellow) looks far brighter than hsl(240 100% 50%) (blue) even though both have 50% lightness.
You can write lightness as a percentage or as a number from 0 to 1. These are the same color:
.a { color: oklch(62% 0.19 255); }
.b { color: oklch(0.62 0.19 255); }Chroma
Chroma measures how far the color is from gray at that lightness. At 0 the color is a neutral gray. It has no strict upper limit in the syntax, but real displays cannot show values much above 0.3 to 0.37, and the usable maximum depends on the lightness and the hue. In the percentage form, 100% equals 0.4, so 50% equals 0.2.
Chroma is not saturation. HSL saturation is relative to the maximum the sRGB space allows at that point. OKLCH chroma is an absolute distance, so the same chroma value means the same amount of color at any hue.
Hue
Hue is an angle. A bare number is read as degrees, and you can also write units: 255deg, 0.708turn, 4.45rad and 283grad all name roughly the same angle. The numbers differ from HSL hues because OKLCH arranges the wheel by perception. Approximate landmarks:
| Hue angle | Roughly |
|---|---|
| 30 | red |
| 70 | orange |
| 100 | yellow |
| 145 | green |
| 195 | cyan |
| 255 | blue |
| 305 | purple |
| 350 | pink |
Treat these as starting points and adjust by eye. Hue 0 is a pinkish red, not pure red.
Alpha
Alpha follows a slash and accepts a number from 0 to 1 or a percentage. These are equivalent:
.overlay-a { background: oklch(20% 0.02 255 / 0.6); }
.overlay-b { background: oklch(20% 0.02 255 / 60%); }Leave out the slash and alpha for a fully opaque color. Alpha does not change the other three channels. A translucent version of a color has the same lightness, chroma and hue, and the page behind it shows through.
Changing one channel at a time
The practical benefit of oklch() is that each channel moves one visual property and leaves the others alone. A hover state is the simplest case. Lowering only the lightness gives a darker version of the same color:
.button {
background: oklch(60% 0.15 250);
}
.button:hover {
background: oklch(52% 0.15 250);
}The hover background has the same hue and chroma. In HSL, darkening by lowering L often makes colors look duller or shifts the apparent hue, and the result depends on which hue you started with. In OKLCH, an 8-point drop in lightness looks like a similar step for every hue.
The same applies to the other channels:
- Change only
Cto make a color more muted or more vivid without making it lighter or darker. - Change only
Hto move to a related color of similar weight, for example from a blue button to a green one. - Change only alpha to fade the color.
A lightness scale
Because lightness is perceptual, a scale built by stepping L while holding C and H looks even. Custom properties make the hue easy to swap:
:root {
--hue: 255;
--step-100: oklch(95% 0.03 var(--hue));
--step-300: oklch(80% 0.09 var(--hue));
--step-500: oklch(62% 0.17 var(--hue));
--step-700: oklch(45% 0.15 var(--hue));
--step-900: oklch(28% 0.08 var(--hue));
}
.theme-green {
--hue: 145;
}Notice that chroma is not constant here. It is low at the light and dark ends and highest near the middle. Very light and very dark colors cannot hold much chroma on a real screen, so scales usually taper chroma at the extremes. The next chapters build on this when they cover palettes and tokens.
A family of hues
To get colors that feel like they belong together, keep L and C the same and vary only H:
.tag-red { background: oklch(65% 0.15 30); }
.tag-yellow { background: oklch(65% 0.15 100); }
.tag-green { background: oklch(65% 0.15 145); }
.tag-blue { background: oklch(65% 0.15 255); }The four backgrounds should look equally bright. This is how you get categorical colors for charts and badges that none of them dominates. The same trick in HSL gives noticeably uneven results.
Neutrals
For a neutral gray, set chroma to 0. The hue then has no effect, and you can write any value:
.gray {
color: oklch(70% 0 0);
}A small amount of chroma gives a tinted neutral that matches a brand color without looking colored:
.surface {
background: oklch(97% 0.01 255);
}At low chroma values like 0.01, the hue still matters a little. Reusing your brand hue for all neutrals gives surfaces that feel consistent with the rest of the design.
Edge cases and common errors
The following problems come up often when people first write oklch() by hand.
- A bare number for lightness above 1. A plain number is on the 0 to 1 scale, so
oklch(62 0.19 255)means 6200% lightness, which clamps to white. Write62%or0.62. - Commas.
oklch(62%, 0.19, 255)is invalid. Use spaces. - A percent sign on hue. Hue is an angle.
255%is invalid. - Treating chroma like HSL saturation.
oklch(62% 1 255)does not mean "fully saturated". The number1is far beyond any display's range, and the browser will pull it back into displayable colors, with results that vary by browser and display. - Assuming the same chroma works at every lightness and hue. Some combinations cannot be displayed. A very light, highly chromatic blue, such as
oklch(95% 0.25 255), does not exist on standard displays. The browser maps it to the nearest color it can show, and the result may not be the one you expected. The gamut chapter covers how to detect and handle this. - Expecting hue to mean the same thing as in HSL. Copying
hsl(240 ...)asoklch(... 240)gives a different blue. Pick new hue values, using the landmarks above or a picker.
Browsers also accept the keyword none in place of any component, for example oklch(none 0.1 250). It means "this channel is missing" and matters mostly when colors are interpolated or derived from other colors. You will rarely write it by hand at this point.
A short worked example
This stylesheet puts the pieces together. It defines one hue and builds a primary button, a hover state, a focus ring and a muted card from it.
:root {
--hue: 255;
--primary: oklch(55% 0.19 var(--hue));
--primary-hover: oklch(47% 0.19 var(--hue));
--ring: oklch(55% 0.19 var(--hue) / 0.4);
--surface: oklch(97% 0.01 var(--hue));
--text: oklch(25% 0.03 var(--hue));
}
.card {
background: var(--surface);
color: var(--text);
padding: 1rem;
border-radius: 0.5rem;
}
.button {
background: var(--primary);
color: oklch(98% 0 0);
border: 0;
padding: 0.5rem 1rem;
border-radius: 0.25rem;
}
.button:hover {
background: var(--primary-hover);
}
.button:focus-visible {
outline: 3px solid var(--ring);
outline-offset: 2px;
}Changing --hue to 145 re-themes the whole page, and every color keeps the same relative lightness and chroma. Each variant is still written out by hand. Later chapters replace that repetition with color-mix() and relative color syntax. Also, keeping the text lightness at 25% does not by itself guarantee readable text, because contrast depends on both foreground and background. A later chapter shows how to check it.
Summary of what to remember
- Write
oklch(L C H / alpha)with spaces and no commas. - Lightness is perceptual, chroma is an absolute amount of color, and hue is an angle with its own landmarks.
- Move one channel to get one visual change.
- Use
62%or0.62for lightness, never62. - Not every combination can be displayed, so very high chroma at extreme lightness gets adjusted by the browser.
Exercise
A teammate writes background: oklch(70 0.12 145); for a green badge, and the badge shows up white. What is wrong, and how do you fix it?
Show answer
A plain number for lightness is on the 0 to 1 scale, so 70 means 7000% and is clamped to 100%, which is white. Write the lightness as 70% or 0.7: oklch(70% 0.12 145).
Exercise
You have .link { color: oklch(55% 0.18 255); }. Write a rule so that on hover the link is darker but keeps the same hue and chroma, and write a second color that is the same blue at 30% opacity.
Show answer
For the hover state, lower only the lightness: .link:hover { color: oklch(47% 0.18 255); }. For the translucent color, add alpha after a slash: oklch(55% 0.18 255 / 0.3) (or / 30%).
Exercise
You want four category colors for a chart that look equally bright, using hues 30, 100, 145 and 255. Which components do you hold constant and which do you vary, and why does this work better in oklch() than in hsl()?
Show answer
Hold lightness and chroma constant (for example oklch(65% 0.15 H)) and vary only the hue. This works because OKLCH lightness is perceptual, so equal L values look equally light across hues. In HSL, equal L values do not look equally light, so yellow looks much brighter than blue.