CSS Masking
Masking hides parts of an element using an image or gradient as a stencil. Where the mask is opaque, the element shows; where the mask is transparent, the element disappears. Its sibling, clipping (clip-path), cuts an element to a geometric shape with hard edges.
| Technique | Property | Edges | Input |
|---|---|---|---|
| Clipping | clip-path |
Hard | Shapes: circle(), ellipse(), polygon(), inset(), path() |
| Masking | mask-image |
Hard or soft | Any image or gradient (uses its alpha channel) |
clip-path shapes
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; flex-wrap: wrap; gap: 14px; }
figure { margin: 0; text-align: center; font-size: 13px; }
.shape { width: 100px; height: 100px; background: linear-gradient(135deg, #ff4081, #7c4dff); }
.circle { clip-path: circle(50%); }
.ellipse { clip-path: ellipse(50% 30% at 50% 50%); }
.tri { clip-path: polygon(50% 0, 100% 100%, 0 100%); }
.star { clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%); }
.inset { clip-path: inset(15px round 20px); }
</style>
</head>
<body>
<figure><div class="shape circle"></div>circle()</figure>
<figure><div class="shape ellipse"></div>ellipse()</figure>
<figure><div class="shape tri"></div>polygon()</figure>
<figure><div class="shape star"></div>star polygon</figure>
<figure><div class="shape inset"></div>inset()</figure>
</body>
</html>Polygon points are x y pairs, measured from the top-left corner. Clip paths can also be transitioned between shapes with the same number of points.
<!DOCTYPE html>
<html>
<head>
<style>
body { display: grid; place-items: center; height: 240px; margin: 0; font-family: system-ui, sans-serif; }
.morph {
width: 160px; height: 160px; background: conic-gradient(#ffc107, #ff5722, #e91e63, #ffc107);
clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
transition: clip-path .5s;
}
.morph:hover { clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%); }
</style>
</head>
<body>
<div class="morph" title="hover me"></div>
</body>
</html>mask-image with gradients
A gradient mask fades content out smoothly. Black (or any opaque colour) means "show", transparent means "hide".
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Georgia, serif; display: flex; gap: 16px; }
.text { width: 200px; height: 200px; overflow: hidden; background: #fff8e1; padding: 10px; line-height: 1.5;
-webkit-mask-image: linear-gradient(to bottom, black 50%, transparent);
mask-image: linear-gradient(to bottom, black 50%, transparent); }
.spot { width: 200px; height: 200px; background: repeating-linear-gradient(45deg, #1e88e5 0 12px, #90caf9 12px 24px);
-webkit-mask-image: radial-gradient(circle, black 40%, transparent 70%);
mask-image: radial-gradient(circle, black 40%, transparent 70%); }
</style>
</head>
<body>
<div class="text">This long paragraph fades away towards the bottom of the box instead of ending abruptly. It is a nice way to hint that there is more content to read if the user expands the card. The text keeps going and going.</div>
<div class="spot"></div>
</body>
</html>Tip: Add the
-webkit-mask-*prefixed versions alongside the standard ones; some browsers still need them.
Masking with an SVG image
Any image with transparency can be a mask. Here an SVG heart (as a data URI) stencils a gradient background, so the result is a multicoloured heart.
<!DOCTYPE html>
<html>
<head>
<style>
body { display: flex; gap: 20px; align-items: center; padding: 20px; }
.heart {
width: 140px; height: 140px;
background: linear-gradient(45deg, #f50057, #ff9100, #ffea00);
-webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 21s-7-4.5-9.5-9A5.5 5.5 0 0 1 12 6a5.5 5.5 0 0 1 9.5 6C19 16.5 12 21 12 21z'/%3E%3C/svg%3E") center / contain no-repeat;
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 21s-7-4.5-9.5-9A5.5 5.5 0 0 1 12 6a5.5 5.5 0 0 1 9.5 6C19 16.5 12 21 12 21z'/%3E%3C/svg%3E") center / contain no-repeat;
transition: transform .2s;
}
.heart:hover { transform: scale(1.1); }
.small { width: 60px; height: 60px; background: #00bfa5; }
</style>
</head>
<body>
<div class="heart"></div>
<div class="heart small"></div>
</body>
</html>This technique is a popular way to make recolourable icons: change the background and the icon changes colour.
Mask properties
| Property | Meaning |
|---|---|
mask-image |
The image or gradient used as the mask |
mask-size |
Like background-size (contain, cover, lengths) |
mask-position |
Where the mask sits |
mask-repeat |
repeat, no-repeat, … |
mask-mode |
alpha (default for images) or luminance |
mask |
Shorthand for all of the above |
Test yourself
In a CSS mask, what happens where the mask image is transparent?
- The element shows normally
- The element is hidden there
- The element turns black
- Nothing changes
Which clip-path function would you use for a triangle?
- circle()
- inset()
- polygon()
- ellipse()
What is a key difference between clip-path and mask-image?
- clip-path can make soft, faded edges
- mask-image can use gradients for soft edges; clip-path has hard edges
- They are identical
- mask-image only works on text