We’ve had color functions in preprocessors like Sass for a while now. This ability to adjust and modify colors should be coming soon to CSS with the CSS Color Module Level 4. We’ll be able to apply a number of color adjusters to base colors. This becomes very handy when coupled with CSS variables, because it’ll be possible to have our base colors defined as variables, and then apply adjusters where needed.
Note that color-mod was previously known as just the color function. The name was recently changed in the spec to color-mod.
Here’s an example of how the color-mod function is used:
.box {
// Let's make it a little redder
color: color-mod(rgb(147,123,25) red(218));
}
Or with an HEX value as the base color:
.box {
color: color-mod(#937b19 contrast(25%);
}
Or even with computed properties (CSS variables):
:root {
--base-color: #937b19;
}
.box {
color: color-mod(var(--base-color) tint(59%));
}
The resulting color from the above snippets will be rgb(218, 123, 25).
You can use multiple color adjuster in the same color function:
.box {
color: color-mod(purple lightness(62%) red(218) blue(202) whiteness(25%));
}
Here’s a list of available color adjusters:
.box {
color: color-mod(hotpink blend(yellow 59%));
}
CSS Colors Level 4 is still at the Working Draft stage in the recommendation process, and the color function is not implemented in any browser yet. The good news though is that you can start using it today, thanks to PostCSS and the cssnext plugin.
👉 Checkout ColorMe.io, a great tool to help you compose colors with the color function.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!