๐ What Is mask-size?
The mask-size property controls how large the mask image appears when applied to an element. It works similarly to background-size, determining how the mask graphic scales and covers the element's box.
With mask-size, you can scale your stencil, shape, gradient, or pattern to achieve the exact masking effect you want. ๐ญโจ
๐งฉ Basic Syntax
Basic Syntax
mask-size: auto | <length> | <percentage> | cover | contain;
mask-size: <width> <height>;
mask-size: 50px 80px;
mask-size: 100% 50%;โ You can define one or two values
โ Applied similarly to background-size
โ Supports keywords like cover and contain
๐ฏ All mask-size Values Explained
| Value | Description |
|---|---|
| auto | Uses intrinsic mask image size |
| 50px, 30% | Exact width or scaled by percentage |
| 50px 80px | Custom width and height |
| cover | Mask covers entire element (may crop) |
| contain | Mask fits inside element without cropping |
๐งช Example: Default Size
auto size
.shape {
mask-image: url(mask-heart.png);
mask-size: auto;
}โ Mask displays at its natural image size.
๐งช Example: Scaling the Mask
Scaled mask
.icon {
mask-image: url(star.svg);
mask-size: 40px;
}โ Mask scales to 40px width (height auto-adjusts).
๐งช Example: Setting Width & Height
Custom width & height
.stencil {
mask-image: url(shape.png);
mask-size: 100px 150px;
}โ Perfect for custom stencil proportions.
๐งช Example: Using Percentages
Percentage size
.hero {
mask-image: url(mask-overlay.png);
mask-size: 100% 60%;
}โ Mask fills full width and 60% of the element's height.
๐จ Using cover and contain
โ cover
cover example
.cover-mask {
mask-image: url(mask-shape.png);
mask-size: cover;
}โ Mask fills and covers the entire area (cropping possible).
โ contain
contain example
.contain-mask {
mask-image: url(mask-shape.png);
mask-size: contain;
}โ Mask fits fully inside the element (no cropping).
๐งช Example: Tile Pattern Mask with mask-repeat
Pattern tiling
.pattern {
mask-image: url(tile.png);
mask-size: 40px 40px;
mask-repeat: repeat;
}โ Creates custom patterned masks.
๐ฅ Example: Animated Mask Scaling
Animated mask-size
@keyframes pulseMask {
0% { mask-size: 80px; }
100% { mask-size: 120px; }
}
.pulse {
mask-image: radial-gradient(circle, white 50%, transparent);
animation: pulseMask 1s infinite alternate;
}โ Produces a pulsing spotlight reveal effect.
๐ Advanced Example: mask-size + mask-position
Moving + scaling spotlight
@keyframes spotlight {
0% {
mask-size: 20% 20%;
mask-position: 0% 50%;
}
100% {
mask-size: 40% 40%;
mask-position: 100% 50%;
}
}
.reveal {
mask-image: radial-gradient(circle, white 60%, transparent);
animation: spotlight 2s infinite alternate;
}โ Dynamic spotlight sweep animation.
โ Great for loaders, reveal cards, and transitions.
๐ ๏ธ Best Practices
- Use auto when you want original mask proportions
- Use cover for full-section masking effects
- Use contain to preserve mask shape
- Combine with mask-position for perfect alignment
- For tiling patterns, pair with mask-repeat
- Avoid extremely large mask-size values (performance impact)
โ ๏ธ Browser Support
- โ Chrome
- โ Safari
- โ Edge
- โ ๏ธ Firefox requires -webkit-mask-size
Note
๐ Debugging Tips
- If mask looks stretched โ try contain instead of cover
- If mask is too small โ use pixel or percentage values
- If tiling breaks โ check mask-repeat
- Preview mask scaling using DevTools