On this page10 sections
  1. Learning Objectives
  2. 1. Gradient Masks
  3. 2. Fading the End of Scrolling Text
  4. 3. Shape Masks From SVG
  5. 4. Coloured Icons From One SVG
  6. Mask vs clip-path
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 042: Masking

Category7: Advanced Styling
Lecture042 of 057
Estimated video length8 min
TargetCSS3 · any modern browser
LevelIntermediate

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-repeat and mask-position
  • Know how masks differ from clip-path

1. Gradient Masks

Example

html
<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

html
<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:

html
<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

html
<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-pathmask
EdgesHardCan be soft (gradients)
ShapesBasic shapes and polygonsAny image, SVG or gradient
Partial transparencyNoYes

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

  1. In a mask, what does a transparent area do?
  2. Fade an image out towards the bottom.
  3. Which properties size and position a mask image?
  4. What can a mask do that clip-path can't?
  5. How do you colour an SVG icon with a mask?

Practice Answers

  1. It hides that part of the element.
  2. mask-image: linear-gradient(black 50%, transparent);
  3. mask-size and mask-position (plus mask-repeat).
  4. Create soft edges and partial transparency.
  5. Use the SVG as the mask and set the element's background to 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-repeat and mask-position mirror the background properties.
  • Mask + background colour = recolourable icons.