CSS mask Property
The CSS mask property is a shorthand that controls which portions of an element are visible by applying one or more masking images.
CSS masks use transparency or luminance to reveal, partially reveal, or hide portions of an element. They are useful for creating fades, decorative shapes, image effects, and custom visual transitions without changing the underlying HTML content.
mask Syntax
The mask shorthand accepts a mask image or SVG mask reference, along with optional values that control how the mask is positioned, sized, repeated, and applied.
Basic Syntax
selector {
mask: <mask-layer>#;
}
The # notation indicates that one or more mask layers can be supplied, separated by commas. It is syntax notation, not a character included in an actual declaration.
Mask Image
.element {
mask: url("mask.png");
}
Linear Gradient Mask
.element {
mask: linear-gradient(to right, black, transparent);
}
Radial Gradient Mask
.element {
mask: radial-gradient(circle, black 40%, transparent 75%);
}
Position and Size
.element {
mask: url("mask.svg") center / contain no-repeat;
}
Multiple Mask Layers
.element {
mask: linear-gradient(to right, black, transparent), radial-gradient(circle, black, transparent);
}
Remove the Mask
.element {
mask: none;
}
CSS-Wide Keywords
.element {
mask: initial;
}
.element {
mask: inherit;
}
.element {
mask: unset;
}
.element {
mask: revert;
}
.element {
mask: revert-layer;
}
The mask property is a shorthand. Its initial state uses no mask image, leaving the element fully visible unless another masking or clipping effect applies.
Property Values
The mask shorthand combines values from several masking properties. Individual values can be omitted when their initial settings are appropriate.
| Value | Description |
|---|---|
none |
Specifies no mask image for the layer. |
url() |
References an image or supported SVG masking resource. |
linear-gradient() |
Creates a gradient mask with a linear transition. |
radial-gradient() |
Creates a gradient mask with a radial transition. |
alpha |
Uses the alpha channel to determine mask transparency. |
luminance |
Uses color brightness and alpha to determine mask transparency. |
match-source |
Uses the masking mode associated with the source. |
left, center, right, top, bottom |
Position the mask image within its positioning area. |
length or percentage |
Specify mask positioning or sizing values, depending on their location in the shorthand. |
auto |
Uses automatic mask sizing when specified in the size component. |
cover |
Scales the mask image to cover its positioning area. |
contain |
Scales the mask image to fit inside its positioning area. |
repeat, no-repeat, repeat-x, repeat-y, space, round |
Control how the mask image repeats. |
border-box, padding-box, content-box |
Control the mask positioning area or clipping region, depending on their placement. |
fill-box, stroke-box, view-box |
Provide additional reference boxes, particularly for SVG graphics. |
no-clip |
Prevents clipping of the mask layer to a specified box. |
add, subtract, intersect, exclude |
Control how multiple mask layers are combined. |
initial |
Resets all properties represented by the shorthand to their initial values. |
inherit |
Inherits the corresponding computed values from the parent element. |
unset |
Resets the constituent properties according to whether each property is inherited. |
revert |
Reverts the constituent properties according to cascade origin rules. |
revert-layer |
Reverts the constituent properties according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Property type | Shorthand |
| Initial state | No mask image |
| Inherited | No |
| Applies to | All elements, with SVG-specific exceptions defined by the specification |
| Computed value | Determined by the constituent longhand properties |
| Animation type | Determined by the constituent longhand properties |
| Multiple layers | Yes |
| Common uses | Image fades, decorative shapes, transparent overlays, and SVG masking |
How It Works
A CSS mask controls how much of an element is visible at each point. The browser renders the element and uses the mask to determine the transparency of the resulting image.
A fully opaque area of an alpha mask reveals the element, a fully transparent area hides it, and partially transparent areas create smooth transitions.
Basic Gradient Mask
.fade-box {
background-color: #2563eb;
mask: linear-gradient(to right, black, transparent);
}
The element is fully visible on the left and gradually disappears toward the right.
Masking Does Not Change Layout
A mask changes the visible appearance of an element but does not remove the element from document flow or change its calculated dimensions.
Masking Applies to Rendered Content
The masking effect can affect an element's background, borders, text, and descendants as part of the rendered element.
Important Consideration
Masking is a visual effect. Hidden portions of an element are not automatically removed from the accessibility tree or from keyboard interaction.
Mask Images
Mask images can come from raster images, SVG images, CSS gradients, or supported SVG mask references.
PNG Mask
.masked-image {
mask: url("shape-mask.png") center / contain no-repeat;
}
A PNG image with transparency can reveal or hide parts of an element according to its alpha channel.
SVG Image Mask
.masked-image {
mask: url("shape-mask.svg") center / contain no-repeat;
}
An SVG image can provide a scalable mask source.
Linear Gradient Mask
.masked-image {
mask: linear-gradient(to bottom, black 65%, transparent);
}
This creates a vertical fade that begins near the bottom of the element.
Radial Gradient Mask
.masked-image {
mask: radial-gradient(circle, black 35%, transparent 75%);
}
The center remains visible while the outer edges gradually disappear.
Conic Gradient Mask
.masked-image {
mask: conic-gradient(black 0deg 240deg, transparent 240deg 360deg);
}
This creates an angular mask with a visible sector and a transparent sector.
SVG Mask Element
An SVG <mask> element can define a reusable masking resource.
<svg width="0" height="0" aria-hidden="true" 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
.masked-image {
mask: url(#circle-mask);
}
SVG mask elements commonly use luminance-based masking by default. White regions reveal content, while black regions hide it.
Mask Mode
The masking mode determines whether the browser uses transparency or luminance to calculate the visible portions of an element.
Alpha Masking
.element {
mask: linear-gradient(to right, black, transparent) alpha;
}
With alpha masking, the color itself does not determine visibility. Opaque black and opaque white are both fully visible; transparent areas hide the element.
Luminance Masking
.element {
mask: linear-gradient(to right, white, black) luminance;
}
With luminance masking, lighter colors reveal more of the element and darker colors reveal less. Transparency also affects the result.
Match the Source
.element {
mask: url("mask.svg") match-source;
}
The match-source value uses the appropriate mode for the source. CSS image sources generally use alpha masking, while an SVG <mask> reference can use the mode defined by that SVG mask.
Important Consideration
An opaque black gradient does not hide an element when alpha masking is used. To hide content with an alpha mask, use transparency.
Mask Position and Size
The mask shorthand can control the placement and dimensions of a mask image using position values followed by a slash and size values.
Centered Mask
.element {
mask: url("shape-mask.svg") center no-repeat;
}
Centered and Contained
.element {
mask: url("shape-mask.svg") center / contain no-repeat;
}
The mask image scales to fit within the positioning area while maintaining its aspect ratio.
Cover the Entire Area
.element {
mask: url("shape-mask.svg") center / cover no-repeat;
}
The mask image scales to cover the positioning area. Portions of the mask may extend beyond that area.
Explicit Dimensions
.element {
mask: url("shape-mask.svg") center / 150px 150px no-repeat;
}
Percentage Dimensions
.element {
mask: url("shape-mask.svg") center / 60% 60% no-repeat;
}
Position at the Top Right
.element {
mask: url("shape-mask.svg") right top / 100px 100px no-repeat;
}
Important Consideration
When specifying mask size in the shorthand, place the size after the position and separate them with a forward slash.
Mask Repetition
Mask images repeat by default. Use repetition values in the shorthand to control whether and how the image tiles across the element.
Repeat in Both Directions
.element {
mask: url("pattern-mask.png") repeat;
}
Do Not Repeat
.element {
mask: url("shape-mask.svg") center / 120px 120px no-repeat;
}
Repeat Horizontally
.element {
mask: url("pattern-mask.png") repeat-x;
}
Repeat Vertically
.element {
mask: url("pattern-mask.png") repeat-y;
}
Space Repeated Masks
.element {
mask: url("pattern-mask.png") space;
}
Round Repeated Masks
.element {
mask: url("pattern-mask.png") round;
}
The space value distributes whole repeated images with spacing when possible, while round adjusts image dimensions to fit a whole number of repetitions.
Important Consideration
For a single decorative mask shape, no-repeat is usually appropriate. Otherwise, the mask image may tile unexpectedly.
Mask Origin and Clip
The mask-origin and mask-clip components determine the reference area used to position the mask and the region in which the mask can affect the element.
Use the Border Box
.element {
mask: url("shape-mask.svg") border-box;
}
A single box value sets both the mask origin and clipping box.
Use the Content Box
.element {
mask: url("shape-mask.svg") content-box;
}
Separate Origin and Clip
.element {
mask: url("shape-mask.svg") padding-box content-box;
}
When two box values are supplied, the first controls the mask origin and the second controls clipping.
Disable Mask Clipping
.element {
mask: url("shape-mask.svg") border-box no-clip;
}
The no-clip value removes clipping by the mask layer's box, although other clipping and painting constraints may still apply.
SVG Reference Boxes
svg path {
mask: url("shape-mask.svg") fill-box;
}
SVG graphics can use reference boxes such as fill-box, stroke-box, and view-box.
Important Consideration
Changing the mask origin or clipping box can alter both the mask's placement and the portion of the element that remains visible.
Multiple Mask Layers
The mask property supports multiple comma-separated layers. Each layer can use a different image, position, size, repetition setting, or compositing operation.
Two Gradient Layers
.element {
mask: linear-gradient(to right, black, transparent), linear-gradient(to bottom, black, transparent);
}
Both gradients are assigned as mask layers. Their combined effect depends on the compositing settings.
Image and Gradient Layers
.element {
mask: url("shape-mask.svg") center / contain no-repeat, linear-gradient(to right, black, transparent);
}
Different Positions
.element {
mask: radial-gradient(circle, black 60%, transparent 65%) left center / 50% 100% no-repeat, radial-gradient(circle, black 60%, transparent 65%) right center / 50% 100% no-repeat;
}
Layer Order
The first mask layer is the topmost layer. Subsequent layers are placed beneath it, and their combined result is determined by mask-composite.
Important Consideration
Multiple mask layers do not necessarily behave like a single gradient. Their transparency is combined according to the compositing operation used for each layer.
Mask Compositing
Mask compositing controls how multiple mask layers interact. The mask-composite component supports four operations.
| Value | Effect |
|---|---|
add |
Combines the visible areas of the mask layers. |
subtract |
Subtracts the underlying mask from the current mask layer. |
intersect |
Keeps the areas shared by the combined masks. |
exclude |
Combines nonoverlapping areas while excluding overlap. |
Add Mask Layers
.element {
mask: radial-gradient(circle at 35% 50%, black 30%, transparent 31%) add, radial-gradient(circle at 65% 50%, black 30%, transparent 31%);
}
The visible regions of the two circular masks are combined.
Intersect Mask Layers
.element {
mask: linear-gradient(to right, black 65%, transparent 65%) intersect, linear-gradient(to bottom, black 65%, transparent 65%);
}
Only the overlapping visible region remains.
Exclude Overlapping Areas
.element {
mask: radial-gradient(circle at 40% 50%, black 35%, transparent 36%) exclude, radial-gradient(circle at 60% 50%, black 35%, transparent 36%);
}
The overlapping regions are excluded from the resulting mask.
Important Consideration
Compositing operations are applied between mask layers. A compositing value on the final layer has no lower layer to combine with.
Shorthand and Longhand Properties
The mask shorthand combines several masking properties into one declaration.
Shorthand Example
.element {
mask: url("shape-mask.svg") center / contain no-repeat;
}
Equivalent Longhand Declarations
.element {
mask-image: url("shape-mask.svg");
mask-position: center;
mask-size: contain;
mask-repeat: no-repeat;
}
The remaining mask components use their initial values.
Related Longhand Properties
| Property | Purpose |
|---|---|
mask-image |
Specifies the masking image or resource. |
mask-mode |
Controls alpha or luminance masking. |
mask-position |
Positions the mask image. |
mask-size |
Controls the mask image dimensions. |
mask-repeat |
Controls mask image repetition. |
mask-origin |
Defines the positioning reference box. |
mask-clip |
Defines the clipping region. |
mask-composite |
Controls how multiple mask layers combine. |
Reset Behavior
.element {
mask-size: 150px 150px;
mask: url("shape-mask.svg") no-repeat;
}
The later shorthand resets mask-size to its initial value because no size is supplied in that declaration.
Mask Border Reset
The mask shorthand also resets the mask-border-* properties to their initial values, even though those properties cannot be assigned through the mask shorthand itself.
.element {
mask-border-source: url("border-mask.png");
mask: linear-gradient(black, transparent);
}
The later mask declaration resets mask-border-source to none.
Important Consideration
Place the mask shorthand before any individual mask properties that should override its settings.
mask vs. clip-path
Both mask and clip-path can control which portions of an element are visible, but they work differently.
| Feature | mask | clip-path |
|---|---|---|
| Visibility | Supports partial transparency | Defines a clipping region |
| Soft edges | Supports smooth fades | Generally creates defined boundaries |
| Gradients | Can use gradients directly | Does not accept gradients as clipping shapes |
| Images | Can use image masks | Uses clipping shapes or paths |
| Typical use | Fades, transparency, complex image effects | Geometric clipping and custom shapes |
Mask Example
.element {
mask: radial-gradient(circle, black 40%, transparent 75%);
}
The edges fade gradually.
Clipping Example
.element {
clip-path: circle(40%);
}
The element is clipped to a circular region with a defined boundary.
Important Consideration
Use mask when partial transparency is required. Use clip-path when the goal is to define a geometric visible region.
mask Example
This example demonstrates three CSS gradient masks: a horizontal fade, a radial fade, and a diagonal fade. Each mask changes the visibility of a colored element.
HTML Example
<div class="mask-demo">
<div class="mask-demo-box mask-demo-horizontal">Horizontal Fade</div>
<div class="mask-demo-box mask-demo-radial">Radial Fade</div>
<div class="mask-demo-box mask-demo-diagonal">Diagonal Fade</div>
</div>
CSS Example
.mask-demo {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
gap: 15px;
}
.mask-demo-box {
display: flex;
align-items: center;
justify-content: center;
min-height: 140px;
padding: 15px;
background-color: #2563eb;
color: #fff;
font-weight: bold;
text-align: center;
}
.mask-demo-horizontal {
mask: linear-gradient(to right, black 35%, transparent);
}
.mask-demo-radial {
mask: radial-gradient(circle, black 35%, transparent 75%);
}
.mask-demo-diagonal {
mask: linear-gradient(135deg, black 35%, transparent);
}
The first box fades from left to right, the second fades outward from its center, and the third fades diagonally. The elements retain their original dimensions even where the mask makes portions transparent.
Change the Fade Direction
.mask-demo-horizontal {
mask: linear-gradient(to bottom, black 35%, transparent);
}
The horizontal fade becomes a vertical fade.
Create a Hard Edge
.mask-demo-horizontal {
mask: linear-gradient(to right, black 50%, transparent 50%);
}
The transition becomes a sharp boundary rather than a gradual fade.
Create an Elliptical Fade
.mask-demo-radial {
mask: radial-gradient(ellipse, black 30%, transparent 75%);
}
The visible region uses an elliptical gradient shape.
Remove the Mask
.mask-demo-box {
mask: none;
}
Without a mask, the entire background and text of each box remain visible.
Common Mistakes
Using Black to Hide Content in an Alpha Mask
.element {
mask: linear-gradient(black, white);
}
Both colors are fully opaque, so this gradient does not create a fade when alpha masking is used. Use transparent colors or explicitly select luminance masking.
Forgetting no-repeat
Mask images repeat by default. A small mask image may tile across the element unless repetition is disabled.
Omitting the Slash Before Mask Size
.element {
mask: url("shape-mask.svg") center / contain no-repeat;
}
The slash separates the mask position from the mask size.
Using an Invalid Image URL
An invalid or unavailable mask image can cause the element to become fully transparent instead of appearing unmasked.
Confusing Masking with Layout
Masking does not remove hidden portions from document flow or resize the element's layout box.
Assuming All Mask Sources Use Luminance
CSS gradient and image masks generally use alpha masking by default. An SVG <mask> reference can use luminance masking.
Overwriting Longhand Properties
.element {
mask-size: contain;
mask: url("shape-mask.svg") no-repeat;
}
The shorthand resets mask-size to its initial value. Place the longhand declaration afterward if it should override the shorthand.
Confusing mask with mask-border
The mask shorthand controls mask layers. The mask-border property provides separate border masking functionality.
Ignoring Accessibility
Visually hidden content may still be accessible to assistive technologies or keyboard users. Do not rely on masking alone to hide content that must be unavailable to users.
Important Considerations
Keep the following points in mind when using the mask property:
- The property is a shorthand for several CSS masking properties.
- It controls element visibility using mask images or masking resources.
- The initial state has no mask image.
- The property is not inherited.
- Masking can produce full visibility, partial transparency, or complete transparency.
- CSS gradients can be used directly as mask images.
- PNG and SVG images can provide masking sources.
- SVG
<mask>elements can define reusable masking resources. - Alpha masking uses transparency to determine visibility.
- Luminance masking uses brightness and transparency.
- Opaque black remains visible under alpha masking.
- Mask images repeat by default.
- Use
no-repeatfor a single mask shape. - Mask position and size can be set in the shorthand.
- A forward slash separates position and size values.
- Multiple mask layers are separated by commas.
- The first mask layer is the topmost layer.
- Mask compositing controls how multiple layers interact.
- The
mask-origincomponent controls the positioning reference box. - The
mask-clipcomponent controls the clipping region. - The shorthand resets omitted mask components to their initial values.
- The shorthand also resets
mask-border-*properties. - Masking does not change the element's layout dimensions.
- Masking can affect backgrounds, borders, text, and descendants.
- Masking can create smooth fades that are not possible with ordinary geometric clipping alone.
- Unavailable mask resources can cause unexpected transparency.
- Visual masking does not automatically remove content from the accessibility tree.
- Test complex masks and compositing effects across supported browsers.
Browser Support
The CSS mask property has Baseline 2023 Newly available status, reflecting broad support for the standardized shorthand across current major browsers.
Some masking features, including certain SVG references and compositing operations, may have different compatibility requirements. Older browsers may require the -webkit-mask prefix for some masking effects.
For current compatibility information, visit Can I Use? and search for CSS mask.
Specifications
The mask property is defined in CSS Masking Module Level 1.
CSS Masking Module Level 1: mask
The specification defines the shorthand syntax, mask layers, masking modes, positioning, sizing, clipping, and compositing behavior.
For additional documentation and examples, consult MDN Web Docs.
