Sub topic 8
Deriving colors with relative color syntax
Relative color syntax lets you write a new color as a change to an existing one. You take a base color, split it into channels, and adjust the channels you want to change. This chapter covers the syntax, the channel keywords, common derivations, and the errors that catch people out.
It assumes you can write oklch() colors, use color-mix(), and define CSS custom properties. If you can't yet, review those chapters first.
The problem it solves
Suppose a design has one brand color and you need a darker hover state and a half-transparent border. You have three options without relative color syntax:
- Hard-code each variant as a separate value. The variants drift out of sync when the brand color changes.
- Use
color-mix()with black, white ortransparent. This works for tints and shades but gives you no control over a single channel such as chroma. - Compute values in a preprocessor, which can't see runtime values such as a theme switched by a custom property.
Relative color syntax runs in the browser, at style computation time. It reads whatever color the origin currently resolves to, so one change to --brand updates every derived color.
The syntax
A relative color puts the keyword from and an origin color inside a color function. After the origin, you list the three channels and an optional alpha.
:root {
--brand: oklch(62% 0.19 255);
}
.button {
background: oklch(from var(--brand) calc(l - 0.1) c h);
border-color: oklch(from var(--brand) l c h / 0.4);
}The browser evaluates this in four steps:
- It resolves the origin color. Here that is the value of
--brand. - It converts the origin into the color space of the function you wrote. This is OKLCH here.
- It binds the channel keywords
l,c,handalphato the converted numbers. - It evaluates each channel expression you wrote and builds the result.
The first color in oklch(from var(--brand) l c h) is not required to be OKLCH. Any valid color works as the origin, including hex, rgb(), a named color, currentcolor, or the result of color-mix(). The browser converts it for you.
What each channel keyword means
Inside oklch(from ...), the keywords resolve to plain numbers:
| Keyword | Meaning | Typical range |
|---|---|---|
| `l` | Lightness | 0 to 1 |
| `c` | Chroma | 0 to about 0.4 |
| `h` | Hue angle in degrees | 0 to 360 |
| `alpha` | Opacity | 0 to 1 |
Lightness is the one that surprises people. In a normal oklch() value you write 62%. In a relative expression, l is the number 0.62. So you subtract 0.1 to darken, not 10%.
.button {
/* Correct: l is a number between 0 and 1 */
background: oklch(from var(--brand) calc(l - 0.1) c h);
}You can still write a percentage as a literal replacement for one channel. For example, oklch(from var(--brand) 40% c h) sets lightness to a fixed value. What you can't do is mix a percentage and the number l inside one calc().
Each channel is independent, so you can replace, offset or scale any of them:
- A fixed value replaces the channel:
oklch(from var(--brand) 0.5 c h). calc(l + 0.1)offsets it.calc(c * 0.5)scales it.- The bare keyword keeps it unchanged.
Common derivations
All of these assume --brand: oklch(62% 0.19 255).
Lighter and darker variants. Change only l.
.surface-light { background: oklch(from var(--brand) calc(l + 0.3) c h); }
.surface-dark { background: oklch(from var(--brand) calc(l - 0.3) c h); }Muted variant. Scale chroma down and leave lightness and hue alone.
.muted {
background: oklch(from var(--brand) l calc(c * 0.4) h);
}Alpha variants. The part after the slash is the alpha channel. A bare number replaces the opacity, and the alpha keyword lets you scale the existing opacity.
.overlay {
background: oklch(from var(--brand) l c h / 0.4);
}
.fade {
background: oklch(from var(--brand) l c h / calc(alpha * 0.5));
}The second rule is the safer pattern when the origin may already be translucent. Replacing alpha with 0.4 would discard any transparency the origin had.
Hue rotation. Add to h for related accent colors. Hue is an angle, so a rotation of 180 gives the opposite color.
.accent {
background: oklch(from var(--brand) l c calc(h + 180));
}A value above 360 or below 0 is fine. The browser normalizes the angle.
Converting from another notation. The origin can be a legacy color. This is useful when a brand color arrives as a hex value you don't want to rewrite.
.legacy {
background: oklch(from #3b82f6 calc(l - 0.1) c h);
}Using other color functions
Relative syntax is not limited to oklch(). Every color function accepts from, with its own channel keywords.
.a { color: rgb(from var(--brand) r g b / 0.5); }
.b { color: hsl(from var(--brand) h s calc(l - 10)); }
.c { color: oklab(from var(--brand) calc(l - 0.1) a b); }Pay attention to the number ranges. In rgb(), the keywords r, g and b are 0 to 255. In hsl(), s and l are numbers from 0 to 100, so you subtract 10, not 0.1. The hue, saturation and lightness keywords in hsl() keep the weaknesses covered in the earlier chapter on perceptual uniformity. Prefer oklch() for adjustments that need to look consistent.
Combining with color-mix() and custom properties
color-mix() can be the origin of a relative color. This lets you mix first and fine-tune after.
.card {
background: oklch(
from color-mix(in oklch, var(--brand) 20%, white)
l calc(c * 0.8) h
);
}You can also go the other way and pass a relative color into color-mix():
.badge {
background: color-mix(
in oklch,
oklch(from var(--brand) l c calc(h + 40)),
white 70%
);
}A useful token pattern stores the origin in one custom property and builds every state from it. Here is a complete example you can paste into a page.
:root {
--brand: oklch(62% 0.19 255);
--brand-hover: oklch(from var(--brand) calc(l - 0.08) c h);
--brand-active: oklch(from var(--brand) calc(l - 0.14) c h);
--brand-border: oklch(from var(--brand) l c h / 0.4);
--brand-disabled: oklch(from var(--brand) l calc(c * 0.3) h / 0.6);
}
.button {
background: var(--brand);
border: 2px solid var(--brand-border);
color: white;
}
.button:hover { background: var(--brand-hover); }
.button:active { background: var(--brand-active); }
.button:disabled { background: var(--brand-disabled); }Because the derived tokens refer to var(--brand), overriding --brand in a scope updates them there. Note one caveat: a custom property that contains var() is resolved where it is declared. If you redefine --brand on .theme-green but --brand-hover is declared only on :root, the hover token is computed from the root value and ignores the override. Redeclare the derived tokens in the themed scope, or declare them on * or on the component.
.theme-green {
--brand: oklch(65% 0.17 150);
--brand-hover: oklch(from var(--brand) calc(l - 0.08) c h);
}Gamut, clamping and the achromatic edge case
Adjusting channels can produce colors your display can't show. Raising c on an already vivid color is the usual cause. The browser maps these colors into the display gamut when it paints them. You get a visible color, but it may differ from what the numbers imply, and two colors you expected to differ may look the same.
Lightness is clamped to the 0 to 1 range, and chroma can't go below 0. So calc(l - 0.5) on a mid-dark color gives black, not an error.
Achromatic origins are another edge case. Gray, white and black have no meaningful hue. When you convert them to OKLCH, the hue channel is treated as missing. Depending on the browser and the origin, h may resolve to 0. If you rotate the hue of a gray and keep c at 0, nothing visible changes. To tint a gray you must set chroma explicitly.
.tinted-gray {
background: oklch(from gray l 0.03 250);
}Errors you will meet
- An invalid color is silently dropped. If a relative color fails to parse, the declaration is ignored and the cascade falls back to an earlier declaration. DevTools shows the property as invalid.
- Wrong units in
calc().calc(l - 10%)inoklch()mixes a number and a percentage and is invalid. Usecalc(l - 0.1). - An unknown keyword. Each function only defines its own names.
his not defined inrgb(), andris not defined inoklch(). - A custom property that isn't a color. If
--brandis not a valid color at computed time, every color derived from it is invalid too. This commonly happens when a variable is unset. - Expecting a perceptual match across hues.
calc(l - 0.1)darkens by the same amount of lightness for every hue. But the resulting colors still need a contrast check. The later chapter on contrast covers this.
Progressive enhancement
Relative color syntax has newer browser support than plain oklch(). Check current compatibility tables before relying on it. Unsupported syntax makes the declaration invalid, so the usual approach is a fallback declaration first and an @supports block for the new syntax.
:root {
--brand: #3b82f6;
--brand-hover: #2563eb;
}
@supports (color: oklch(from red l c h)) {
:root {
--brand: oklch(62% 0.19 255);
--brand-hover: oklch(from var(--brand) calc(l - 0.08) c h);
}
}The @supports test uses a real relative color, so it passes only in browsers that parse the syntax. This is more accurate than testing for plain oklch() support, because a browser can support one and not the other.
When to choose it
| Need | Best tool |
|---|---|
| Blend two different colors | `color-mix()` |
| Tint or shade toward white or black | `color-mix()` |
| Change one channel, such as chroma only | Relative color syntax |
| Change opacity of a variable color | Relative color syntax |
| Rotate hue by a fixed angle | Relative color syntax |
Use color-mix() when the idea is "between these two colors". Use relative color syntax when the idea is "this color, but with one property changed". They compose, so you don't have to pick one for a whole project.
Exercise
Given --brand: oklch(62% 0.19 255), write a declaration for a background that has the same hue and lightness as --brand but half the chroma and 80% of its original opacity. Then say what is wrong with writing calc(l - 10%) in an oklch() relative color.
Show answer
background: oklch(from var(--brand) l calc(c 0.5) h / calc(alpha 0.8)); In oklch() relative syntax, l is a plain number from 0 to 1, so subtracting a percentage mixes incompatible types and the declaration is invalid. Use calc(l - 0.1) instead.
Exercise
You declare --brand-hover: oklch(from var(--brand) calc(l - 0.08) c h) on :root, then set --brand to a different color on .theme-green. Inside .theme-green, the hover color still matches the root brand. Why, and how do you fix it?
Show answer
A custom property containing var() is resolved where it is declared, so --brand-hover was computed from the root value of --brand. Redeclare --brand-hover inside .theme-green so it is computed from the overridden --brand.