On this page10 sections
Lecture 042: Masking
| Category | 7: Advanced Styling |
| Lecture | 042 of 057 |
| Estimated video length | 8 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
A mask decides how much of an element shows at each point. Where the mask is solid, the element is visible; where it's transparent, the element disappears. With gradients that means soft fades; with images or SVG, any shape you like.
Learning Objectives
- Fade elements out with gradient masks
- Shape elements with image and SVG masks
- Control masks with
mask-size,mask-repeatandmask-position - Know how masks differ from
clip-path
1. Gradient Masks
Example
<style>
.fade-bottom { mask-image: linear-gradient(black 50%, transparent); }
.fade-sides { mask-image: linear-gradient(to right, transparent, black 20%, black 80%, transparent); }
.spot { mask-image: radial-gradient(circle, black 40%, transparent 70%); }
img { width: 200px; margin: 4px; }
</style>
<img class="fade-bottom" src="https://picsum.photos/id/1018/400/260" alt="Mountain lake">
<img class="fade-sides" src="https://picsum.photos/id/1018/400/260" alt="Mountain lake">
<img class="spot" src="https://picsum.photos/id/1018/400/260" alt="Mountain lake">Black (or any solid colour) = visible; transparent = hidden. Only the mask's transparency matters, not its colour.
2. Fading the End of Scrolling Text
Example
<div style="height: 110px; overflow: auto; padding: 8px; border: 1px solid #dee2e6;
mask-image: linear-gradient(black 70%, transparent);">
<p>Line one of a long list.</p><p>Line two.</p><p>Line three.</p><p>Line four.</p><p>Line five.</p><p>Line six.</p>
</div>The fade hints that there's more to scroll.
3. Shape Masks From SVG
Example: a star-shaped photo:
<style>
.star {
width: 180px; height: 180px; object-fit: cover;
mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpolygon points='12,1 15,9 23,9 17,14 19,22 12,17 5,22 7,14 1,9 9,9'/%3E%3C/svg%3E");
mask-size: contain;
mask-repeat: no-repeat;
mask-position: center;
}
</style>
<img class="star" src="https://picsum.photos/id/1080/360/360" alt="Strawberries">mask-size, mask-repeat and mask-position work just like their background- equivalents.
4. Coloured Icons From One SVG
A mask plus a background colour lets one SVG icon take any colour:
Example
<style>
.icon { display: inline-block; width: 40px; height: 40px; margin: 4px;
mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 21 3 12Q0 9 1.5 5.5 4 1 8.5 2.5 11 3.5 12 6 13 3.5 15.5 2.5 20 1 22.5 5.5 24 9 21 12Z'/%3E%3C/svg%3E") center / contain no-repeat; }
</style>
<span class="icon" style="background: #e03131;"></span>
<span class="icon" style="background: #1971c2;"></span>
<span class="icon" style="background: linear-gradient(#f76707, #ae3ec9);"></span>Mask vs clip-path
clip-path | mask | |
|---|---|---|
| Edges | Hard | Can be soft (gradients) |
| Shapes | Basic shapes and polygons | Any image, SVG or gradient |
| Partial transparency | No | Yes |
Common Beginner Mistakes
1. Thinking mask colours matter. Only transparency does: a red mask works the same as a black one.
2. Forgetting mask-repeat: no-repeat. The shape tiles across the element.
3. Very old browsers. They needed the -webkit-mask-* prefix; current browsers don't.
Practice Questions
- In a mask, what does a transparent area do?
- Fade an image out towards the bottom.
- Which properties size and position a mask image?
- What can a mask do that
clip-pathcan't? - How do you colour an SVG icon with a mask?
Practice Answers
- It hides that part of the element.
mask-image: linear-gradient(black 50%, transparent);mask-sizeandmask-position(plusmask-repeat).- Create soft edges and partial transparency.
- Use the SVG as the
maskand set the element'sbackgroundto the colour you want.
Key Takeaways
- Masks show an element where the mask is solid and hide it where it's transparent.
- Gradient masks give soft fades; SVG or image masks give shapes.
mask-size,mask-repeatandmask-positionmirror the background properties.- Mask +
backgroundcolour = recolourable icons.