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

Conclusion

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.

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Masking
https://developer.mozilla.org/en-US/docs/Web/CSS/mask

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

An in-depth guide on the CSS position property

Positions are used a lot in CSS to describe how elements are placed in a document. Also, they describe how elements behave with the top, left, ...

28.08.2020

How to build complex layouts with CSS Grid

Grid layout is one the most powerful features available in modern CSS. In this article we discuss how to use css grids to build amazing, responsive ...

7 Concepts: Why Your SVG Is the Way It Is

If you have already worked with SVGs or you are new to it, this article is for you. I am sharing a few important concepts which will help you debug ...

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