Day 92: relative color syntax

posted on

It’s time to get me up to speed with modern CSS. There’s so much new in CSS that I know too little about. To change that I’ve started #100DaysOfMoreOrLessModernCSS. Why more or less modern CSS? Because some topics will be about cutting-edge features, while other stuff has been around for quite a while already, but I just have little to no experience with it.


With the relative color syntax we can modify existing colors using color functions. If an origin color is specified, each color channel can either be directly specified, or taken from the origin color and modified.

For example, we can take a HEX color and add opacity using the rgb() color function and the from keyword.

div {
  --color: #FF0000;
}

div {
  background-color: rgb(from var(--color) r g b / 50%);
}
rgb(255 0 0 / 50%)

Or we can take the color and replace a specific channel.

div {
  background-color: rgb(from var(--color) r g 150);
}
rgb(255 0 0 / 50%)

We can even use the calc() function.

div {
  background-color: hsl(from var(--color) h s calc(l - 10%));
}
hsl(0deg 100% 40%)

We can use channel keywords in their corresponding argument, but we don't have to. We can use them in any position.

div {
 background-color: rgb(from var(--color)
                    calc(r * .3 + g * .59 + b * .11)
                    calc(r * .3 + g * .59 + b * .11)
                    calc(r * .3 + g * .59 + b * .11));
}
rgb(30% 30% 30%)

How cool is that!? Is there a catch? Well, yeah, it's currently only supported in Safari behind a flag.

See on CodePen

Further reading

Do you want to learn even more awesome CSS?

I'm running a workshop with my friends at Smashing Magazine in which I introduce you to the most useful modern features in CSS and show how you can implement them today in your code base to improve scalability, maintainability, and productivity.

Learn more about the workshop!

Overview: 100 Days Of More Or Less Modern CSS