CSS mask-image Property

The CSS mask-image property defines one or more images used as masks to control which portions of an element are visible, partially transparent, or hidden.

Mask images can be created using CSS gradients, external image files, or SVG mask elements. Unlike a background image, which adds visible artwork to an element, a mask uses transparency or luminance information to control the visibility of the element and its contents.

mask-image Syntax

The mask-image property accepts the keyword none, an image value, an SVG mask reference, or a comma-separated list of mask layers.

Basic Syntax

selector {
  mask-image: none;
}

Linear Gradient

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

Radial Gradient

.element {
  mask-image: radial-gradient(circle, black 50%, transparent 80%);
}

Conic Gradient

.element {
  mask-image: conic-gradient(black 0deg 180deg, transparent 180deg 360deg);
}

External Image

.element {
  mask-image: url("images/mask.png");
}

SVG Mask Reference

.element {
  mask-image: url("#custom-mask");
}

Multiple Mask Layers

.element {
  mask-image:
    linear-gradient(to right, black, transparent),
    radial-gradient(circle, black 40%, transparent 70%);
}

CSS-Wide Keywords

.element {
  mask-image: initial;
}
.element {
  mask-image: inherit;
}
.element {
  mask-image: unset;
}
.element {
  mask-image: revert;
}
.element {
  mask-image: revert-layer;
}

The initial value is none. An element without a mask image is not hidden by CSS masking.

Property Values

Value Description
none Specifies an empty mask image layer. This is the initial value.
url() References an external image resource or an SVG mask element.
linear-gradient() Creates a linear gradient that controls visibility through its alpha values.
radial-gradient() Creates a circular or elliptical gradient mask.
conic-gradient() Creates a gradient mask with color stops arranged around a center point.
repeating-linear-gradient() Creates a repeating linear gradient mask.
repeating-radial-gradient() Creates a repeating radial gradient mask.
repeating-conic-gradient() Creates a repeating conic gradient mask.
image() Provides an image value where the CSS Images function is supported.
image-set() Provides alternative image resources for the browser to select from.
initial Resets the property to none.
inherit Inherits the computed value from the parent element.
unset Resets the property to none because it is not inherited.
revert Reverts the property according to cascade origin rules.
revert-layer Reverts the property according to cascade layer rules.

Property Characteristics

Characteristic Value
Initial value none
Inherited No
Applies to All elements, with SVG-specific applicability rules
Computed value As specified, with image URLs made absolute
Animation type Discrete
Multiple values Yes, comma-separated mask layers
Shorthand property mask

How It Works

A mask image provides information that determines the transparency of an element. Areas where the mask is fully opaque reveal the element, while fully transparent areas hide it.

Partially transparent mask regions make the corresponding portions of the element partially visible.

Step 1: Create an Element

<div class="masked-panel">
  Masked Content
</div>

Step 2: Apply Basic Styling

.masked-panel {
  width: 250px;
  height: 150px;
  padding: 20px;
  background-color: #2563eb;
  color: #fff;
}

Step 3: Add a Mask Image

.masked-panel {
  mask-image: linear-gradient(to right, black, transparent);
}

The left side remains visible because the gradient is opaque. The right side gradually disappears as the gradient becomes transparent.

Step 4: Adjust the Fade

.masked-panel {
  mask-image: linear-gradient(to right, black 50%, transparent 100%);
}

The element remains fully visible through the first half of the gradient and fades across the remaining half.

Important Consideration

CSS masking changes the element's appearance without changing its layout dimensions. The element still occupies its original space in the document.

Linear Gradient Masks

The linear-gradient() function creates a mask with transparency that changes along a straight line.

Fade from Left to Right

.element {
  mask-image: linear-gradient(to right, black, transparent);
}

Fade from Top to Bottom

.element {
  mask-image: linear-gradient(to bottom, black, transparent);
}

Fade from Both Sides

.element {
  mask-image: linear-gradient(to right, transparent, black 25% 75%, transparent);
}

The center remains visible while the left and right edges fade away.

Create a Sharp Edge

.element {
  mask-image: linear-gradient(to right, black 50%, transparent 50%);
}

Matching gradient stop positions produce a sharp transition rather than a gradual fade.

Repeating Stripes

.element {
  mask-image: repeating-linear-gradient(
    90deg,
    black 0 15px,
    transparent 15px 30px
  );
}

The repeating gradient creates alternating visible and transparent vertical stripes.

When to Use Linear Gradient Masks

Linear gradient masks are useful for fading images, softening content edges, creating stripes, and revealing portions of an element without editing its original image.

Radial Gradient Masks

The radial-gradient() function creates circular or elliptical mask regions that extend outward from a center point.

Circular Fade

.element {
  mask-image: radial-gradient(circle, black 40%, transparent 75%);
}

The center remains visible and gradually fades toward the outside.

Circular Cutout

.element {
  mask-image: radial-gradient(circle, transparent 30%, black 31%);
}

The center is hidden while the surrounding region remains visible.

Move the Mask Center

.element {
  mask-image: radial-gradient(circle at 70% 30%, black 20%, transparent 65%);
}

The visible region is centered toward the upper-right portion of the element.

Elliptical Mask

.element {
  mask-image: radial-gradient(ellipse, black 45%, transparent 75%);
}

Repeating Rings

.element {
  mask-image: repeating-radial-gradient(
    circle,
    black 0 10px,
    transparent 10px 20px
  );
}

The repeating radial gradient creates alternating visible and transparent rings.

When to Use Radial Gradient Masks

Radial masks are useful for spotlights, circular reveals, decorative cutouts, vignettes, and soft-edged image effects.

Conic Gradient Masks

The conic-gradient() function creates a gradient that progresses around a center point rather than along a straight line or outward from a center.

Half-Circle Mask

.element {
  mask-image: conic-gradient(black 0deg 180deg, transparent 180deg 360deg);
}

One half of the element is revealed while the other half is hidden.

Quarter-Circle Mask

.element {
  mask-image: conic-gradient(black 0deg 90deg, transparent 90deg 360deg);
}

Angular Fade

.element {
  mask-image: conic-gradient(black, transparent);
}

The mask gradually changes transparency around its center.

Repeating Segments

.element {
  mask-image: repeating-conic-gradient(
    black 0deg 20deg,
    transparent 20deg 40deg
  );
}

The repeating conic gradient creates alternating angular sections.

When to Use Conic Gradient Masks

Conic gradient masks are useful for circular progress effects, segmented designs, radial patterns, and angular reveals.

Image File Masks

The url() function can reference an image file to use as a mask.

PNG Mask

.element {
  mask-image: url("images/mask-shape.png");
}

A PNG image with transparency can define the visible and hidden portions of an element.

WebP Mask

.element {
  mask-image: url("images/mask-shape.webp");
}

SVG Image Mask

.element {
  mask-image: url("images/mask-shape.svg");
}

An SVG file used as an image can provide scalable mask artwork. This is different from referencing an SVG <mask> element.

Mask Image with Size and Position

.element {
  mask-image: url("images/mask-shape.png");
  mask-size: contain;
  mask-position: center;
  mask-repeat: no-repeat;
}

The mask image is scaled to fit within the available positioning area, centered, and displayed without repetition.

High-Resolution Mask Images

.element {
  mask-image: image-set(
    url("images/mask.png") 1x,
    url("images/mask@2x.png") 2x
  );
}

The image-set() function allows the browser to select an appropriate image resource according to its supported selection criteria.

Important Consideration

External mask images must load successfully. If a specified mask image cannot be loaded or decoded, its mask layer is treated as transparent and the affected element may become invisible.

SVG Masks

CSS can reference an SVG <mask> element using a URL fragment identifier.

SVG Mask Definition

<svg width="0" height="0" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <mask id="circle-mask" maskContentUnits="objectBoundingBox">
      <circle cx="0.5" cy="0.5" r="0.45" fill="white"/>
    </mask>
  </defs>
</svg>

Reference the SVG Mask

.element {
  mask-image: url("#circle-mask");
}

The CSS declaration references the SVG mask element with the matching ID.

Using an External SVG Mask Element

.element {
  mask-image: url("images/masks.svg#circle-mask");
}

The URL references a mask element defined inside an external SVG document, subject to browser support and resource security restrictions.

SVG Mask Colors

SVG mask elements commonly use luminance masking, where lighter colors contribute more visibility and darker colors contribute less. The mask-type property can define whether the SVG mask uses luminance or alpha values.

Important Consideration

SVG masks have their own coordinate systems, mask regions, and rendering rules. When referencing them from CSS, verify the mask dimensions and units so the intended artwork covers the target element.

Alpha vs. Luminance Masking

CSS masks can use either alpha information or luminance information to determine visibility.

Alpha Masking

Alpha masking uses the transparency of the mask image. Fully opaque pixels reveal the element, while fully transparent pixels hide it.

.element {
  mask-image: linear-gradient(black, transparent);
  mask-mode: alpha;
}

Opaque black is fully revealing in alpha mode because the color itself does not determine visibility.

Luminance Masking

Luminance masking uses the brightness of the mask image, together with its alpha value, to determine visibility.

.element {
  mask-image: linear-gradient(white, black);
  mask-mode: luminance;
}

Opaque white reveals the element, while opaque black hides it. Intermediate brightness levels produce partial transparency.

Comparison

Mask Pixel Alpha Mode Luminance Mode
Opaque white Visible Visible
Opaque black Visible Hidden
Transparent Hidden Hidden
Partially transparent white Partially visible Partially visible

Default Behavior

The initial value of mask-mode is match-source. Ordinary CSS image masks use alpha masking, while SVG mask references can use the mask type defined by the SVG mask element.

Important Consideration

A black-to-white gradient does not create a transparency fade when used as an ordinary alpha mask because both colors are fully opaque. Use transparent color stops or explicitly select luminance masking.

Multiple Mask Layers

The mask-image property supports multiple images separated by commas.

Two Mask Images

.element {
  mask-image:
    linear-gradient(to right, black, transparent),
    radial-gradient(circle, black 40%, transparent 70%);
}

Each image creates a separate mask layer. The first listed image is the topmost layer.

Three Mask Images

.element {
  mask-image:
    linear-gradient(to right, black, transparent),
    radial-gradient(circle, black 40%, transparent 70%),
    linear-gradient(to bottom, black, transparent);
}

Combining Mask Layers

.element {
  mask-image:
    radial-gradient(circle at 35% 50%, black 30%, transparent 31%),
    radial-gradient(circle at 65% 50%, black 30%, transparent 31%);
  mask-composite: intersect;
}

The intersect operation keeps only the region where both circular mask layers overlap.

Subtracting a Mask Layer

.element {
  mask-image:
    linear-gradient(black, black),
    radial-gradient(circle, black 30%, transparent 31%);
  mask-composite: subtract;
}

The circular mask is subtracted from the opaque rectangular mask, creating a transparent circular opening.

Using Different Layer Sizes

.element {
  mask-image:
    radial-gradient(circle, black 50%, transparent 51%),
    linear-gradient(black, black);
  mask-size: 100px 100px, 100% 100%;
  mask-position: center;
  mask-repeat: no-repeat;
}

Each mask layer can have its own dimensions, position, and repetition settings.

Important Consideration

Multiple mask layers are combined using mask-composite. The default add operation combines their visible regions rather than restricting visibility to their overlap.

Mask Position and Size

Several related CSS properties control how a mask image is displayed on an element.

Property Purpose
mask-size Controls the dimensions of the mask image.
mask-position Controls the position of the mask image.
mask-repeat Controls whether and how the mask image repeats.
mask-origin Defines the positioning area for the mask image.
mask-clip Defines the area affected by a mask layer.
mask-mode Determines whether alpha or luminance information controls visibility.
mask-composite Controls how multiple mask layers are combined.

Center a Mask Image

.element {
  mask-image: radial-gradient(circle, black 50%, transparent 51%);
  mask-size: 150px 150px;
  mask-position: center;
  mask-repeat: no-repeat;
}

Cover the Element

.element {
  mask-image: url("images/mask-shape.png");
  mask-size: cover;
  mask-position: center;
  mask-repeat: no-repeat;
}

Repeat a Mask Pattern

.element {
  mask-image: radial-gradient(circle, black 45%, transparent 46%);
  mask-size: 40px 40px;
  mask-repeat: repeat;
}

The small circular mask repeats across the element, creating a pattern of visible circles.

Important Consideration

The default mask-repeat value is repeat. Specify no-repeat when a single mask shape should appear without tiling.

mask-image vs. background-image

Although both properties accept CSS image values, they serve different purposes.

Property Purpose
background-image Paints an image behind an element's content.
mask-image Controls the visibility of the element and its rendered contents.

Background Image

.element {
  background-image: linear-gradient(to right, #2563eb, #059669);
}

The gradient is painted as a visible background.

Mask Image

.element {
  background-color: #2563eb;
  mask-image: linear-gradient(to right, black, transparent);
}

The gradient controls the transparency of the element rather than painting black over its background.

Using Both Properties

.element {
  background-image: linear-gradient(135deg, #2563eb, #059669);
  mask-image: radial-gradient(circle, black 45%, transparent 80%);
}

The background gradient provides the visible colors, while the radial mask determines which portions remain visible.

Using the mask Shorthand

The mask-image property is a longhand component of the mask shorthand.

Basic Shorthand

.element {
  mask: linear-gradient(to right, black, transparent);
}

This declaration sets the mask image and resets the other mask-related longhand properties to their initial values.

Mask with Position and Size

.element {
  mask: url("images/mask-shape.png") center / contain no-repeat;
}

Equivalent Longhand Declarations

.element {
  mask-image: url("images/mask-shape.png");
  mask-position: center;
  mask-size: contain;
  mask-repeat: no-repeat;
}

Multiple Mask Layers

.element {
  mask:
    linear-gradient(to right, black, transparent),
    radial-gradient(circle, black 40%, transparent 70%);
}

Override the Shorthand

.element {
  mask: url("images/mask-shape.png") center / contain no-repeat;
  mask-size: 120px 120px;
}

The later mask-size declaration overrides the size specified by the shorthand.

Shorthand Reset Behavior

.element {
  mask-image: url("images/mask-shape.png");
  mask: linear-gradient(black, transparent);
}

The later shorthand replaces the previously declared mask image.

Important Consideration

The mask shorthand also resets the mask-border longhand properties to their initial values. Place declarations that should override the shorthand after it.

mask-image Example

This example compares four mask effects using the same background color. Each element uses a different CSS gradient to control its visible area.

HTML Example

<div class="mask-image-demo">
  <div class="mask-image-item">
    <div class="mask-image-box mask-linear"></div>
    <p>Linear Fade</p>
  </div>
  <div class="mask-image-item">
    <div class="mask-image-box mask-radial"></div>
    <p>Radial Fade</p>
  </div>
  <div class="mask-image-item">
    <div class="mask-image-box mask-cutout"></div>
    <p>Circular Cutout</p>
  </div>
  <div class="mask-image-item">
    <div class="mask-image-box mask-stripes"></div>
    <p>Repeating Stripes</p>
  </div>
</div>

CSS Example

.mask-image-demo {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 20px;
  padding: 20px;
  background-color: #f1f5f9;
}
.mask-image-item {
  min-width: 0;
  text-align: center;
}
.mask-image-box {
  width: 100%;
  height: 120px;
  background-color: #2563eb;
}
.mask-linear {
  mask-image: linear-gradient(to right, black, transparent);
}
.mask-radial {
  mask-image: radial-gradient(ellipse, black 30%, transparent 75%);
}
.mask-cutout {
  mask-image: radial-gradient(circle, transparent 25%, black 26%);
}
.mask-stripes {
  mask-image: repeating-linear-gradient(
    90deg,
    black 0 15px,
    transparent 15px 30px
  );
}
.mask-image-item p {
  margin: 10px 0 0;
  font-weight: bold;
}
@media (max-width: 575px) {
  .mask-image-demo {
    grid-template-columns: 1fr;
  }
}
Browser Result

Linear Fade

Radial Fade

Circular Cutout

Repeating Stripes

The linear mask fades from left to right. The radial mask reveals the center with softened edges. The circular cutout hides the center, while the repeating gradient creates alternating visible and transparent stripes.

Fade an Image

<img class="faded-image" src="images/landscape.jpg" alt="Mountain landscape">
.faded-image {
  display: block;
  max-width: 100%;
  height: auto;
  mask-image: linear-gradient(to bottom, black 60%, transparent);
}

The image remains visible through most of its height and fades toward its bottom edge.

Create a Spotlight Effect

.spotlight {
  background-color: #059669;
  mask-image: radial-gradient(circle at center, black 25%, transparent 70%);
}

The center remains visible while the surrounding area gradually fades away.

Create a Repeating Dot Pattern

.dot-pattern {
  background-color: #2563eb;
  mask-image: radial-gradient(circle, black 35%, transparent 36%);
  mask-size: 30px 30px;
  mask-repeat: repeat;
}

The radial mask repeats across the element, creating a pattern of visible dots.

Common Mistakes

Using Opaque Black and White for an Alpha Fade

.element {
  mask-image: linear-gradient(black, white);
}

Both colors are fully opaque. With the default alpha masking behavior, the gradient does not create a transparency fade.

Correct Alpha Gradient

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

Confusing mask-image with background-image

A background image adds visible artwork. A mask image controls the transparency of the element and its contents.

Using an Invalid Color as the Property Value

.element {
  mask-image: black;
}

The property requires an image value, a mask reference, or none. A standalone color is not valid.

Forgetting mask-repeat

.element {
  mask-image: url("images/mask-shape.png");
  mask-size: 50px 50px;
}

The image repeats by default. Add mask-repeat: no-repeat when only one copy should appear.

Using an Incorrect Image Path

If the mask resource cannot be loaded, the mask layer may become transparent and hide the element. Verify the URL and confirm that the image file exists.

Assuming Masking Changes Layout

Masking changes the rendered appearance, not the dimensions or normal document position of the element.

Expecting Multiple Masks to Intersect Automatically

Multiple mask layers use the add compositing operation by default. Use mask-composite: intersect when only their overlapping regions should remain visible.

Confusing SVG Image Files with SVG Mask Elements

Referencing an SVG image file is not the same as referencing an SVG <mask> element. The latter uses SVG masking rules and may have different default mask modes.

Overwriting the Property with the mask Shorthand

.element {
  mask-image: url("images/mask-shape.png");
  mask: linear-gradient(black, transparent);
}

The later shorthand replaces the earlier mask image.

Important Considerations

Keep the following points in mind when using mask-image:

  • The property defines one or more images used to mask an element.
  • Its initial value is none.
  • The property is not inherited.
  • Masking affects the visibility of the element and its rendered contents.
  • Fully opaque alpha mask regions reveal the element.
  • Fully transparent mask regions hide the element.
  • Partially transparent mask regions produce partial visibility.
  • CSS gradients can be used directly as mask images.
  • External image resources can provide custom mask shapes.
  • SVG mask elements can be referenced through URLs.
  • Ordinary CSS image masks use alpha masking by default.
  • SVG mask references can use luminance masking.
  • The mask-mode property controls the mask interpretation mode.
  • The mask-size property controls mask image dimensions.
  • The mask-position property controls mask placement.
  • The mask-repeat property controls mask tiling.
  • The mask-origin property determines the mask positioning area.
  • The mask-clip property determines the clipping area.
  • The mask-composite property controls how multiple mask layers combine.
  • Mask images are listed from topmost to bottommost.
  • Failed mask image resources can result in transparent mask layers.
  • Masking does not change the element's layout dimensions.
  • The property is a longhand component of the mask shorthand.
  • The mask shorthand can reset previously declared mask properties.
  • Complex mask effects should be tested in the browsers your website supports.

Browser Support

The CSS mask-image property is Baseline 2023 - Newly available. It is supported in current versions of major browsers, including common uses involving gradients and image-based masks.

Some older browsers require the -webkit-mask-image prefix. Support can also vary for particular image functions, SVG mask references, and related masking features.

For current compatibility information, visit Can I Use? and search for mask-image.

When supporting older browsers, test the actual rendering result, especially when using SVG references, multiple mask layers, or complex gradient functions.

Specifications

The mask-image property is defined in CSS Masking Module Level 1.

CSS Masking Module Level 1: mask-image

The specification defines mask image sources, mask layer processing, and how mask images affect the transparency of rendered elements.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: mask-image