π What Is mask-image?
The mask-image property allows you to apply a mask layer to an element. A mask controls which parts of an element are visible and which parts are hidden, based on the alpha (transparency) or luminance (brightness) of the mask image.
This enables advanced cutout effects, custom shapes, gradients, and artistic UI designs. π¨β¨
π§© Basic Syntax
Syntax
mask-image: url(mask.png);
mask-image: linear-gradient(to bottom, transparent, black);β Multiple masks can be used with commas
β Works similar to background-image, but controls visibility instead of pixels.
π― How Masks Work (Important!)
- Black β fully transparent (hidden)
- White β fully visible
- Gray β partially visible
Note
π§ͺ Example: Mask an Image With a Shape
Circle mask
.photo {
mask-image: url(circle-mask.png);
mask-size: cover;
mask-repeat: no-repeat;
}β Only parts of the image inside the circle remain visible.
β¨ Using Gradients as Masks
Gradient fade mask
.fade-bottom {
mask-image: linear-gradient(to bottom, black 70%, transparent);
}β Creates a smooth fade-out effect at the bottom of an element.
π¨ Masking With Multiple Layers
Multiple masks
.shape {
mask-image:
radial-gradient(circle, white 60%, transparent),
linear-gradient(90deg, white, transparent);
}β Masks stack similarly to multiple backgrounds.
π§ Additional Mask Properties
- mask-size β controls scaling (like background-size)
- mask-position β where to place the mask
- mask-repeat β repeat or tile the mask
- mask-mode β alpha or luminance masking
- mask-origin β border-box / padding-box / content-box
- mask-composite β how multiple masks combine
π₯ Example: Text Reveal With mask-image
Text masked by gradient
.reveal {
font-size: 4rem;
background: url(poster.jpg);
-webkit-background-clip: text;
color: transparent;
mask-image: linear-gradient(to right, transparent, black, transparent);
}β Beautiful βspotlightβ text reveal effect.
π¬ Masking SVG Shapes
SVG mask
.logo {
mask-image: url(logo-mask.svg);
mask-size: contain;
}β Clean edges and scalable shapes thanks to vector masking.
π Example: Highlight With Mask Movement
Animated mask
@keyframes sweep {
to { mask-position: 100% 0; }
}
.highlight {
mask-image: linear-gradient(90deg, transparent, white, transparent);
mask-size: 200% 100%;
animation: sweep 2s infinite linear;
}β Creates a shimmering loading or highlighting animation.
π₯ Advanced: mask-image + mix-blend-mode
Combined effect
.shine {
mask-image: url(mask-shape.png);
mix-blend-mode: screen;
opacity: 0.8;
}β Excellent for futuristic UI cards or glowing stickers.
β οΈ Browser Support Notes
- β Chrome
- β Safari
- β Edge
- β οΈ Firefox (partial support, uses -webkit-mask-image)
Note
π Debugging Tips
- If the element disappears β your mask is fully black or fully transparent
- Try switching gradient direction if mask hides wrong part
- Use mask-repeat: no-repeat to avoid unexpected tiling
- Use mask-position to align the mask art correctly