π What Is mask-type?
The mask-type property is used specifically with SVG masks and determines how the mask is interpreted:
π by its luminance (brightness) or
π by its alpha (transparency).
It is placed inside an SVGβs <mask> element.
CSS masking uses mask-mode for HTML elements, but SVG uses mask-type.
π§© Basic Syntax
SVG mask-type syntax
<mask id="m1" mask-type="luminance">
<!-- mask content -->
</mask>π― Values of mask-type
| Value | Meaning | Use Case |
|---|---|---|
| alpha | Mask uses the alpha channel (opacity) | PNG/SVG shapes with transparency |
| luminance | Mask uses brightness (white=visible, black=hidden) | Gradients, grayscale masks, soft fades |
Note
β Use alpha when your mask uses transparency instead of brightness.
π§ͺ Example 1 β Luminance-Based SVG Mask (Default)
Luminance mask
<svg>
<mask id="fadeMask" mask-type="luminance">
<rect width="100%" height="100%" fill="url(#fadeGradient)" />
</mask>
<linearGradient id="fadeGradient">
<stop offset="0%" stop-color="white" />
<stop offset="100%" stop-color="black" />
</linearGradient>
<image href="photo.jpg" mask="url(#fadeMask)" />
</svg>β White reveals image
β Black hides image
β Produces smooth fade-out effect
π§ͺ Example 2 β Alpha-Based SVG Mask
Alpha mask
<svg>
<mask id="shapeMask" mask-type="alpha">
<circle cx="100" cy="100" r="80" fill="black" fill-opacity="1" />
</mask>
<image href="pic.png" mask="url(#shapeMask)" />
</svg>β Circle opacity controls reveal
β Transparent = hidden
β Opaque = visible
β Perfect for stencil-like cutout shapes
π¨ mask-type in Action: Cutout Text Effect
Text mask using alpha
<svg>
<mask id="textMask" mask-type="alpha">
<text x="0" y="100" font-size="100" fill="white">HELLO</text>
</mask>
<rect width="100%" height="100%" fill="blue" mask="url(#textMask)" />
</svg>β Text acts as the mask
β Background is shown through the text area
β A clean text-cutout effect
π§ mask-type vs mask-mode
| Property | Used In | Purpose |
|---|---|---|
| mask-type | SVG <mask> element | Defines interpretation model (alpha/luminance) |
| mask-mode | CSS masking on HTML elements | Defines how mask is interpreted |
β They solve the same problem but in different environments.
π₯ Advanced Example β Blurred Spotlight Mask
Spotlight using luminance
<svg>
<mask id="spotlight" mask-type="luminance">
<radialGradient id="grad">
<stop offset="0%" stop-color="white" />
<stop offset="100%" stop-color="black" />
</radialGradient>
<rect fill="url(#grad)" width="100%" height="100%" />
</mask>
<image href="stage.jpg" width="100%" mask="url(#spotlight)" />
</svg>β Bright center shows image
β Dark edges hide image
β Creates a natural spotlight reveal
π οΈ Best Practices
- Use alpha for SVG masks that rely on opacity
- Use luminance for grayscale gradient-based masks
- Avoid unnecessary complexity inside masks β keep shapes simple
- Use high-resolution mask images for clean edges
- Remember that mask-type only works in SVG mask elements
β οΈ Browser Support
- β Chrome
- β Edge
- β Safari
- β Firefox
Note
π Debugging Tips
- If mask looks inverted β try switching alpha β luminance
- Ensure SVG mask elements are placed before their usage
- Check that mask shape has proper opacity or brightness
- Use DevTools βMask Editing Modeβ (Chrome) to preview