DEV Community

Cover image for A Practical Guide to Creating Stunning Visual Effects with CSS Blend-Modes
DrPrime01
DrPrime01

Posted on

A Practical Guide to Creating Stunning Visual Effects with CSS Blend-Modes

Outline

  • Introduction
  • Understanding Blend Modes Primary Colour Properties
  • Blend modes — Mix-Blend-Mode and Background-Blend-Mode
  • Practical Applications of Blend Modes
  • Tips for Using Blend Modes Effectively
  • Conclusion

Blend modes are CSS data type that describes the resultant colour of two or more HTML elements after overlapping. With the CSS filter property, blend modes allow developers to create rich, dynamic Photoshop-like visual effects with only a few lines of CSS. They provide you, as a developer, with a powerful way to enhance the look and feel of a website design and make your website outstanding and professional.
CSS equips developers with two blend-mode properties: mix-blend-mode and background-blend-mode. The mix-blend-mode property defines how an element’s content colour should blend with other contents of the element’s parent. The background-blend-mode does the same, except its focus is on mixing the element’s colour with its background.
CSS offers developers 16 keywords and five global values for the blend mode properties, each with unique visual effects on the elements you apply. This guide will help you understand blend modes in-depth, explain each keyword and global value, their practical applications, and tips for using them effectively.

Understanding Blend Modes

Blend modes are used to adjust the luminosity, contrast, saturation, and hue of an element’s content and background to neighbouring elements’ content and backgrounds. For an in-depth understanding of blend modes, you need to approach the 4 primary terms you will come across in almost all definitions of each blend mode property from the lens of graphic design.

  • Luminosity: It’s the intensity of light emitted by each pixel of a screen. For the eyes to perceive the colour of an object, the object needs to be exposed to light. Upon exposure to light, the object absorbs some of the light and reflects the rest. The intensity of the light reflected is a measure of the object’s luminosity. Your eyes perceive that reflected light intensity as how bright the object’s colour is, therefore, luminosity is basically how bright your eyes perceive an object’s colour. Luminosity is not only defined by how bright your eyes perceive an object’s colour; it’s also influenced by 2 other properties: saturation and hue.
  • Saturation: This design property goes from 0 - 100% and defines how pure colour appears. Designers tone colours down with the addition of white and black colours (grey), in addition to some other changes, to achieve the specific colour of the image they are trying to recreate. If all “other changes” applied to a colour are removed, and other colour properties remain constant, the amount of grey in the colour modified defines how unsaturated it is. However, in the total absence of grey in a colour, the saturation per cent of a colour stands at 100, meaning, the colour is absolutely pure. As any saturation per cent less than 100 indicates that a particular colour is toned, the luminosity of the colour is also altered due to the impurities in the colour (the grey colour).
  • Hue: Briefly, it’s the purest representation of a colour. Don’t confuse it with the saturation property. Any addition of grey to a pure colour reduces its saturation per cent from 100. The pure form of a colour before it gets saturated is “Hue”. While other colour properties remain constant, hue is a colour without a tint (addition of any degree of white) or a shade (addition of any degree of black). In the same manner that tampering with the saturation of a colour messes up its luminosity, tampering with the hue degree has the same effect.
  • Contrast: In this context, it’s the difference between two or more hues. It gives each colour a distinctive property, making people able to distinguish the colours easily.

Now that the primary colour properties have been properly dissected, it’s time to learn how they relate to each blend mode property and give it a distinct effect.

Blend modes — Mix-Blend-Mode and Background-Blend-Mode

CSS blend mode properties are particularly useful for controlling the blending behaviour of transparent and translucent elements with the underlying content. Additionally, they can also be applied to opaque elements to produce usually appealing effects. The blend modes enable web developers to manipulate the way colours are combined by adjusting the brightness, mixing 2 or more colours, increasing the contrast of 2 or more overlapping content, and revealing the difference between foreground and background colours of overlapping elements, resulting in various design effects.
The mix-blend-mode property defines how an element's content should mix with other content of the same parent and the background while the background-blend-mode defines how an element’s background image should blend with other background images in the same element and the background colour. The background-blend-mode property works for elements with at least one background image, while the mix-blend-mode property works for any element with or without a background image. Both properties share 16 keywords and 5 global values for defining the styles.

  • Normal: Normal is the default value. There is no visible change in an element with a blend mode property of normal. It’s just like putting an opaque piece of paper above another opaque paper; you’re only able to see the colour of the topmost paper.
  • Multiply: As its name implies, the resultant colour is the effect derived by multiplying the foreground and background colours. It is analogous to superimposing one translucent plastic bag on another. The lighter the topmost element’s colour, the more the base element’s colour is revealed.
  • Screen: The foreground and background colours are inverted, and the multiply effect is applied to the result, which is finally inverted again. For this effect, the darker the foreground colour, the more the background colour will show through.
  • Overlay: This effect portrays itself as either the multiply or screen effect based on the background colour. It embraces the multiply effect if the background colour is darker, and if it’s lighter, it gives off the screen effect.
  • Lighten: The resultant colour is equivalent to the lighter of both coloured elements.
  • Darken: The resultant colour is equivalent to the darker of both coloured elements.
  • Color-dodge: Lightens the foreground colour (or background image) to reflect the background colour.
  • Color-burn: The inverse of color-dodge. It reflects a highly contrasted background colour in the foreground colour (or background image).
  • Hard-light: It is similar to overlay but with swapped layers. It can give a multiply or screen effect, but unlike overlay, it depends on the foreground colour. If the foreground colour is darker, it yields the multiply effect, and if it’s lighter, it yields the screen effect.
  • Soft-light: Similar to hard-light but a softer version of it. It uses color-burn/color-dodge instead of multiply/screen.
  • Difference: The resultant effect is derived by subtracting the darker-coloured element from the lighter one.
  • Exclusion: Similar to difference but with a softer contrast.
  • Hue: It’s composed of the foreground colour’s hue and the background colour’s saturation and luminosity.
  • Saturation: Similar to hue but with reversed roles. The foreground colour’s saturation and the background colour’s hue and luminosity are mixed to give the final effect.
  • Luminosity: Also similar to hue but with reversed roles. The foreground colour’s luminosity and the background colour’s hue and saturation are mixed to give the final effect.
  • Color: For this effect, the foreground colour’s hue and saturation and the background colour’s luminosity are mixed to give the final effect.

Practical Applications for Blend Modes

Creating Dynamic Image Overlays

Blend modes can be used to create captivating image overlays, adding depth and interest to your web designs. Here's an example of how to create a colour overlay on an image:

.image-container {
      position: relative;
      width: 100%;
      height: 400px;
    }

    .image {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    .overlay {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 255, 0.5); /* Blue overlay */
      mix-blend-mode: multiply;
    }
Enter fullscreen mode Exit fullscreen mode

This CSS will create a blue overlay on top of an image, giving it a moody, cinematic effect.

Enhancing Typography

Blend modes can make text stand out against complex backgrounds. Here's how you can use blend modes to ensure text readability:

.text-overlay {
      color: white;
      mix-blend-mode: difference;
    }
Enter fullscreen mode Exit fullscreen mode

This will make the text visible against both light and dark backgrounds by inverting its colour based on the background.

Designing Striking Backgrounds

Create unique background effects by blending multiple background images:

    .blended-background {
      background-image: 
        url('texture.jpg'),
        linear-gradient(to right, #ff0000, #00ff00);
      background-blend-mode: overlay;
    }
Enter fullscreen mode Exit fullscreen mode

This combines a texture image with a gradient, creating a rich, textured background.

Artistic Image Manipulation

Use blend modes to apply artistic effects to images without the need for image editing software:

   .artistic-image {
      background-image: url('portrait.jpg');
      background-color: #ff00ff; /* Magenta */
      background-blend-mode: color;
    }
Enter fullscreen mode Exit fullscreen mode

This applies a colourization effect to the image, similar to duotone effects in print design.

Tips for Using Blend Modes Effectively

  1. Start Simple: Begin with basic blend modes like multiply or screen before moving on to more complex ones.
  2. Consider Accessibility: Ensure that text remains readable when using blend modes. Always test your designs with different colour blindness simulations.
  3. Test Across Browsers: Not all browsers support all blend modes equally. Always test your designs in multiple browsers and have fallbacks ready.
  4. Be Mindful of Performance: Overusing blend modes, especially on large images or backgrounds, can impact page performance. Use them judiciously.
  5. Combine with Other CSS Properties: Blend modes work well with opacity, filters, and animations to create even more interesting effects.
  6. Use with SVGs: Blend modes can create fascinating effects when applied to SVG elements.
  7. Experiment: Don't be afraid to try unexpected combinations. Sometimes the most interesting effects come from experimentation.
  8. Consider Dark Mode: If your site has a dark mode, remember that blend modes might behave differently on dark backgrounds.
  9. Document Your Work: When using complex blend mode combinations, leave comments in your CSS to explain the intended effect.
  10. Use Dev Tools: Browser dev tools are invaluable for tweaking real-time blend modes. Use them to fine-tune your effects.

Browser Compatibility

While most modern browsers support blend modes, there are still some considerations:

  • Internet Explorer does not support blend modes at all.
  • Some older versions of browsers may have limited support.
  • Mobile browsers generally have good support but always test on actual devices.

For up-to-date compatibility information, check Can I Use.

Performance Considerations

Blend modes are generally performant, but can cause issues if overused:

  • Applying blend modes to large areas or many elements can slow down rendering.
  • Animated blend modes can be particularly resource-intensive.
  • Consider using will-change: background-blend-mode; or will-change: mix-blend-mode; for frequently changing blend modes, but use sparingly as it can consume memory.

Conclusion

CSS blend modes offer web developers and designers a powerful tool to create stunning visual effects with minimal effort. By understanding the principles behind blend modes and how they interact with colour properties like luminosity, saturation, and hue, you can create designs previously only possible with image editing software.
From enhancing typography and creating dynamic image overlays to designing striking backgrounds and manipulating images artistically, blend modes open up a world of creative possibilities. Remember to use them judiciously, always keeping in mind accessibility, performance, and cross-browser compatibility.
As web design continues to evolve, mastering blend modes will give you an edge in creating unique, eye-catching designs that stand out in the digital landscape. Don't be afraid to experiment and push the boundaries of what's possible with CSS – you might discover exciting new techniques that elevate your web design to the next level.
Lastly, here's a suggested "Resources" section to round out the article:

Resources for Further Learning

To deepen your understanding and skills with CSS blend modes, check out these valuable resources:

  1. MDN Web Docs on Blend Modes - Comprehensive documentation on all blend mode properties.
  2. CSS Tricks - CSS Blend Modes - A practical guide with examples and explanations.
  3. Codrops CSS Reference - Blend Modes - Interactive examples of different blend modes.
  4. Can I Use - CSS Blend Modes - Up-to-date browser compatibility information.
  5. Adobe Color - A tool for creating colour schemes that work well with blend modes.
  6. Blend - CSS Playground - An interactive tool to experiment with different blend modes.
  7. CSS Blend Mode Generator - A CodePen demo to generate blend mode effects.

These resources will help you explore the full potential of CSS blend modes and inspire you to create unique visual effects in your web projects.

Cover photo by Hitesh Choudhary

Top comments (2)

Collapse
 
ritik1009 profile image
ritik1009

Can you provide some example of the blend mode in your post? so the reader can get some idea what it is about? and how does it looks? before theory there should be some demo.

Collapse
 
drprime01 profile image
DrPrime01

Sure. I'll add an update.