Image editing has been easier than ever since so many apps support blending two images gather. Mixing two images is cool But once the image is saved, you no longer can modify it. This means, Once you get your website assets designed by a designer, you're left with an image you can't modify. CSS can help to display your web assets with blending modes.
Blending modes in CSS opens up various design possibilities within the browser so you're not limited to what image alone represents. Blending or Mixing is combining two elements or two backgrounds to compose a final image. How the final outcome will display depends on the modes used. CSS currently has two properties that enable the blending mode.
- mix-blend-mode: This property enables blending two DOM elements together.
- background-blend-mode: This property will only blend background properties.
CSS offers 15 different blending modes apart from the default, I have grouped them into 5 groups depending on their functionality. Let's understand all of them with working examples.
Let's consider an example with two layers. The first layer is a base layer and on top of that, we'll add a blending layer. The result will be a blended version of both images.
Blending Modes in CSS
Blend mode Syntax:
/* or */
- <value> : normal | multiply | screen | overlay | darken | lighten | color-dodge | color-burn | hard-light | soft-light | difference | exclusion | hue | saturation | color | luminosity
Let's go through all the groups in detail. I have created demos that with images and various overlays. Click on circles to try and compare them in real time.
Darken (Multiply) :
This blend mode mostly multiplies the pixels of the layers. This group contains three values as follows:
- Darken: It blends pixels based on luminance values in each color, the result is either the base color or blend color depending on which is darker
- Multiply: It blends the luminosity of the base color by the blend color. The result is always a darker color.
- Color Burn: It gives darker colors than darken mode because it increases the contrast between the base and blending layer.
When you multiply a big number with another number it gets bigger, In terms of colors, the dark color becomes darker. This group makes an image darker.
Hover over Normal Button on the below demo and observe the black and grey colors shades. Now switch the modes by hovering on other buttons. Noticed the dark area becomes darker? This blending works great with shades of grey. All 3 values - darken, multiply, color burn uses a different algorithm to blend pixels. But they all tend to make images darker as a common behavior.
When It's useful?
When you have lots of dark color in the image and you want only a blending layer to only affect that dark arena you can use it.
- A card with a caption with background image with color overlay
- Make an image darker
- Blend the dark color with the image
- Remove white background on from logos
This blend mode basically divides the values. So small value becomes smaller making it much lighter. This group contains three values.
- Lighten: It mixes the Lighter color between the base and the blending layer.
- Screen: It blends lighter colors of both layers but this one has a greater blending range than lighten depending on the luminosity of the image.
- Color Doge: It works as lighten but it decreases the contrast between the base and blending layers making it much brighter.
All the blending mode in this group enhances the white color. So lighter colors of the blending layers are blended with the base layer.
Observer the White color and grey spectrum in the example. Now hover over the different effects to see them in action. You'll notice the white colors affecting the layer beneath. This is the default behavior of the values in this group. They all make white brighter.
When It's useful?
When you want to make an image bright so that black text is visible on the image, you can use this blending mode. This way you can get washed version of the image.
- A card with a dark caption with background image with color overlay
- Make Image Lighter
- Change the color of the image
Probably the most used blending mode in CSS. This group has three values and it performs blending bases on the contrast of the blending layer.
- Overlay: It blends pixels using mid-tones. Its blending is done at half strength so both lighter and darker colors shift towards mids of the color range.
- Soft Light: a version of the overlay which blends layers depending on the luminance value of the base layer.
- Hard Light: A version of overlay that blends layers depending on the brightness of the base layer.
This group blends colors softly so it's used without opacity. Other blending modes are harsh and feel overdone. Blending mode in this group may work just fine out of the box.
Observe all the colors and shades of grey. Switch the blending mode by hovering on the other buttons. You'll notice these modes blend colors perfectly as you expect them to do. Unlike Darken and Lighten modes, This group works well with colors overlay being the default and soft light and hard light adding variation to the color blending.
When It's useful?
You can use overlay mode any time when you want to overlay an image or color or gradient softly on the base layer.
- To add a color theme or color overlay to images
- Add color overlay
- Softly change colors of base layer
Inversion (Comparative) :
This mode inverts the pixel on the base layer. This simply means The colors will jump on the opposite colors. Red will become Cyan, Green will become Magenta and Blues will become Yellow. There are two possible values in the group:
- Difference: Produces result by finding the difference between pixels of blending layer and the base layer.
- Exclusion: Blends white color values by inverting the colors of the base layer.
Modes in this group invert colors depending on their brightness values. So Black color is mostly unaffected and White will 100% invert the colors.
Observer the Black Color, White Color, and other colors in the example. Hover the mouse to change the blending mode. Spoiler: They look horrific. These blending modes are rarely used on the web.
When It's useful?
This can be used to create a negative effect. Although it's not used often on the web, one can create unique effects using these blend modes.
- When used with opacity it can create a custom effect
- To Invert the Image
- Change monochromatic icon colors on Hover
This group has three values and each blends pixels differently based on the Hue, Saturation, and Luminosity. These modes blends layers as per their names:
- Hue: It preserves the luminosity and saturation of the base layer and blends colors by their lightness or darkness value.
- Saturation: It preserves the luminosity and hue of the base layer and blends colors based on the color strength value.
- Color: preserves the luminosity of the base layer, It will just overlay the color on top of the base layer removing the base layer's colors.
- Luminosity: Luminosity keeps the hue and saturation of the base layers but blends the luminosity of the blending layer.
This group works on the HSL model so Blacks, Whites, and Greys have no greater effect on the blending.
Observer the colors on the below example, and switch the mode to see the difference between each of them.
When It's useful?
Since these modes blends layers with different values, These blend modes have too specific jobs.
- To create a greyscale or sepia image
- To desaturate the image with a 50% grey color
- Create custom monocolored effects
The other blending mode is the default one that doesn't do anything. But's it's worth mentioning - normal, inherit, initial, revert and unset. This mode exists but does nothing so I have not included it in any of the groups.
Since blending mode is rendered by browsers, You'd not see the same results across. If you're a perfectionist, you'd hate little dissimilarities of blending results across different browsers.
Browser support for background-blend-mode and mix-blend-mode
Most of the browser supports all 15 blending modes. But to make sure check caniuse before you try it on real projects. Some browsers don't support all the blending modes and you might know this already, that some browsers won't support this feature ever. So If you're not an IE fan, you're free to use it.
CSS Blending Modes and Performance
These blending calculations are computed within the browser, It affects a performance a little bit. We can update blending modes in real-time so it could slow down the rendering process sometimes.
Are CSS Blend Modes Worth Using?
There is too much going on under the blending modes in the browser. But having the ability to change images on the fly opens up a gateway to unlimited possibilities. Because You can blend more than one layer and create a new effect. I hope when you work on a new project, You'd just import a normal image from photoshop instead of a merged image. Because the browser can do blending for you. It's so much easier to replace the image and try blending modes within the browser to select the one that works.