CSS

How to Use CSS Blend Modes and SVG8 min read

CSS blend modes are an easy way to add image effects right in the browser. Because of this, the traditional way of editing and then saving photos from an image editing software solution is not always necessary. As designers, we’ve spent countless hours adding effects to images with tools like Adobe Photoshop. As you go through this tutorial, you will notice that a lot of the options resemble those in Photoshop, but they can now be done with the efficiency of CSS styling.

Will browser blend modes completely eliminate the need for image editing software? Not entirely, and certainly not quite yet. But CSS and browser support has come a long way in supporting new ways of editing images. CSS and SVG clipping and masking, blend modes, 3D Transform, and more can certainly cut back on the dependency that we have for image editing software. As browsers become more sophisticated, we will see a lot more potential and will (hopefully) spend less time in Photoshop.

CSS blend modes and CSS mix blend modes




This tutorial will cover CSS blend modes and how they are used. To start, there are a couple different options you should be aware of. One is an effect with background-blend-mode and the other is with mix-blend-mode.

Using the background-blend-mode property, you can blend the background layers (image or color) of an element. Blend modes are defined as a value and they specify how to blend or mix the colors of the background image with the color, or other background images,  behind it.

What happens if you want to do some blending but not with background elements? Elements can be blended together using the the mix-blend-mode property. This property describes how the blending should be between stacked HTML elements. Elements on overlapping layers will blend with those beneath it. Any images, text, borders, or headings will be influenced by this property.

Adobe Photoshop multiply example

Taking a look at the traditional way of showing blend modes in an image editor gives us an idea of what we can accomplish with CSS blend modes. The following photo was created in Adobe Photoshop. The image is on its own background layer with a red layer above. A blend mode for the red layer was selected, which is “Multiply.” As you can see, there is a red overlay. To achieve this effect, Adobe Photoshop takes the colors from the layer that has “Multiply” applied, multiplies by the colors on the layer(s) below it, then divides them by 255 to show the result.

This same effect can be achieved with CSS allowing for faster customization and easy updating.

Basic example of a CSS blend mode

A simple example to see how blend modes work is blending the image with a with a background-color. First, the URL path to the image needs to be declared, then a color specified. After those have been decided, the blend mode needs to be chosen. Multiply has been selected here to show how this blend mode affects the appearance of the background-image.

A blend mode is the way the final color value of a pixel is calculated when layers overlap. Each blend mode takes the color value of the foreground and the backdrop (top color and bottom color), calculates its value, and returns a color value. The final, visible layer is the result of the blend mode calculation on every overlapping pixel between the blended layers.

Multiply is a very popular blend mode, but there are also other available blend mode options: screen, overlay, darken, lighten, color-dodge, color-burn, hard-light, soft-light, difference, exclusion, hue, saturation, color, and luminosity. If “normal” is specified, this will reset things. Instead of going through the details of each blend mode one by one, experimenting with them is the best way to determine what the final outcome will be.

Background blend mode with two images

Rather than having a color overlay on an image, layering two images together can have a pretty cool effect. It’s as easy as adding two background images in the CSS declaration. The next choice is to have a background color (or not). If you do not want a background color, this can be removed and the images will blend together depending on the blend mode that you choose.

Gradient on background-blend-modes

Rather than using just a single color, gradients can also give some unique effects.

You’ll also notice that this example has multiple background blend modes. If one blend mode isn’t enough, multiple can be used.

Mix blend mode examples

Up until this point, the focus has been on the background. What if other elements on the page want to take advantage of blend modes? This is no problem and the same blend mode types are available.

Things look the same from background-blend-modes with the exception of initial, inherit, and unset.

  • Initial: Default setting of the property that does not set a blend mode.
  • Inherit: Inherits the blend mode from its parent element.
  • Unset: Removes the blend mode from an element.

Basic example of a mixed blend mode with isolation

When working with mix-blend-mode, you’ll come across the need to do some isolating. First, it’s important to know that “stacking” is possible, and this is valuable when working with a lot of layers. Think of a stack of boxes. Each box is separate from the group. Inside of each box, there can be multiple layers of items. This way of thinking will help when determining what needs to be isolated.

Text and image mix with mix-blend-mode

In this example, the div with a class of img-wrap contains the image. The image has a mix-blend-mode of multiply. So basically, the image appears to fall into the background.

To prevent this, the img-wrap div (also containing the heading text) should be a new set of stacked content so it is set apart from the background of the body element. This is done with the isolation property. The default value is auto, so isolation: isolate; will need to be added.

To test this out, comment out the isolation property on the div with the class of .img-wrap and check the result.

Here is the HTML:

Here is the CSS. Pay close attention to the isolate on the .img-wrap.

Text cut out with mix-blend-mode

Some interesting type effects can be created with mix blend modes. There’s an easy way to make cut out text. The background is hidden by a fill on the h1 element.

Here is the HTML:

That containing <div> is filled with the forest background image.

The heading inside it is styled with an optional background color. The heading has a see-through effect with the semi see-through background image by using mix-blend-mode of multiply:

Mix-blend-mode and SVG

SVG files are very popular on the web, and CSS blending modes also work well with them. Shapes can be easily targeted to give them the desired blend mode.

Isolate was key in this example as well. Without isolating the circles, the gray background would interfere.

Here is the code to create the circle group:

Here are the CSS styles:

Leave a Comment