CSS Image Masking (Adding Shapes to Images)

With CSS masking you create a mask layer to place over an element to partially or fully hide portions of the element. It is used to create various shapes, adding image to text, etc.

The CSS mask-image Property

The CSS mask-image property specifies a mask layer image.

The mask layer image can be a PNG image, an SVG image, a CSS gradient, or an SVG <mask> element.

Use an Image as the Mask Layer

To use a PNG or an SVG image as the mask layer, use a url() value to pass in the mask layer image.

The mask image needs to have a transparent or semi-transparent area. Black indicates fully transparent.

Here is the mask image (a PNG image) we will use:

Here is an image of Road:

Now, we apply the mask image (the PNG image above) as the mask layer for the image of Road:

Example

Here is the source code:

.mask1 {
-webkit-mask-image: url(pi.png);
mask-image: url(pi.png);
-webkit-mask-repeat: no-repeat;
mask-repeat: no-repeat;
}

Example Explained

The mask-image property specifies the image to be used as a mask layer for an element.

The mask-repeat property specifies if or how a mask image will be repeated. The no-repeat value indicates that the mask image will not be repeated (the mask image will only be shown once).

Another Example

If we omit the mask-repeat property, the mask image will be repeated all over the image of Road:

Example

Here is the source code:

.mask1 {
-webkit-mask-image: url(pi.png);
mask-image: url(pi.png);
}

Use Gradients as the Mask Layer

CSS linear and radial gradients can also be used as mask images.

Linear Gradient Examples

Here, we use a linear-gradient as the mask layer for our image. This linear gradient goes from top (black) to bottom (transparent):

Road to Life

Example

Use a linear gradient as a mask layer:

.mask1 {
-webkit-mask-image: linear-gradient(black, transparent);
mask-image: linear-gradient(black, transparent);
}

Here, we use a linear-gradient along with text masking as the mask layer for our image:

Love is the most significant thing in human’s life. Each science and every single literature masterwork will tell you about it. Humans are also social animals. We lived for centuries with this way of life, we were depended on one another to tell us how our clothes fit us, how our body is whether healthy or emaciated. All these we get the honest opinions of those who love us, those who care for us and makes our happiness paramount.

Love is a set of emotions, behaviors, and beliefs with strong feelings of affection. So, for example, a person might say he or she loves his or her dog, loves freedom, or loves God. The concept of love may become an unimaginable thing and also it may happen to each person in a particular way.

Love has a variety of feelings, emotions, and attitude. For someone love is more than just being interested physically in another one, rather it is an emotional attachment. We can say love is more of a feeling that a person feels for another person. Therefore, the basic meaning of love is to feel more than liking towards someone.

Example

Use a linear gradient along with text masking as a mask layer:

.mask1 {
max-width: 600px;
height: 450px;
overflow-y: auto;
background: url(img_road.jpg) no-repeat;
-webkit-mask-image: linear-gradient(black, transparent);
mask-image: linear-gradient (black, transparent);
}

Radial Gradient Examples

Here, we use a radial-gradient (shaped as a circle) as the mask layer for our image:

Road to Life

Example

Use a radial gradient as a mask layer (a circle):

.mask2 {
-webkit-mask-image: radial-gradient(circle, black 50%, rgba(0, 0, 0, 0.5) 50%);
mask-image: radial-gradient(circle, black 50%, rgba(0, 0, 0, 0.5) 50%);
}

Here, we use a radial-gradient (shaped as an ellipse) as the mask layer for our image:

Road to Life

Example

Use another radial gradient as a mask layer (an ellipse):

.mask3 {
-webkit-mask-image: radial-gradient(ellipse, black 50%, rgba(0, 0, 0, 0.5) 50%);
mask-image: radial-gradient(ellipse, black 50%, rgba(0, 0, 0, 0.5));
}

Use SVG as the Mask Layer

The SVG <mask> element can be used inside an SVG graphic to create masking effects.

Here, we use the SVG <mask> element to create different mask layers for our image:

Example

An SVG mask layer (formed as a triangle):

<svg width="600" height="400">
<mask id="svgmask1">
<polygon fill="#ffffff" points="200 0, 400 400, 0 400"></polygon>
</mask>
<image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="img_road.jpg" mask="url(#svgmask1)"></image>
</svg>

Example

An SVG mask layer (formed as a star):

<svg width="600" height="400">
<mask id="svgmask2">
<polygon fill="#ffffff" points="100,10 40,198 190,78 10,78 160,198"></polygon>
</mask>
<image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="img_road.jpg" mask="url(#svgmask2)"></image>
</svg>


Example

An SVG mask layer (formed as circles):

<svg width="600" height="400">
<mask id="svgmask3">
<circle fill="#ffffff" cx="75" cy="75" r="75"></circle>
<circle fill="#ffffff" cx="80" cy="260" r="75"></circle>
<circle fill="#ffffff" cx="270" cy="160" r="75"></circle>
</mask>
<image xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="img_road.jpg" mask="url(#svgmask3)"></image>
</svg>

CSS Masking Properties

The following table lists all the CSS masking properties:

Property Description
mask-image Specifies an image to be used as a mask layer for an element
mask-mode Specifies whether the mask layer image is treated as a luminance mask or as an alpha mask
mask-origin Specifies the origin position (the mask position area) of a mask layer image
mask-position Sets the starting position of a mask layer image (relative to the mask position area)
mask-repeat Specifies how the mask layer image is repeated
mask-size Specifies the size of a mask layer image

Comments