Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
Blending allows us to merge an element with its background in different ways. There are 16 blend modes available in CSS:
There are two blending properties. These two properties are:
All 16 blend modes can be applied to either blending property.
Mix-blend-mode is used to blend an element with any elements that are behind it. The content behind the element can be any other elements.
Common blends are:
The example below shows the blending of an image with a colour (Run Example)
<!DOCTYPE html>
<html>
<head>
<title>Course notes example code</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
#container
{
width:600px;
height:600px;
background-color:blue;
}
img
{
width:100%;
height:100%;
mix-blend-mode:lighten;
}
</style>
</head>
<body>
<div id='container'>
<img src="person.jpg"/>
</div>
</body>
</html>
Write code to place an image on a red background with a 'darken' blend, as shown here.
The example below shows the blending of an image with another image (Run Example)
<!DOCTYPE html>
<html>
<head>
<title>Course notes example code</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
img
{
width:600px;
height:600px;
position:absolute;
}
.blend
{
mix-blend-mode:darken;
}
</style>
</head>
<body>
<img src ="city.jpg">
<img class = "blend" src="person.jpg"/>
</body>
</html>
Write code to place a person's face behind a rain-soaked window, as shown here.
Write code to allow a user to select between various images and various blend modes, as shown here.
The example below shows the blending of an image with text (Run Example)
<!DOCTYPE html>
<html>
<head>
<title>Course notes example code</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
p
{
margin-top:340px;
font-size:270px;
color:green;
}
img
{
width:600px;;
height:600px;
position:absolute;
top:0px;
left:0px;
}
.blend
{
mix-blend-mode:overlay;
}
</style>
</head>
<body>
<p>DkIT</p>
<img class = "blend" src="person.jpg" />
</body>
</html>
Write code to get an image on text blend to cause the image to be grayscale, as shown here.
Write code to get an image on text blend to cause the image and the text to be a negative, as shown here.
The example below shows the blending of text with an image (Run Example)
<!DOCTYPE html>
<html>
<head>
<title>Course notes example code</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
p
{
margin-top:340px;
font-size:270px;
color:red;
}
img
{
width:600px;;
height:600px;
position:absolute;
top:0px;
left:0px;
}
.blend
{
mix-blend-mode:color-dodge;
}
</style>
</head>
<body>
<img src="person.jpg" alt="Fresh Water" />
<p class='blend'>DkIT</p>
</body>
</html>
The example below shows the blending of text with text (Run Example)
<!DOCTYPE html> <html> <head> <title>Course notes example code</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> p { font-size:200px; letter-spacing:-20px; margin-top:0px; margin-bottom:-150px; mix-blend-mode:difference; } p:nth-child(1) { color:red; } p:nth-child(2) { color:green; } </style> </head> <body> <p>DkIT</p> <p>DkIT</p> </body> </html>
Write code to blend different coloured words together, as shown here.
Write code to blend the multi-coloured letters of a word together, as shown here. Hint: As each letter is a different colour, you will need to hold the individual letters in a list.
The example below shows a div being used as a blend (Run Example)
<!DOCTYPE html>
<html>
<head>
<title>Course notes example code</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
img,
#horizontal_stripe
p
{
margin:0px;
padding:0px;
}
img,
#horizontal_stripe
{
width:800px;
}
img
{
height:600px;
}
#horizontal_stripe
{
width:800px;
height:200px;
position:absolute;
top:200px;
background-color:pink;
mix-blend-mode:hue;
}
</style>
</head>
<body>
<img src="mountain.jpg">
<div id="horizontal_stripe"></div>
</body>
</html>
Extend the code above to blend a cross onto an image, as shown here.
Write code to place three blended circles and some non-blended text over an image, as shown here.
The background-blend-mode property specifies a blend mode for an element and its background-image or background-color.
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.