Imperative Guide to CSS masking

Imperative Guide to CSS masking

Imperative Guide to CSS masking

What is CSS Masking

Masking in CSS is the process of hiding or partially hiding images or graphical elements. It is a graphical operation that hides a partial portion of an image or object. It allows you to use an image as a mask layer with different opacity levels. This implies that you can use an Image, an SVG, or a gradient as your mask to create a beautiful visual effect.

In CSS, masking is implemented using a mask-image property to provide an image to be used as the mask. When it’s 100% black the Image will be visible. When it’s 100% transparent, it will hide it, and when it’s anything in-between, it will partially mask the Image. In this article, we will explain how to use the mask-image property in CSS and also show you three ways  to use an image as a mask layer which are:

  • Masking with Image
  • Masking using Gradient
  • Masking with SVG

Before that, there is some browser compatibility with CSS. Some browser doesn’t fully support the standard CSS masking property. You will have to prefix -webkit- to the specific property for it to be browser compatible. You can also check Can I use CSS Masks? for the browser support for CSS masks.

When using masking in CSS, it’s advisable to correct what will happen if masking is unavailable with feature queries to detect support for mask-mage or -webkit-mask-image. While making provision for a readable fallback adding your masked version.

Masking Using Gradient

Masking an image with a gradient can give a beautiful and nice-appealing look to your image. A linear gradient can mask an image area from transparent to black. For example, in the image below, the first image is our default image without a mask. While the second image has a linear gradient applied to the mask-image value:

This the CSS mask-image rule used for the second image in the diagram below here:

Masking Image in CSS with Gradient

Masking Image in CSS with Gradient

You can also be stylistic and creative with the masking by using any gradient type. The third image in the diagram above uses a radial gradient in creating a circular mask.

Masking with Image

To mask an image, a fully transparent area will make the image invisible while the part with a little transparency will allow the original to be visible.

The first image is the original image in the below example, while the second image is the image mask itself. The third image applies to the mask below.

Masking in CSS with image

Masking in CSS with image

Below is the CSS style applied to the mask:

Here we specified the mask-image url() which works similarly to the way the background-image property works.

Masking with SVG

If you love SVG as I do. Instead of using an image as the mask, you can define image masks with the SVG mask element.

To achieve this, we will define the image as part of an SVG element itself:

Masking image in CSS with SVG

Masking image in CSS with SVG


I hope you really enjoy working through this article. We have learned how to use mask in CSS to hides part of the element it is applied to. We covered how masking works with Image, Gradient, and SVG.  Also kindly check this reference below to learn more in-depth on masking.

If you have any questions, kindly engage me in the comment below.

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles


Style like a pro with CSS variables

In my opinion, one of the most significant advantages of using CSS variables is that I can avoid repeatedly duplicating the same value by storing it ...


CSS Grid

Cascading Style Sheets Grid Layout, or CSS Grid for short, is a fantastic layout framework made especially for creating flat web designs. It's a ...


CSS Flexbox

Flexbox, also known as Flexible Box Layout, is a CSS layout module that has transformed the approach to responsive design and content arrangement. ...

No comments yet

Sign in

Forgot password?

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy