CSS Blend modes can be used on modern browsers to achieve a variety of pretty cool effects.
In this article we'll see how to use the mix-blend-mode and background-blend-mode properties, and see some examples of these blend modes in action.
You are free to copy, modify and use this code as you wish.
There are 2 CSS properties that have to do with blending:
Both properties are not inheritable.
An interesting thing is that the alpha channel does not blend like the other components, instead it can be used to weaken/strengthen the effect, and as transparency, depending which blending mode you use.
The effect, of course, is applied after other image filters (like an SVG blur or a sepia effect).
mix-blend-mode specifies how the contents of an elements will blend with the elements behind them and the background.
Let's take a look at all the currently available blending modes.
We'll be using 3 overlapping elements with different colors:
Elements blend normally, also known as alpha blending. This is the default blending.
The RGB components are added together. Also known as additive rendering. Useful for creating nebulae and similar effects.
The RGB components are multiplied. Useful for colorizing black and white images.
A subtraction is performed between the elements' RGB components. The result, of course, is different depending on the z-index.
When element B is drawn in front of element A, the resulting color has B's hue and A's saturation and lightness.
When element B is drawn in front of element A, the resulting color has B's saturation and A's hue and lightness.
When element B is drawn in front of element A, the resulting color has B's lightness and A's saturation and hue.
When element B is drawn in front of element A, the resulting color has B's hue and saturation and A's lightness. Useful for colorizing any image.
A XOR is performed between the RGB components.
When element B is drawn in front of element A, the resulting color has the highest value for each RGB component.
When element B is drawn in front of element A, the resulting color has the lowest value for each RGB component.
A mix between screen and multiply. Can be seen as a linear interpolation between black, B's value, and white, using A's lightness as interpolation factor.
The darker is A, the more its color is used. Blending with white produces no difference.
This darkens the image.
The brighter is B, the more its color affects A. Blending with white gives white. Blending with black produces no difference.
This lightens the image.
Same as overlay, but with A and B swapped.
This is a variation of hard-light. The implementation of this is not well defined so it might look different in different rendering engines.
More about this blend mode can be found on Wikipedia.
background-blend-mode specifies how multiple backgrounds in this element will blend with each other (such as a background-color and a background-image).
Let's take a look at all the currently available blending modes.
We'll be using an element with these 2 background images:
Backgrounds blend normally, also known as alpha blending. This is the default blending.
The RGB components are added together. Also known as additive rendering. Useful for creating nebulae and similar effects.
The RGB components are multiplied. Useful for colorizing black and white images.
A subtraction is performed between the elements' RGB components. The result, of course, is different depending on the order in which the images are drawn.
When element B is drawn in front of element A, the resulting color has B's hue and A's saturation and lightness.
When element B is drawn in front of element A, the resulting color has B's saturation and A's hue and lightness.
When element B is drawn in front of element A, the resulting color has B's lightness and A's saturation and hue.
When element B is drawn in front of element A, the resulting color has B's hue and saturation and A's lightness. Useful for colorizing any image.
A XOR is performed between the RGB components.
When element B is drawn in front of element A, the resulting color has the highest value for each RGB component.
When element B is drawn in front of element A, the resulting color has the lowest value for each RGB component.
A mix between screen and multiply. Can be seen as a linear interpolation between black, B's value, and white, using A's lightness as interpolation factor.
The darker is A, the more its color is used. Blending with white produces no difference.
This darkens the image.
The brighter is B, the more its color affects A. Blending with white gives white. Blending with black produces no difference.
This lightens the image.
Same as overlay, but with A and B swapped.
This is a variation of hard-light. The implementation of this is not well defined so it might look different in different rendering engines.
More about this blend mode can be found on Wikipedia.
We can combine the 2 properties we've seen and add some animations to procedurally generate a nebula with a few lines of code.
As of June 2016, CSS blend modes are not a standard yet. They are fully supported by Firefox (and other gecko-based browsers), fully supported by Chrome (and other webkit-based browsers), slightly broken in Safari, and unsupported by Microsoft Edge/IE.
See updated list of supported browsers.
Some of these effects can be done with SVG as well, so if it's relevant to your site, you might want to use it as a fallback.
To detect browser support, you can use this code: