🎭 Mastering mask-type in CSS (SVG Masking)

πŸ“Œ 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.

>>β€œmask-type tells SVG whether to treat your mask by transparency or by brightness.”

🧩 Basic Syntax

SVG mask-type syntax

<mask id="m1" mask-type="luminance">
  <!-- mask content -->
</mask>

🎯 Values of mask-type

ValueMeaningUse Case
alphaMask uses the alpha channel (opacity)PNG/SVG shapes with transparency
luminanceMask uses brightness (white=visible, black=hidden)Gradients, grayscale masks, soft fades

Note

βœ” Default behavior of SVG masks is luminance.
βœ” 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

PropertyUsed InPurpose
mask-typeSVG <mask> elementDefines interpretation model (alpha/luminance)
mask-modeCSS masking on HTML elementsDefines 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

mask-type is widely supported because it's part of the SVG spec β€” not CSS masking.

πŸ” 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

πŸ”— Helpful Resources

>>β€œmask-type is the key to controlling how SVG masks reveal β€” by transparency or by light.” ✨