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

mask-position

Definition

The mask-position CSS property specifies the position of a mask image relative to the element it is applied to. It determines where the mask image is placed within the element's content box, and controls 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-position property to center, which means the mask image is centered within the element's content box:

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

In this example, we apply a mask image to a div element and set the mask-position property to 50% 50%, which means the mask image is centered within the element's content box:

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

In this example, we apply a mask image to a div element and set the mask-position property to top left, which means the mask image is positioned at the top left corner of the element's content box:

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

Values

ValueDescription
<length>Specifies a position in pixels, percentages, or other length units.
leftPositions the mask image on the left edge of the element's content box.
rightPositions the mask image on the right edge of the element's content box.
centerCenters the mask image within the element's content box.
topPositions the mask image at the top edge of the element's content box.
bottomPositions the mask image at the bottom edge of the element's content box.
x% y%Specifies a position as a percentage of the element's content box width and height.
x ySpecifies a position as an offset from the top left corner of the element's content box.

Best Practices

  • Choose the appropriate value for your use case: The mask-position property allows you to position the mask image within the element's content box. Choose the appropriate value based on your use case and the effect you are trying to achieve.
  • Test in different browsers: The mask-position 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-position 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. Consider the performance implications of using the mask-position property, and optimize your code accordingly.
  • Use fallbacks for unsupported browsers: If you are using the mask-position 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