...

Creating color palettes with the CSS color-mix() function | MDN Blog

Creating color palettes with the CSS color-mix() function | MDN Blog

The color-mix() function allows us to specify the two colors we want to mix and then outputs the result. We can control the amount of each color in the mix, as well as the color interpolation space, which determines how the colors blend together.

A graphic showing the components of a color-mix() function, namely an interpolation method and two colors along with their percentages

The color interpolation method is a required parameter. We’ll cover it in a later section. For now, we’ll use srgb to walk through the examples.

We specify the amount of each color as percentages. If we omit the percentages of both colors, color-mix() will use 50% for each by default. As shown below, mixing red and blue in equal parts gives us a purple hue as expected.

.result { background-color: color-mix(in srgb, blue, red); } 

If we specify the percentage for only one color, the percentage for the other color is automatically adjusted so that the total adds up to 100%. For example, whether we specify 90% for blue or 10% for red, the result is the same — a color that’s mostly blue with a hint of red.

 color-mix(in srgb, blue 90%, red) color-mix(in srgb, blue, red 10%) 

If the sum of the percentages for the two colors is less than 100%, the color-mix() behavior is slightly different: the sum is saved as an alpha multiplier, and the two colors are scaled using this multiplier so that their total reaches 100%. (See the Percentage Normalization section in the specification for a few examples).

Although both the color-mix() functions below mix the same amount of each color, the second function, where the sum of the percentages is 40%, produces the same color but with an alpha value of 0.4:

 color-mix(in srgb, blue, red) color-mix(in srgb, blue 20%, red 20%) 

Discover more from WIREDGORILLA

Subscribe now to keep reading and get access to the full archive.

Continue reading