brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: relative color syntax in CSS makes a see-through or darker version of any color

Css
~/tips/tip-css-relative-color-syntax

background: hsl(from var(--accent) h s calc(l - 10));

Relative color syntax derives transparent or darker variants from one CSS variable.

Say you keep one brand color in a variable and want a pale background and a darker hover from it. Relative color syntax takes the color apart into channels so you can change just one.

.notice {
  --accent: #c2410c;
  background: rgb(from var(--accent) r g b / 15%);
  border-color: var(--accent);
}
.button:hover {
  background: hsl(from var(--accent) h s calc(l - 10));
}

After from you give the origin color. Inside the function the letters stand for its channels: r g b in rgb(), h s l in hsl(). Keep a channel as is, replace it with a number or do math on it with calc(). The first rule keeps the color and only sets the alpha. The second, with --accent defined higher up, keeps hue and saturation and takes 10 off the lightness.

Compared with color-mix(), this gives exact control over one channel instead of a blend with another color. Both are useful; for a transparent tint I find the relative form clearer.

It works in Chrome 122 (Chrome 125 for the hsl() form), Firefox 128 and Safari 18. In an older browser the whole declaration is invalid and ignored, so put a plain fallback color on the line before it.

Further reading: MDN: Using relative colors

More Css tips CSS Overview in Chrome DevTools audits the colors and fonts of a page .notice:empty { .toast { article { counter-reset: h2; } .toolbar { All Css tips (66) All topics

Link