Skip to main content

The CSS mask-image Property

Placing a mask layer over an element is how CSS masking hides some or all of that element.

You choose the layer's image with the mask-image property, and PNG and SVG images both work, as do CSS gradients and the SVG <mask> element.


Use a PNG Image as the Mask Layer

A url() value is how a PNG image gets passed in as the mask layer.

Such an image must contain an area that is transparent or semi-transparent. Transparency hides the element, whereas opaque pixels, even black ones, leave it visible.

Two properties work together here. mask-image names the image that masks the element, and mask-repeat decides if or how that image repeats, with no-repeat showing it a single time.

Example

Prompt
Create an HTML page that applies a PNG mask image to a photo using the CSS mask-image property. Use url(w3logo.png) as the mask with mask-repeat set to no-repeat. Include the -webkit- prefix for browser compatibility.
Output: complete HTML page

Repeat the Mask Layer Image

Drop the mask-repeat property and the mask image tiles across the entire image.

Example

Prompt
Create an HTML page that applies a PNG mask image to a photo using the CSS mask-image property, but omit the mask-repeat property so the mask repeats over the entire image. Use url(w3logo.png) as the mask.
Output: complete HTML page

Position the Mask Layer Image

A mask image starts out at the top-left corner of its element, repeated in both directions. To start it somewhere else, use mask-position, whose value is relative to the mask position area.

Example

Here the mask layer image sits in the center:

Prompt
Create an HTML page that applies a PNG mask image to a photo using the CSS mask-image property with mask-repeat set to no-repeat and mask-position set to center. Use url(w3logo.png) as the mask.
Output: complete HTML page