CSS mask-type Property
The CSS mask-type property specifies whether an SVG mask uses luminance or alpha values to determine transparency.
It applies to SVG <mask> elements and accepts two values: luminance and alpha. The selected value determines whether the colors and brightness of the mask content affect visibility or whether only its alpha transparency is used.
mask-type Syntax
The mask-type property accepts one keyword that determines how the SVG mask's content is interpreted.
Basic Syntax
mask {
mask-type: luminance;
}
Alpha Mask
mask {
mask-type: alpha;
}
Target a Specific SVG Mask
#shape-mask {
mask-type: alpha;
}
CSS-Wide Keywords
mask {
mask-type: initial;
}
mask {
mask-type: inherit;
}
mask {
mask-type: unset;
}
mask {
mask-type: revert;
}
mask {
mask-type: revert-layer;
}
The initial value is luminance. The property is not inherited.
Property Values
| Value | Description |
|---|---|
luminance |
Uses the luminance of the mask content, multiplied by its alpha value, to determine visibility. This is the default. |
alpha |
Uses only the alpha transparency of the mask content, regardless of its color or brightness. |
initial |
Resets the property to luminance. |
inherit |
Inherits the computed value from the parent element. |
unset |
Resets the property to luminance 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 | luminance |
| Inherited | No |
| Applies to | SVG <mask> elements |
| Computed value | As specified |
| Animation type | Discrete |
| Accepted values | luminance, alpha, and CSS-wide keywords |
How It Works
An SVG mask contains shapes, images, or other graphical content that determine which portions of another element remain visible.
The browser renders the mask content and converts it into a transparency mask. The mask-type property determines how that conversion takes place.
Luminance Masking
With luminance, the browser considers both the brightness and alpha transparency of the mask content. White produces maximum visibility, black produces no visibility, and intermediate brightness values produce partial visibility.
Alpha Masking
With alpha, the browser considers only transparency. Fully opaque black, white, and colored shapes all produce full visibility.
Applying an SVG Mask
.masked-element {
mask-image: url("#shape-mask");
}
The URL references an SVG <mask> element with the matching ID. The mask's mask-type value determines its default interpretation when the referencing mask layer uses mask-mode: match-source.
luminance Value
The luminance value calculates mask visibility using the brightness and alpha transparency of the rendered mask content.
Basic Example
#brightness-mask {
mask-type: luminance;
}
SVG Mask
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 120">
<defs>
<mask id="brightness-mask" style="mask-type: luminance;">
<rect width="240" height="120" fill="white"/>
<circle cx="120" cy="60" r="40" fill="black"/>
</mask>
</defs>
<rect width="240" height="120" fill="#2563eb" mask="url(#brightness-mask)"/>
</svg>
The white area reveals the blue rectangle. The black circle hides the corresponding portion, creating a transparent circular opening.
Using Gray
<mask id="gray-mask" style="mask-type: luminance;">
<rect width="100%" height="100%" fill="gray"/>
</mask>
Gray produces partial visibility because its luminance falls between black and white.
Using a Gradient
<defs>
<linearGradient id="fade-gradient">
<stop offset="0%" stop-color="white"/>
<stop offset="100%" stop-color="black"/>
</linearGradient>
<mask id="fade-mask" style="mask-type: luminance;">
<rect width="100%" height="100%" fill="url(#fade-gradient)"/>
</mask>
</defs>
The gradient produces a gradual transition from visible to transparent.
When to Use luminance
Use luminance when the brightness of the mask's colors should control transparency, particularly with grayscale images, gradients, and SVG shapes.
alpha Value
The alpha value determines visibility using the transparency of the rendered SVG mask content rather than its brightness.
Basic Example
#alpha-mask {
mask-type: alpha;
}
SVG Mask
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 120">
<defs>
<mask id="alpha-mask" style="mask-type: alpha;">
<circle cx="120" cy="60" r="45" fill="black"/>
</mask>
</defs>
<rect width="240" height="120" fill="#2563eb" mask="url(#alpha-mask)"/>
</svg>
The black circle is fully opaque, so it reveals a circular portion of the blue rectangle. The area outside the circle remains transparent.
Partial Transparency
<mask id="transparent-mask" style="mask-type: alpha;">
<circle cx="120" cy="60" r="45" fill="black" opacity="0.5"/>
</mask>
The circle has 50% opacity, so the corresponding portion of the masked element becomes partially transparent.
Colored Shapes
<mask id="color-mask" style="mask-type: alpha;">
<rect width="100%" height="100%" fill="red"/>
</mask>
Because the red rectangle is fully opaque, it produces full visibility. Its color does not affect the alpha calculation.
When to Use alpha
Use alpha when mask visibility should depend on transparency rather than the brightness or color of the SVG shapes.
Alpha vs. Luminance
The main difference between the two values is how they interpret opaque colors.
| Mask Content | luminance | alpha |
|---|---|---|
| Opaque white | Fully visible | Fully visible |
| Opaque black | Fully hidden | Fully visible |
| Opaque gray | Partially visible | Fully visible |
| Opaque red | Partially visible according to luminance | Fully visible |
| Transparent color | Fully hidden | Fully hidden |
| White at 50% opacity | 50% visible | 50% visible |
Same Black Shape, Different Results
#mask-one {
mask-type: luminance;
}
#mask-two {
mask-type: alpha;
}
If both masks contain an opaque black circle, the luminance mask hides the circle while the alpha mask reveals it.
Luminance Calculation
Luminance masking converts the rendered color to a brightness value and multiplies that value by the pixel's alpha. The calculation uses color-space conversion and luminance weighting, so different colors do not contribute equally.
Alpha Calculation
Alpha masking uses the rendered pixel's alpha value directly. An opaque pixel produces full mask coverage regardless of its color.
SVG mask Element
The mask-type property is intended for SVG <mask> elements. It can be applied using CSS or the SVG mask-type presentation attribute.
Using CSS
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 120">
<style>
#circle-mask {
mask-type: alpha;
}
</style>
<defs>
<mask id="circle-mask">
<circle cx="120" cy="60" r="45" fill="black"/>
</mask>
</defs>
<rect width="240" height="120" fill="#059669" mask="url(#circle-mask)"/>
</svg>
Using an SVG Presentation Attribute
<mask id="circle-mask" mask-type="alpha">
<circle cx="120" cy="60" r="45" fill="black"/>
</mask>
The presentation attribute specifies the same mask type, but a CSS declaration can override it through the cascade.
Referencing an SVG Mask from HTML
<svg width="0" height="0" aria-hidden="true">
<defs>
<mask id="html-mask" mask-type="alpha">
<circle cx="50" cy="50" r="45" fill="white"/>
</mask>
</defs>
</svg>
<div class="masked-box">Masked Content</div>
.masked-box {
width: 100px;
height: 100px;
background-color: #2563eb;
mask-image: url("#html-mask");
}
The HTML element references the SVG mask through its ID. For more predictable results, define the SVG mask's coordinate system and dimensions to match the intended target.
SVG Mask Region
The SVG <mask> element also supports attributes such as x, y, width, height, maskUnits, and maskContentUnits. These control the mask region and coordinate systems, not whether alpha or luminance is used.
mask-type and mask-mode
The mask-type and mask-mode properties are related but apply to different parts of the masking system.
| Property | Purpose |
|---|---|
mask-type |
Defines the default interpretation of an SVG <mask> element. |
mask-mode |
Controls whether a mask image layer uses alpha, luminance, or the referenced source's default mode. |
Using match-source
.masked-element {
mask-image: url("#shape-mask");
mask-mode: match-source;
}
When the mask image references an SVG <mask> element, match-source uses that element's mask-type value.
Explicit Alpha Mode
.masked-element {
mask-image: url("#shape-mask");
mask-mode: alpha;
}
An explicit alpha value on the referencing mask layer uses alpha masking, overriding the SVG mask's default luminance interpretation for that layer.
Ordinary CSS Mask Images
.masked-element {
mask-image: linear-gradient(black, transparent);
mask-mode: alpha;
}
For ordinary CSS image masks, use mask-mode rather than mask-type to select alpha or luminance interpretation.
Important Consideration
The mask shorthand on an element does not set the mask-type property of a referenced SVG mask. These are separate declarations on different elements.
mask-type Example
This example compares luminance and alpha masking using identical SVG mask content. Both masks contain an opaque white rectangle and an opaque black circle.
HTML Example
<div class="mask-type-demo">
<svg viewBox="0 0 520 150" xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Comparison of luminance and alpha SVG masks">
<defs>
<mask id="demo-luminance" maskUnits="userSpaceOnUse" x="0" y="0" width="240" height="120" style="mask-type: luminance;">
<rect x="0" y="0" width="240" height="120" fill="white"/>
<circle cx="120" cy="60" r="40" fill="black"/>
</mask>
<mask id="demo-alpha" maskUnits="userSpaceOnUse" x="280" y="0" width="240" height="120" style="mask-type: alpha;">
<rect x="280" y="0" width="240" height="120" fill="white"/>
<circle cx="400" cy="60" r="40" fill="black"/>
</mask>
</defs>
<rect width="520" height="120" fill="#e2e8f0"/>
<rect x="0" y="0" width="240" height="120" fill="#2563eb" mask="url(#demo-luminance)"/>
<rect x="280" y="0" width="240" height="120" fill="#2563eb" mask="url(#demo-alpha)"/>
<text x="120" y="142" text-anchor="middle" font-size="14">luminance</text>
<text x="400" y="142" text-anchor="middle" font-size="14">alpha</text>
</svg>
</div>
CSS Example
.mask-type-demo {
max-width: 650px;
margin: 0 auto;
padding: 15px;
background-color: #f8fafc;
}
.mask-type-demo svg {
display: block;
width: 100%;
height: auto;
}
The luminance example displays a transparent circular opening because the black circle has zero luminance. The alpha example displays the entire blue rectangle because both the white background and black circle are fully opaque.
Create a Circular Alpha Mask
<svg viewBox="0 0 240 120" xmlns="http://www.w3.org/2000/svg">
<defs>
<mask id="circle-only" mask-type="alpha" maskUnits="userSpaceOnUse" x="0" y="0" width="240" height="120">
<circle cx="120" cy="60" r="50" fill="black"/>
</mask>
</defs>
<rect width="240" height="120" fill="#059669" mask="url(#circle-only)"/>
</svg>
Because the mask contains only an opaque circle, the alpha mask reveals a circular portion of the green rectangle.
Common Mistakes
Applying mask-type to an HTML Element
.masked-box {
mask-type: alpha;
}
The mask-type property applies to SVG <mask> elements, not ordinary HTML elements. Use mask-mode when controlling how a mask layer on an HTML element is interpreted.
Expecting Black to Hide Content in Alpha Mode
An opaque black shape reveals content in alpha mode. Use transparent content to hide areas, or select luminance masking when black should produce transparency.
Using an Invalid Value
mask {
mask-type: grayscale;
}
The grayscale keyword is invalid. Use luminance or alpha.
Confusing mask-type with mask-mode
mask-type defines the default interpretation of an SVG mask. mask-mode controls the interpretation of a mask image layer on the element being masked.
Forgetting the Default Value
The default is luminance. An SVG mask containing only opaque black shapes may hide the entire masked region unless alpha mode is selected.
Confusing the Mask Region with the Mask Type
The mask-type property does not control the mask's position or dimensions. SVG mask geometry is controlled by the mask element's attributes and coordinate systems.
Using an Incorrect Mask Reference
.element {
mask-image: url("#missing-mask");
}
The referenced SVG mask must exist and have a matching ID. An unresolved mask reference can cause the element to become invisible.
Important Considerations
Keep the following points in mind when using mask-type:
- The property applies to SVG
<mask>elements. - Its initial value is
luminance. - The property is not inherited.
luminanceuses brightness multiplied by alpha transparency.alphauses alpha transparency without considering color brightness.- Opaque black produces no visibility in luminance mode but full visibility in alpha mode.
- Fully transparent pixels hide the corresponding content in both modes.
- Gray and colored shapes can produce partial visibility in luminance mode.
- The property can be set through CSS or an SVG presentation attribute.
- The
mask-modeproperty can override the default interpretation of a referenced SVG mask. - The property does not determine mask positioning, dimensions, or repetition.
- SVG mask coordinate systems and mask regions are controlled separately.
- Changing the mask type affects rendered visibility without changing document layout.
- Test SVG mask behavior across browsers, especially when combining SVG masks with CSS masking properties.
Browser Support
The CSS mask-type property is Baseline 2023 - Newly available. It is supported in current versions of major browsers for specifying alpha and luminance interpretation on SVG <mask> elements.
Older browsers may have differences in SVG mask rendering, presentation attribute support, or interactions with CSS mask-mode.
For current compatibility information, visit Can I Use? and search for mask-type.
When supporting older browsers, test both alpha and luminance masks with the actual SVG content used by your website.
Specifications
The mask-type property is defined in CSS Masking Module Level 1.
CSS Masking Module Level 1: mask-type
The specification defines alpha and luminance masking and how SVG mask elements provide their default mask interpretation.
For additional documentation and examples, consult MDN Web Docs.
