W3cubDocs

/SVG

<mask>

In SVG, you can specify that any other graphics object or <g> element can be used as an alpha mask for compositing the current object into the background. A mask is defined with the <mask> element. A mask is used/referenced using the mask property.

Usage context

Attributes

Global attributes

Specific attributes

DOM Interface

This element implements the SVGMaskElement interface.

Example

SVG

<?xml version="1.0"?>
<svg width="200" height="80" viewBox="0 0 200 80" version="1.1"
    xmlns="http://www.w3.org/2000/svg" 
    xmlns:xlink="http://www.w3.org/1999/xlink">

  <defs>
    <mask id="myMask" maskUnits="userSpaceOnUse"
        x="0" y="0" width="200" height="80">
      <rect x="0" y="0" width="100" height="80" fill="white"/>
    </mask>

    <text id="Text" x="100" y="48" font-size="26" font-weight="bold"
        text-anchor="middle">
      Black & White
    </text>
  </defs>

  <!-- Draw black rectangle in the background -->
  <rect x="100" y="10" width="95" height="60" />
  
  <!-- Draw the text string twice. First, the white text without mask.
       Second, the black text with the mask applied-->
  <use xlink:href="#Text" fill="white"/>
  <use xlink:href="#Text" fill="black" mask="url(#myMask)"/>
</svg>

Result

Specifications

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) ?
Feature Android Edge Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support ? (Yes) ? ? ? ?

See also

© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/SVG/Element/mask