Tutorial

The CSS color-mod Function

Published on January 12, 2017
Default avatar

By Alligator.io

The CSS color-mod Function

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.

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%));
}

Color Adjusters

Here’s a list of available color adjusters:

  • alpha: A value for the alpha-transparency between 0% and 100%.
  • red, green & blue: A value between 0 and 255. Given a starting color of rgb(140, 254, 255), the starting red value would be 140, so anything higher than 140 increases the amount of red in the color and anything lower than 140 decreases the amount of red in color. Green and blue work the same way, with affecting their respective color.
  • blackness & whiteness: A value between 0% and 100%.
  • contrast: A value between 0% and 100%.
  • saturation: A value between 0% and 100%. 0% is gray.
  • lightness: A value between 0% and 100%. 0% is black, and 100% is white.
  • tint: A value between 0% and 100%.
  • hue: A value between 0 and 360.
  • shade: A value between 0% and 100%. 100% is black.
  • blend: Blend makes it easy to blend a color with another color. Here’s an example if it’s usage:
.box {
  color: color-mod(hotpink blend(yellow 59%));
}

Browser Support

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.

Learn more about us


About the authors
Default avatar
Alligator.io

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


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!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel