CSS Image Masking lets you control which parts of an element are visible by applying a mask image or gradient. The mask defines a shape:
Masking is perfect for creative effects like circular crops, text reveals, and complex shapes without actually editing the original image.
mask-image — defines the image or gradient used as the mask.mask-repeat — controls repetition of the mask (similar to background-repeat).mask-position — controls where the mask is placed.mask-size — controls how big the mask is.-webkit-mask-* — WebKit-prefixed versions for better browser support (Safari, some Chromium browsers).The simplest way to apply a mask is using an external image file (PNG/SVG with transparency):
/* Apply a custom shape mask to an image element */
img.profile-photo {
mask-image: url("mask-shape.png");
mask-repeat: no-repeat;
mask-position: center;
mask-size: contain;
-webkit-mask-image: url("mask-shape.png");
-webkit-mask-repeat: no-repeat;
-webkit-mask-position: center;
-webkit-mask-size: contain;
}
You can also use gradients instead of external images to build smooth, dynamic masking shapes directly in CSS.
/* Fade an image from left (visible) to right (transparent) */
.banner {
mask-image: linear-gradient(to right, black 40%, transparent 100%);
mask-repeat: no-repeat;
-webkit-mask-image: linear-gradient(to right, black 40%, transparent 100%);
-webkit-mask-repeat: no-repeat;
}
These are the core properties you will use when working with CSS masking:
| Property | Description |
|---|---|
mask-image |
Defines the mask shape/image (PNG, SVG, gradient, etc.). |
mask-repeat |
Controls how the mask repeats (no-repeat, repeat, repeat-x, repeat-y). |
mask-position |
Sets the position of the mask inside the element (center, top, etc.). |
mask-size |
Controls the size of the mask (cover, contain, or custom values). |
mask-composite |
Controls how multiple masks blend with each other. |
This example uses a radial-gradient mask to reveal only a circular area in the center of the image.
Only the center circular region is fully visible. The area outside the circle gradually fades to transparent because of the gradient. The actual image is still full-sized — only its visibility is being controlled.
/* Circular spotlight mask using radial-gradient */
.masked-photo {
width: 100%;
display: block;
mask-image: radial-gradient(circle at center, black 0, black 40%, transparent 60%);
-webkit-mask-image: radial-gradient(circle at center, black 0, black 40%, transparent 60%);
mask-repeat: no-repeat;
-webkit-mask-repeat: no-repeat;
mask-position: center;
-webkit-mask-position: center;
mask-size: cover;
-webkit-mask-size: cover;
}
mask-image behaves similarly to background-image:
For better support, especially in Safari, always include the -webkit-mask-* versions of the properties.
linear-gradient and radial-gradient are great for soft edges and fades.mask-* and -webkit-mask-* for cross-browser support.mask-composite to combine multiple mask layers creatively.radial-gradient. Make the edges soft by fading to transparent.mask-image on any photo.mask-image with mask-repeat: repeat-x;.mask-position and mask-size to move and scale your mask over the same image.-webkit-mask-image to fix any compatibility issues.