1. css
  2. /properties
  3. /mask-origin

mask-origin

Definition

The mask-origin CSS property specifies the origin position of a mask image. It defines where the mask image is positioned relative to the element it is applied to, and determines which part of the mask image is used to mask the element.

Examples

In this example, we apply a mask image to a div element and set the mask-origin property to border-box, which means the mask image is positioned relative to the border box of the element:

<div class="masked"></div>
.masked {
  width: 200px;
  height: 200px;
  background-color: red;
  mask-image: url('mask-image.png');
  mask-origin: border-box;
}

In this example, we apply a mask image to a div element and set the mask-origin property to content-box, which means the mask image is positioned relative to the content box of the element:

<div class="masked"></div>
.masked {
  width: 200px;
  height: 200px;
  background-color: red;
  mask-image: url('mask-image.png');
  mask-origin: content-box;
}

In this example, we apply a mask image to a div element and set the mask-origin property to padding-box, which means the mask image is positioned relative to the padding box of the element:

<div class="masked"></div>
.masked {
  width: 200px;
  height: 200px;
  background-color: red;
  mask-image: url('mask-image.png');
  mask-origin: padding-box;
}

Values

ValueDescription
padding-boxThe mask image is positioned relative to the padding box of the element.
border-boxThe mask image is positioned relative to the border box of the element.
content-boxThe mask image is positioned relative to the content box of the element.
fill-boxThe mask image is scaled to fill the element's padding box and is positioned relative to the padding box of the element. This value is only available in SVG content.
stroke-boxThe mask image is scaled to fill the element's padding box and is positioned relative to the padding box of the element. This value is only available in SVG content.
view-boxThe mask image is positioned relative to the SVG viewport. This value is only available in SVG content.

Best Practices

  • Choose the appropriate value for your use case: The mask-origin property allows you to position the mask image relative to different boxes of the element. Choose the appropriate value based on your use case and the effect you are trying to achieve.
  • Test in different browsers: The mask-origin property is supported in most modern browsers, but it's still a good idea to test your code in different browsers to ensure cross-browser compatibility.
  • Use a high-quality mask image: The effectiveness of the mask image depends on its quality. Make sure the mask image is high-quality, with sharp edges and good contrast.
  • Experiment with different values: The mask-origin property is just one of many properties you can use to achieve masking effects. Experiment with different values to achieve the effect you want.
  • Consider performance: Masking can be computationally expensive, especially when using large images or complex masks.
  • Use fallbacks for unsupported browsers: If you are using the mask-origin property and other masking properties, make sure to provide fallbacks for browsers that do not support these features. One approach is to use a solid color as a fallback.

Browser Compatibility

ChromeFirefoxSafariInternet ExplorerMicrosoft EdgeOpera
YesYesYesNoYesYes