# Relative Colors

> The relative color syntax in CSS creates a new color based on an existing one by modifying its channels.

## How do I lighten, darken or add transparency to a CSS color?

The syntax that derives a new color from an existing one using the `from` keyword, for example `oklch(from var(--brand) l c h / 50%)`. Allows creating lighter, darker or transparent variants of a color without preprocessors.

- Topic: Colors
- Type: concept, syntax
- Browser support: Chrome 122+, Chrome Android 122+, Edge 122+, Firefox 128+, Firefox Android 128+, Safari 18+, Safari iOS 18+

## Example

Wrong:

```css
:root {
  --brand: #1f5eff;
  --brand-50: #1f5eff80;
}
```

Right:

```css
:root {
  --brand: #1f5eff;
  --brand-50: rgb(from var(--brand) r g b / 50%);
}
```

The variant is derived from `--brand`, so changing the brand color updates its transparent version too.

## Common mistakes

- Writing channel math without `calc()`, which makes the color invalid.
- Using channel names from another space, like `l` inside `rgb(from ...)`.

## Key points

- Shades and transparent variants use `from` syntax instead of extra hardcoded hex values.
- Lightness changes use `oklch(from ...)`, so the hue stays stable.
- Channel math is wrapped in `calc()`, like `calc(alpha / 2)`.
- Generated text and background pairs are checked for contrast.

## Interview questions

### Can I use relative color syntax with hex colors?

Yes. The origin after `from` can be any color, including hex, names or `var()`, and the outer function sets the output space.

### How do I make a darker shade with relative color syntax?

Lower the lightness channel, like `hsl(from var(--brand) h s calc(l - 10))`, or do the same in `oklch()` for a steadier hue.

## Related cards

- [oklch()](https://wonderium.dev/css/oklch.md)
- [color-mix()](https://wonderium.dev/css/color-mix.md)
- [contrast-color()](https://wonderium.dev/css/contrast-color.md)
- [Variables](https://wonderium.dev/css/variables.md)
- [hsl()](https://wonderium.dev/css/hsl.md)

---

- Level: [CSS](https://wonderium.dev/css.md)
- HTML version: https://wonderium.dev/css/relative-colors
- Last updated: 2026-11-25
