The background-blend-mode
CSS property defines how an element's background images and its background color should blend with each other. This powerful feature allows you to create a wide variety of visual effects, from simple color overlays to complex, artistic compositions, directly in the browser.
Example 1: Multiply Blend Mode
.multiply-effect {
/* Sets a background image */
background-image: url('https://via.placeholder.com/600x400');
/* Sets a solid background color */
background-color: #ff6347; /* Tomato */
/* Blends the image and color using the multiply mode */
background-blend-mode: multiply;
/* Ensures the background covers the entire element */
background-size: cover;
height: 400px;
}
Explanation: The multiply
blend mode multiplies the colors of the background image and the background color. This results in a darker image, as the lighter pixels of the image take on the hue of the background color.
Example 2: Screen Blend Mode
.screen-effect {
/* Specifies the background image */
background-image: url('https://via.placeholder.com/600x400');
/* Defines a solid background color */
background-color: #4682b4; /* SteelBlue */
/* Applies the screen blend mode */
background-blend-mode: screen;
/* Stretches the background to fill the element */
background-size: cover;
height: 400px;
}
Explanation: The screen
blend mode inverts the colors of both the image and the background color, multiplies them, and then inverts the result. This creates a lighter, brighter effect, the opposite of multiply
.
Example 3: Overlay Blend Mode
.overlay-effect {
/* The image for the background */
background-image: url('https://via.placeholder.com/600x400');
/* The color to blend with the image */
background-color: #32cd32; /* LimeGreen */
/* Uses the overlay blend mode for a combined effect */
background-blend-mode: overlay;
/* Makes sure the background image covers the element */
background-size: cover;
height: 400px;
}
Explanation: The overlay
blend mode combines the multiply
and screen
modes. Darker parts of the image become darker, and lighter parts become lighter, which increases the contrast of the final image.
Example 4: Difference Blend Mode
.difference-effect {
/* Setting the background image */
background-image: url('https://via.placeholder.com/600x400');
/* Setting the background color to blend */
background-color: #ffff00; /* Yellow */
/* Applying the difference blend mode */
background-blend-mode: difference;
/* Resizing the background to cover the element */
background-size: cover;
height: 400px;
}
Explanation: The difference
blend mode subtracts the darker of the two colors from the lighter color. This can create a psychedelic or inverted color effect, depending on the colors used.
Example 5: Luminosity Blend Mode
.luminosity-effect {
/* Define the background image */
background-image: url('https://via.placeholder.com/600x400');
/* Define the background color for blending */
background-color: #ee82ee; /* Violet */
/* Use the luminosity blend mode */
background-blend-mode: luminosity;
/* Ensure the background image fits the element */
background-size: cover;
height: 400px;
}
Explanation: The luminosity
blend mode preserves the luminosity of the top layer (the image) while adopting the hue and saturation of the bottom layer (the color). This is great for creating monochromatic images with a color tint.
Example 6: Gradient Blend
.gradient-blend-effect {
/* A linear gradient as the background image */
background-image: linear-gradient(to right, #ff0000, #0000ff); /* Red to Blue */
/* A solid color to blend with the gradient */
background-color: #00ff00; /* Green */
/* Blending the gradient and the solid color */
background-blend-mode: lighten;
/* Defining the height of the element */
height: 400px;
}
Explanation: The lighten
blend mode compares the color information for each pixel of the gradient and the background color and selects the lighter of the two. This results in a blended background with the brightest color values from both layers.
Example 7: Multiple Backgrounds and Blend Modes
.multiple-blend-effect {
/* Two background images are used */
background-image: url('https://via.placeholder.com/300x200'), url('https://via.placeholder.com/300x200/0000FF');
/* A solid background color */
background-color: #ff1493; /* DeepPink */
/* Two corresponding blend modes are applied */
background-blend-mode: screen, multiply;
/* Positioning the background images */
background-position: left, right;
background-repeat: no-repeat;
height: 400px;
}
Explanation: You can apply different blend modes to multiple background images. In this example, the first image is blended with the background color using screen
, and the second image is blended using multiply
.