With CSS masking, you make a cover to hide some parts of an element. This cover can hide a little or a lot of the element.
The CSS mask-image Property
The CSS property called mask-image is used to tell the web browser which image to use as a mask.
The image for the mask layer can be in different formats like PNG, SVG, a CSS gradient, or an SVG element.
Use an Image as the Mask Layer
To use a PNG or SVG image as the mask layer, you can simply provide the image's URL inside the url() value.
The picture with a mask should have a see-through part. When it's black, it means it's completely see-through.
Here is the mask picture, which is in PNG format, that we will utilize:
Look at this picture from Cinque Terre, Italy:
Now, let's use the PNG image above as a mask to apply over the picture from Cinque Terre, Italy:
Example Explained
The mask-image property tells a web page which picture to use as a special layer that hides or shows parts of an element.
The "mask-repeat" property decides how many times a mask image will be shown. If it's set to "no-repeat," the mask image will be displayed only once.
Another Example
If we don't use the 'mask-repeat' property, the mask image will be duplicated across the entire image of Cinque Terre, Italy.
Use Gradients as the Mask Layer
You can use CSS linear and radial gradients as mask images too.
Linear Gradient Examples
In this code, we're using a special kind of gradient to hide part of our picture. This gradient starts at the top in black and fades out towards the bottom until it's completely see-through.
In this code, we create a special effect on an image using a linear gradient combined with text to make the image look unique.
The Cinque Terre is a beautiful coastal region in the northwest part of Italy, specifically within Liguria. It's situated in the western part of La Spezia Province and is made up of five charming villages: Monterosso al Mare, Vernazza, Corniglia, Manarola, and Riomaggiore.
The Cinque Terre is a beautiful coastal region located in the northwest of Italy, specifically in Liguria. It can be found to the west of La Spezia Province and is made up of five charming villages: Monterosso al Mare, Vernazza, Corniglia, Manarola, and Riomaggiore.
The Cinque Terre is a stretch of beautiful coastline in northwest Italy, specifically in the Liguria region. This area is located to the west of the La Spezia Province and is made up of five charming villages: Monterosso al Mare, Vernazza, Corniglia, Manarola, and Riomaggiore.
Radial Gradient Examples
In this code, we create a circular gradient that acts like a mask for our image.
Example
Apply a different circular gradient as a mask layer, in the shape of an ellipse.
<!DOCTYPE html>
<html>
<head>
<style>
p {
font-size: 20px;
padding: 20px;
color: white;
}
.mask1 {
max-width: 600px;
height: 350px;
overflow-y: scroll;
background: url(/assets/files/eiffel-tower.jpg) no-repeat;
-webkit-mask-image: linear-gradient(black, transparent);
mask-image: linear-gradient (black, transparent);
}
</style>
</head>
<body>
<h1>The mask-image Property</h1>
<h3>A linear gradient as a mask layer:</h3>
<div class="mask1">
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.</p>
</div>
</body>
</html>