Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Filters, Masks and Shapes

The mask property

Reference for the CSS mask shorthand: hide parts of an element using an image or gradient, with mask-image, size, position, repeat and composite.

The mask property hides parts of an element using another image, usually a gradient or an SVG. Where the mask is opaque the element shows through; where the mask is transparent the element is hidden; in between, it fades. That makes masks the tool for soft edges, fading text and icon shapes that take any color.

mask is a shorthand that works like background: it sets one or more mask layers, each with its own image, size, position and repeat.

CSS
.fade {
  mask: linear-gradient(to bottom, black 60%, transparent) no-repeat center / 100% 100%;
}

Try it

Fading edges with a gradient mask
A repeating dotted mask

Syntax

CSS
mask: <mask-layer>#

/* <mask-layer> = <mask-reference> || <position> [ / <bg-size> ]?
                || <repeat-style> || <geometry-box> || [ <geometry-box> | no-clip ]
                || <compositing-operator> || <masking-mode> */
  • Initial value: the initial value of each longhand (see below)
  • Applies to: all elements; in SVG, container elements (except <defs>) and graphics elements
  • Inherited: no
  • Animatable: as each longhand (the position and size parts)
  • Computed value: as each longhand

The shorthand resets these longhands (with initial values in parentheses):

Longhand Initial value
mask-image none
mask-mode match-source
mask-position 0% 0%
mask-size auto
mask-repeat repeat
mask-origin border-box
mask-clip border-box
mask-composite add

mask-border is a separate shorthand and is not reset by mask in the standard specification.

Values

Value Description
none No mask layer.
<image> A gradient, url() image or other image used as mask.
<position> / <size> Position, then a slash and the size, as in background.
<repeat-style> repeat, no-repeat, space, round, repeat-x, repeat-y.
<geometry-box> The first box value sets mask-origin; if a second is given it sets mask-clip, otherwise both use the same box. no-clip is allowed for the clip.
<compositing-operator> add, subtract, intersect or exclude.
<masking-mode> alpha, luminance or match-source.

Separate multiple layers with commas. The first layer is on top.

Usage notes

  • Transparent means hidden. With the default alpha mode for images and gradients, the color is irrelevant, only the alpha channel counts. Use black or any solid color for "visible" and transparent for "hidden".
  • The shorthand resets everything. Setting mask: url(a.svg) after mask-size: cover throws the size away. Put the shorthand first, then adjust longhands.
  • Masks change the whole subtree, including children, borders and shadows, which are masked too. Masked-out areas also stop receiving pointer events.
  • Creates a stacking context when a mask image is set.
  • Cross-origin images need CORS. A url() mask from another origin without CORS headers is treated as if the mask were not valid, so the element can disappear entirely. Host masks on your own origin.
  • Icons in any color. A single-color SVG used as mask-image on an element with background-color: currentColor lets CSS recolor the icon.
  • Accessibility. Masking is visual only; hidden content remains available to assistive technology.

Browser support

Unprefixed mask works in current versions of the major browsers; older Chromium and Safari versions only understood -webkit-mask, so check a compatibility table if you must support them.