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

mask-repeat

Definition

The mask-repeat CSS property is used to control how a mask image is repeated when it is applied to an element. It specifies whether the mask image should be repeated, and if so, how it should be repeated.

Examples

In this example, we apply a mask image to a div element and set the mask-repeat property to repeat, which means the mask image is repeated both horizontally and vertically to cover the entire element:

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

In this example, we apply a mask image to a div element and set the mask-repeat property to no-repeat, which means the mask image is not repeated and is only displayed once within the element:

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

In this example, we apply a mask image to a div element and set the mask-repeat property to repeat-x, which means the mask image is repeated only horizontally to cover the entire width of the element:

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

Values

ValueDescription
repeatRepeats the mask image both horizontally and vertically to cover the entire element.
no-repeatDisplays the mask image once, without repetition.
repeat-xRepeats the mask image horizontally to cover the entire width of the element.
repeat-yRepeats the mask image vertically to cover the entire height of the element.
spaceRepeats the mask image both horizontally and vertically, but spaces the images so that the edges of adjacent images touch.
roundRepeats the mask image both horizontally and vertically, but scales the images so that they fill the entire element without being stretched.

Best Practices

  • Choose the appropriate value for your use case: The mask-repeat property allows you to control how the mask image is repeated. Choose the appropriate value based on your use case and the effect you are trying to achieve.
  • Test in different browsers: The mask-repeat 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-repeat 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-repeat property, and optimize your code accordingly.
  • Use fallbacks for unsupported browsers: If you are using the mask-repeat 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