Effects
mask-size
mask-size sets how large a mask image is drawn. It takes the same values as background-size: give a width and height directly, or use cover and contain to fit the element.
Syntax
<bg-size>#
<bg-size> = [ <length-percentage [0,∞]> | auto ]{1,2} | cover | containPreview
Presets
Drag the bottom-right corner to resize
Code
.box {
background: linear-gradient(
135deg,
#88d1c7,
#77a7d7,
#eaac3a
);
mask-image: url(/mask-star.svg);
mask-repeat: no-repeat;
mask-position: center;
mask-size: 50%;
}Controls
mask-size50%
width
height
About
cover and contain
coverkeeps the aspect ratio and picks the smallest size that covers the element with no gaps. Whatever overflows is unused.containkeeps the aspect ratio and picks the largest size that fits the whole image. In the leftover space, the image repeats whenmask-repeatis the initialrepeat, and the element disappears withno-repeat.
Width and height
A single value sets the width and leaves the height auto, keeping the aspect ratio. Two values can distort the image. Percentages are relative to the area the mask is positioned in, which by default reaches the outer edge of the border (mask-origin: border-box), unlike backgrounds, which measure up to the padding. The initial value is auto, the image’s natural size.
Watch out
- Where the mask image doesn’t reach, the element disappears. With a background image the background color would show there; with a mask nothing is painted.
mask-repeatstarts asrepeat, so a small size tiles the image. To cut out a single icon shape, pair it withno-repeat.- Gradients have no natural size, so
autofills the whole area. - Inside the
maskshorthand, the size follows the position after a/(mask: url(star.svg) center / contain no-repeat). - Chrome and Edge support the name without
-webkit-from version 120, and Safari from 15.4. For older browsers, write-webkit-mask-size. - The preview here masks the box with an image of a star on a transparent background (96×96), placed once in the center.