liveBook · Manning

Relative color syntax

As explained in CSS in Depth, Second Edition

Relative color syntax is CSS color notation that derives a new color by exposing and manipulating components of an origin color.

Relative color syntax derives a new color from an origin color by exposing its components for manipulation. It can darken a color, shift its hue, or add transparency while preserving other values. section 11.3.4

The syntax follows a practical sequence:

  • Choose a color function such as oklch(), hsl(), or rgb(). section 11.3.4
  • Write from followed by the origin color, then reference its components as variables. section 11.3.4
  • Use calc() to change selected components while leaving others unchanged. section 11.3.4

Darken or recolor an accent

Use `--accent-blue: oklch(from var(--accent-green) l c calc(h + 100deg));` to preserve lightness and chroma while shifting hue. For transparency, use `oklch(from var(--brand-green) l c h / 0.6)`.

How CSS in Depth, Second Edition covers it

How deep it goes
Explains origin colors, component variables, cross-space conversion, calc(), hue shifts, and alpha, but not fallback implementation.
Examples
Demonstrates darker green, hue-shifted blue, and semitransparent green declarations.
What it assumes
Assumes familiarity with CSS color notation and custom properties while explaining component variables from the beginning.
What you can do after
Builds derived CSS colors by manipulating an origin color’s components with relative notation.
How it connects
Connects CSS color functions, color spaces, custom properties, calc(), hue, lightness, chroma, and alpha channels.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.