CSS mask-mode Property
The CSS mask-mode property determines whether a mask image uses its transparency (alpha) or brightness (luminance) to control the visibility of an element.
It accepts three values: alpha, luminance, and match-source. The selected mode changes how the colors and transparency of a mask image are interpreted, making it possible to create fades, cutouts, and other masking effects using CSS gradients, image files, and SVG masks.
mask-mode Syntax
The mask-mode property accepts one or more keywords separated by commas. Each value corresponds to a mask layer defined by mask-image.
Basic Syntax
selector {
mask-mode: match-source;
}
Alpha Masking
.element {
mask-mode: alpha;
}
Luminance Masking
.element {
mask-mode: luminance;
}
Match the Mask Source
.element {
mask-mode: match-source;
}
Multiple Mask Layers
.element {
mask-mode: alpha, luminance;
}
CSS-Wide Keywords
.element {
mask-mode: initial;
}
.element {
mask-mode: inherit;
}
.element {
mask-mode: unset;
}
.element {
mask-mode: revert;
}
.element {
mask-mode: revert-layer;
}
The initial value is match-source. This allows the browser to select the appropriate masking mode based on the type of mask source.
Property Values
| Value | Description |
|---|---|
alpha |
Uses the alpha channel of the mask image to determine visibility, regardless of the image's colors. |
luminance |
Uses the brightness of the mask image, multiplied by its alpha value, to determine visibility. |
match-source |
Uses alpha masking for CSS image sources and follows the SVG mask's mask-type when referencing an SVG <mask> element. |
initial |
Resets the property to match-source. |
inherit |
Inherits the computed value from the parent element. |
unset |
Resets the property to match-source because the property 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 | match-source |
| Inherited | No |
| Applies to | All elements, with SVG-specific applicability rules |
| Computed value | As specified |
| Animation type | Discrete |
| Multiple values | Yes, comma-separated |
| Shorthand property | mask |
How It Works
A mask image provides information that determines how much of an element is visible. The mask-mode property tells the browser which information to use.
With alpha masking, visibility depends on transparency. With luminance masking, visibility depends on both brightness and transparency.
Step 1: Create an Element
<div class="masked-panel">
Masked Content
</div>
Step 2: Apply a Mask Image
.masked-panel {
width: 250px;
height: 150px;
background-color: #2563eb;
mask-image: linear-gradient(to right, black, white);
}
The mask contains fully opaque black and white pixels. With the default match-source mode, a CSS gradient uses alpha masking, so both ends of the gradient reveal the element.
Step 3: Select Luminance Masking
.masked-panel {
mask-mode: luminance;
}
The black portion now hides the element, while the white portion reveals it. Gray values between them produce partial visibility.
Step 4: Switch to Alpha Masking
.masked-panel {
mask-mode: alpha;
}
The element becomes fully visible again because both black and white are opaque.
Important Consideration
The mask-mode property does not create a mask image. It changes how an existing mask image is interpreted.
alpha Value
The alpha value uses the transparency channel of a mask image to determine visibility.
Basic Example
.element {
mask-image: linear-gradient(to right, black, transparent);
mask-mode: alpha;
}
The opaque black region reveals the element, while the transparent region hides it.
Opaque Colors
.element {
mask-image: linear-gradient(to right, black, white);
mask-mode: alpha;
}
The entire element remains visible because both colors have full opacity.
Partially Transparent Colors
.element {
mask-image: linear-gradient(
to right,
rgb(0 0 0 / 100%),
rgb(0 0 0 / 50%),
rgb(0 0 0 / 0%)
);
mask-mode: alpha;
}
The mask gradually changes from fully visible to partially visible and then fully hidden.
Using an Image with Transparency
.element {
mask-image: url("images/transparent-mask.png");
mask-mode: alpha;
}
The image's alpha channel determines which portions of the element remain visible.
When to Use alpha
Use alpha when the mask image contains transparent areas, such as PNG artwork, transparent SVG images, or CSS gradients that fade to transparency.
Important Consideration
In alpha mode, an opaque black pixel and an opaque white pixel produce the same mask visibility. Their brightness does not affect the result.
luminance Value
The luminance value uses the brightness of the mask image together with its alpha channel to determine visibility.
Opaque white produces full visibility, opaque black produces no visibility, and gray values produce partial visibility.
Basic Example
.element {
mask-image: linear-gradient(to right, black, white);
mask-mode: luminance;
}
The element is hidden on the left and gradually becomes visible toward the right.
Reverse the Fade
.element {
mask-image: linear-gradient(to right, white, black);
mask-mode: luminance;
}
The element begins fully visible and gradually disappears.
Using Gray Values
.element {
mask-image: linear-gradient(to right, white, gray, black);
mask-mode: luminance;
}
The gray region produces partial visibility because its luminance falls between black and white.
Using Color
.element {
mask-image: linear-gradient(to right, red, yellow, blue);
mask-mode: luminance;
}
Colored pixels also have luminance values. Their contribution depends on the relative brightness of their color components, not simply their hue.
Transparency Still Matters
.element {
mask-image: linear-gradient(
to right,
rgb(255 255 255 / 100%),
rgb(255 255 255 / 50%),
rgb(255 255 255 / 0%)
);
mask-mode: luminance;
}
Although every color stop is white, the decreasing alpha values reduce the final mask visibility.
When to Use luminance
Use luminance when mask artwork is designed with light and dark regions rather than transparency, such as grayscale gradients or SVG masks based on brightness.
Important Consideration
Luminance is calculated from color components using defined weighting and color-space rules. A colored pixel's luminance is not necessarily the same as its apparent brightness on every display.
match-source Value
The match-source value selects the masking mode based on the type of source referenced by mask-image. It is the initial value.
CSS Gradient
.element {
mask-image: linear-gradient(black, transparent);
mask-mode: match-source;
}
The gradient is a CSS image source, so alpha masking is used.
External Image
.element {
mask-image: url("images/mask-shape.png");
mask-mode: match-source;
}
An ordinary image file uses alpha masking when match-source is specified.
SVG Mask Reference
.element {
mask-image: url("#custom-mask");
mask-mode: match-source;
}
When the URL references an SVG <mask> element, the masking mode follows the SVG mask's mask-type property.
Default SVG Mask Type
The initial value of the SVG mask-type property is luminance. Therefore, an SVG mask reference using match-source generally uses luminance masking unless the SVG mask specifies another type.
When to Use match-source
Use match-source when you want CSS images to use alpha masking while allowing SVG mask definitions to determine their own masking mode.
Important Consideration
Referencing an SVG file as an image is different from referencing an SVG <mask> element. An SVG image resource uses alpha masking with match-source, while an SVG mask element can specify its own mask type.
Alpha vs. Luminance
The difference between alpha and luminance masking becomes most noticeable when the mask contains fully opaque colors of different brightness.
| Mask Pixel | Alpha Mode | Luminance Mode |
|---|---|---|
| Opaque white | Fully visible | Fully visible |
| Opaque black | Fully visible | Hidden |
| Opaque gray | Fully visible | Partially visible |
| Transparent white | Hidden | Hidden |
| 50% transparent white | 50% visible | 50% visible |
| 50% transparent black | 50% visible | Hidden |
Alpha Calculation
In alpha mode, the mask value is the pixel's alpha value:
Mask value = Alpha
Luminance Calculation
In luminance mode, the mask value is calculated by multiplying the pixel's luminance by its alpha value:
Mask value = Luminance * Alpha
For standard sRGB colors, the luminance calculation uses linearized red, green, and blue components with defined weighting factors.
Practical Difference
.alpha-example {
mask-image: linear-gradient(black, white);
mask-mode: alpha;
}
.luminance-example {
mask-image: linear-gradient(black, white);
mask-mode: luminance;
}
The alpha example remains visible throughout. The luminance example fades from hidden to visible.
SVG Masks and mask-mode
SVG provides a <mask> element that can define reusable masking artwork. CSS can reference that mask using mask-image.
SVG Luminance Mask
<svg width="0" height="0" xmlns="http://www.w3.org/2000/svg">
<defs>
<mask id="luminance-mask" maskContentUnits="objectBoundingBox" style="mask-type: luminance;">
<rect width="1" height="1" fill="black"/>
<circle cx="0.5" cy="0.5" r="0.4" fill="white"/>
</mask>
</defs>
</svg>
Reference the SVG Mask
.element {
mask-image: url("#luminance-mask");
mask-mode: match-source;
}
The white circle reveals the element while the black background hides it.
SVG Alpha Mask
<svg width="0" height="0" xmlns="http://www.w3.org/2000/svg">
<defs>
<mask id="alpha-mask" maskContentUnits="objectBoundingBox" style="mask-type: alpha;">
<rect width="1" height="1" fill="transparent"/>
<circle cx="0.5" cy="0.5" r="0.4" fill="black"/>
</mask>
</defs>
</svg>
Reference the Alpha Mask
.element {
mask-image: url("#alpha-mask");
mask-mode: match-source;
}
The opaque black circle reveals the element because the SVG mask specifies alpha masking.
Override the SVG Mask Type
.element {
mask-image: url("#luminance-mask");
mask-mode: alpha;
}
The explicit alpha value overrides the SVG mask's luminance setting for this CSS mask layer.
Important Consideration
The mask-type property belongs to the SVG mask definition, while mask-mode controls how the referenced mask layer is interpreted on the element being masked.
Multiple Mask Layers
The mask-mode property supports comma-separated values for multiple mask image layers.
Two Mask Images
.element {
mask-image:
linear-gradient(black, transparent),
linear-gradient(white, black);
mask-mode: alpha, luminance;
}
The first mask layer uses alpha masking, while the second uses luminance masking.
Three Mask Images
.element {
mask-image:
linear-gradient(black, transparent),
radial-gradient(white, black),
linear-gradient(to right, black, transparent);
mask-mode: alpha, luminance, alpha;
}
Repeating Values
.element {
mask-image:
linear-gradient(black, transparent),
radial-gradient(white, black),
linear-gradient(to right, black, transparent);
mask-mode: alpha, luminance;
}
When fewer mask-mode values are supplied than mask image layers, the values repeat. In this example, the third layer uses alpha.
Extra Values
.element {
mask-image:
linear-gradient(black, transparent),
radial-gradient(white, black);
mask-mode: alpha, luminance, alpha;
}
Values beyond the number of mask image layers are not used for rendering.
Combining Different Mask Modes
.element {
mask-image:
linear-gradient(to right, black, transparent),
radial-gradient(circle, white 40%, black 70%);
mask-mode: alpha, luminance;
mask-composite: intersect;
}
The first layer uses transparency, the second uses brightness, and mask-composite keeps only the regions where the resulting mask layers overlap.
Important Consideration
The mask-mode property controls each layer's interpretation. The mask-composite property controls how the resulting layers are combined.
Using the mask Shorthand
The mask-mode property is a longhand component of the mask shorthand.
Alpha Mode in the Shorthand
.element {
mask: linear-gradient(black, transparent) alpha;
}
Luminance Mode in the Shorthand
.element {
mask: linear-gradient(white, black) luminance;
}
Multiple Mask Layers
.element {
mask:
linear-gradient(black, transparent) alpha,
radial-gradient(white, black) luminance;
}
Equivalent Longhand Declarations
.element {
mask-image:
linear-gradient(black, transparent),
radial-gradient(white, black);
mask-mode: alpha, luminance;
}
Override the Shorthand
.element {
mask: linear-gradient(white, black);
mask-mode: luminance;
}
The later longhand declaration changes the masking mode to luminance.
Shorthand Reset Behavior
.element {
mask-mode: luminance;
mask: linear-gradient(white, black);
}
The later shorthand resets mask-mode to its initial value of match-source. Because the source is a CSS gradient, alpha masking is used.
Correct Declaration Order
.element {
mask: linear-gradient(white, black);
mask-mode: luminance;
}
Place the longhand declaration after the shorthand when it should override the shorthand's default masking mode.
mask-mode Example
This example compares alpha and luminance masking using the same black-to-white gradient. The difference demonstrates why mask colors matter in luminance mode but not in alpha mode.
HTML Example
<div class="mask-mode-demo">
<div class="mask-mode-item">
<div class="mask-mode-box mode-alpha"></div>
<p>alpha</p>
</div>
<div class="mask-mode-item">
<div class="mask-mode-box mode-luminance"></div>
<p>luminance</p>
</div>
<div class="mask-mode-item">
<div class="mask-mode-box mode-match-source"></div>
<p>match-source</p>
</div>
<div class="mask-mode-item">
<div class="mask-mode-box mode-alpha-fade"></div>
<p>alpha with transparency</p>
</div>
</div>
CSS Example
.mask-mode-demo {
display: grid;
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 20px;
padding: 20px;
background-color: #f1f5f9;
}
.mask-mode-item {
min-width: 0;
text-align: center;
}
.mask-mode-box {
width: 100%;
height: 120px;
background-color: #2563eb;
}
.mode-alpha {
mask-image: linear-gradient(to right, black, white);
mask-mode: alpha;
}
.mode-luminance {
mask-image: linear-gradient(to right, black, white);
mask-mode: luminance;
}
.mode-match-source {
mask-image: linear-gradient(to right, black, white);
mask-mode: match-source;
}
.mode-alpha-fade {
mask-image: linear-gradient(to right, black, transparent);
mask-mode: alpha;
}
.mask-mode-item p {
margin: 10px 0 0;
font-weight: bold;
}
@media (max-width: 575px) {
.mask-mode-demo {
grid-template-columns: 1fr;
}
}
alpha
luminance
match-source
alpha with transparency
The alpha and match-source examples remain fully visible because their gradient colors are opaque. The luminance example fades from hidden to visible. The final example demonstrates alpha masking with a gradient that actually changes transparency.
Luminance Spotlight
.luminance-spotlight {
width: 250px;
height: 150px;
background-color: #059669;
mask-image: radial-gradient(circle, white 25%, black 75%);
mask-mode: luminance;
}
The center remains visible while the surrounding area fades to transparency according to the mask's brightness.
Alpha Spotlight
.alpha-spotlight {
width: 250px;
height: 150px;
background-color: #059669;
mask-image: radial-gradient(circle, black 25%, transparent 75%);
mask-mode: alpha;
}
This example produces a similar visual effect using transparency rather than luminance.
Common Mistakes
Expecting Black to Hide Content in Alpha Mode
.element {
mask-image: linear-gradient(black, white);
mask-mode: alpha;
}
Both colors are opaque, so the element remains visible throughout.
Correct Luminance Fade
.element {
mask-image: linear-gradient(black, white);
mask-mode: luminance;
}
Using mask-mode Without a Mask Image
.element {
mask-mode: luminance;
}
The property does not create a mask. A mask image or SVG mask reference is needed for a visible masking effect.
Confusing mask-mode with mask-type
The mask-mode property controls how CSS mask layers are interpreted. The mask-type property defines the masking mode of an SVG <mask> element.
Using an Invalid Keyword
.element {
mask-mode: brightness;
}
The valid keywords are alpha, luminance, and match-source.
Using Space-Separated Values
.element {
mask-mode: alpha luminance;
}
Multiple values must be separated by commas.
Assuming match-source Always Means Alpha
The match-source value uses alpha masking for CSS image sources but can use luminance masking when referencing an SVG mask element.
Ignoring Transparency in Luminance Mode
Luminance masking uses both brightness and alpha. Transparent white does not reveal an element because its alpha value is zero.
Overwriting the Property with the mask Shorthand
.element {
mask-mode: luminance;
mask: linear-gradient(white, black);
}
The later shorthand resets mask-mode to match-source.
Important Considerations
Keep the following points in mind when using mask-mode:
- The property determines how mask image pixels control visibility.
- Its initial value is
match-source. - The property is not inherited.
- The
alphavalue uses the mask image's transparency channel. - The
luminancevalue uses brightness multiplied by alpha. - The
match-sourcevalue selects a mode based on the mask source. - CSS gradients and ordinary image sources use alpha masking with
match-source. - SVG mask references can use the SVG mask's
mask-typesetting. - Opaque black and white are equally visible in alpha mode.
- Opaque black hides content in luminance mode.
- Opaque white reveals content in luminance mode.
- Gray values produce partial visibility in luminance mode.
- Transparent pixels hide content in either mode.
- Partially transparent pixels can produce partial visibility.
- Colored mask images can be used in luminance mode.
- The property does not create a mask image by itself.
- Multiple values can be assigned to multiple mask image layers.
- Comma-separated values correspond to mask layers in order.
- Values repeat when fewer modes are supplied than mask layers.
- The
mask-compositeproperty controls how mask layers are combined. - The
mask-imageproperty defines the mask source. - The property is a longhand component of the
maskshorthand. - The
maskshorthand resets omitted mask modes tomatch-source. - Masking changes visibility without changing layout dimensions.
- Test complex masking effects in the browsers your website supports.
Browser Support
The CSS mask-mode property is Baseline 2023 - Newly available. It is supported in current versions of major browsers, although older browser versions may have limited support for standardized masking behavior.
Support for SVG mask references, luminance masking, and the interaction between mask-mode and mask-type may vary in older implementations.
For current compatibility information, visit Can I Use? and search for mask-mode.
When supporting older browsers, test the actual masking result rather than relying only on whether the property is recognized.
Specifications
The mask-mode property is defined in CSS Masking Module Level 1.
CSS Masking Module Level 1: mask-mode
The specification defines alpha masking, luminance masking, and how mask sources determine the selected mode.
For additional documentation and examples, consult MDN Web Docs.
