CSS Masking - mask-type



The CSS property mask-type sets an SVG <mask> element as a luminance or an alpha mask. It applies to the <mask> element itself.

The mask-mode CSS property can override this property, as it has the same effect. The only difference is, it gets applied to the element where mask is used. Rendering of alpha masks is generally faster.

Possible values

The CSS property mask-type is specifies as one of the following values:

  • luminance: It is a keyword that signifies that the mask image associated is a luminance mask.

  • alpha: It is a keyword that signifies that the mask image associated is an alpha mask. The transparency (alpha channel) values of the mask layer image are used as the mask values. Alpha value or alpha channel specifies to the opacity of the <color>.

Applies to

Applied to all the <mask> elements.

Syntax

mask-type = luminance | alpha;

CSS mask-type - alpha Value

The following example demonstrates the use of the CSS property mask-type, where a mask element with black and transparent areas is used. The element's id (#mask) is used, which places the mask on the <div> element. As the mask has black and transparent areas, the mask-type is set to alpha.

<html>
<head>
<style>
    div {
      width: 150px;
      height: 150px;
      background-color: purple;
      mask-image: url(#mask);
   }    

   mask {
      mask-type: alpha; 
   }
</style>
</head>
<body>
   <div></div>
   <svg>
      <mask id="mask">
         <path fill="black" d="M10,35 A20,20,0,0,1,50,35 A20,20,0,0,1,90,35 Q90,65,50,95 Q10,65,10,35 Z" />
      </mask>    
   </svg></body>
</html>

CSS mask-type - Luminance Value

The following example demonstrates the use of the CSS property mask-type with value as luminance.

<html>
<head>
<style>
   body {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100%;
      margin: 0;
      background-color: darkgrey;
      overflow: hidden;
   }

   .container {
      display: grid;
      position: relative;
      padding: 20px;
      border: 2px solid black;
   }

   .box {
      margin: 20px;
      display: block;
      width: 200px;
      height: 150px;
      background-image: url(images/border.png);
      mask-type: luminance;
   }

   .overlay {
      position: absolute;
      width: 100%;
      height: 100%;
      background-color: rgb(0, 201, 0, 0.2);
   }
</style>
</head>
<body>
   <div class="container">
      <div class="box"></div>
      <div class="overlay"></div>
   </div>
</body>
</html>

In the above example:

  • The .box class represents the element with the background image that will be masked.

  • The .overlay class is used to add a light green overlay to the masked area for better visibility.

Advertisements