The mix-blend-mode
CSS property dictates how an element's content should blend with the content of the element's direct parent and the element's background. This powerful tool allows for the creation of visually rich effects, such as image overlays and dynamic text treatments, by combining colors in various ways.
Example 1: normal
/* In this example, the h1 element has a red background color and is blended with a div element that has a blue background color. The mix-blend-mode is set to normal, which is the default value. This means that the top layer (the h1) will simply be placed on top of the bottom layer (the div), without any color blending. */
.parent-element {
background-color: blue;
width: 300px;
height: 300px;
}
.child-element-1 {
background-color: red;
mix-blend-mode: normal; /* Default value, no blending */
color: white;
text-align: center;
padding: 50px;
}
Explanation
The normal
value is the default setting for mix-blend-mode
. It renders the element without any blending, meaning the top element's content will obscure the content of the elements beneath it. This is the standard behavior for elements on a webpage.
Example 2: multiply
/* Here, the multiply value is used. This blend mode multiplies the colors of the top and bottom layers. The resulting color is always darker. White color in either layer will result in the other layer's color, while black will result in black. */
.parent-element {
background-image: url('https://via.placeholder.com/300');
}
.child-element-2 {
background-color: #ff6347; /* A shade of tomato */
mix-blend-mode: multiply;
height: 300px;
width: 300px;
}
Explanation
The multiply
blend mode multiplies the luminance of the base color by the blend color. The resulting color is always as dark as or darker than the original. This mode is effective for creating shadow effects or for coloring grayscale images.
Example 3: screen
/* The screen value inverts the colors, multiplies them, and then inverts the result. This leads to a brighter outcome. It's the opposite of multiply and is great for creating highlights or glow effects. */
.parent-element {
background-image: url('https://via.placeholder.com/300/0000FF/808080');
background-size: cover;
}
.child-element-3 {
background-color: #00ffff; /* Cyan */
mix-blend-mode: screen;
height: 300px;
width: 300px;
}
Explanation
The screen
blend mode inverts both layers' colors, multiplies them, and then inverts the result. This produces a lighter color and is useful for creating glowing effects or for making dark images brighter. Black in either layer will not change the other layer's color.
Example 4: overlay
/* Overlay combines the multiply and screen blend modes. If the bottom layer is dark, the top layer is multiplied. If the bottom layer is light, the top layer is screened. This creates a result that preserves the highlights and shadows of the bottom layer. */
.parent-element {
background-image: url('https://via.placeholder.com/300');
}
.child-element-4 {
background: linear-gradient(to right, #ff0000, #0000ff); /* Red to blue gradient */
mix-blend-mode: overlay;
height: 300px;
width: 300px;
}
Explanation
The overlay
blend mode is a combination of multiply
and screen
. It darkens dark areas and lightens light areas, increasing the contrast of the underlying layer. It is commonly used to superimpose patterns or textures onto images.
Example 5: darken
/* The darken blend mode compares the color of the top and bottom layers and selects the darker of the two. It does this for each of the red, green, and blue channels separately. */
.parent-element {
background-color: #ffff00; /* Yellow */
padding: 50px;
}
.child-element-5 h2 {
color: #ff00ff; /* Magenta */
mix-blend-mode: darken;
font-size: 3rem;
text-align: center;
}
Explanation
The darken
blend mode compares the color channels of the blend and base layers and selects the darker of the two. It does not produce new colors, only selects from the existing ones. This is useful for placing dark text over a multi-colored background.
Example 6: lighten
/* Lighten is the opposite of darken. It compares the colors of the top and bottom layers and selects the lighter of the two. This is also done on a per-channel basis. */
.parent-element {
background-image: url('https://via.placeholder.com/300/4B0082/FFFFFF');
background-size: cover;
}
.child-element-6 {
background-color: #ffa500; /* Orange */
mix-blend-mode: lighten;
height: 300px;
width: 300px;
}
Explanation
The lighten
blend mode compares the color channels of the blend and base layers and selects the lighter of the two. Similar to darken
, it does not create new colors. This is effective for placing light-colored elements over various backgrounds.
Example 7: difference
/* The difference blend mode subtracts the darker color from the lighter color. The result is often a stark and inverted color effect. White will invert the color of the other layer, while black will have no effect. */
.parent-element {
background-color: #00ff00; /* Green */
}
.child-element-7 {
background-color: #ff0000; /* Red */
mix-blend-mode: difference;
height: 300px;
width: 300px;
}
Explanation
The difference
blend mode subtracts the darker of the two colors from the lighter one. This is useful for creating high-contrast, artistic effects. It can be used to see the variations between two images.