Relative colour syntax in CSS allows seamless colour manipulation, enabling developers to perform various actions such as lightening, darkening, saturating, desaturating, adjusting opacity, and inverting to name just a few.
Example 1: Lightening a brand colour
Here the from
function has an input which is the colour you want to convert and then outputs the channels of your choice.
Here we are converting hue, saturation, lightness (hsl
) channels because we’re setting the from function to work on our hsl()
input.
Each channel can then be edited as can be seen below a multiplier has been added to the lightness channel of hsl
.
Breaking it down:
If we replace the variable var(--brand-color)
with blue
it will make it clearer to read:
hsl(from blue h s calc(l * 1.25))
the full thingfrom blue h s calc(l * 1.25)
the from function, e.g. without calc:from blue h s l
blue
the inputted colourh s
are the unmodifiedh
ands
channels, there values have effectively come straight throughcalc(l * 1.25)
the modifiedl
channel where the value ofl
is being multiplied by1.25
Running example
Example 2: Changing opacity
Before relative colour syntax, to change the opacity of a colour, the rgb equivalent of a colour would have to be stored. A typical example, when using CSS variables, might be:
With CSS relative colour syntax the following is possible:
Cutting out the need for repetitive rgb colour space conversions of existing variables.
Read more
You can see a lot more examples at https://developer.chrome.com/blog/css-relative-color-syntax#adjust_opacity_a_color ☍