← Back to HTML & CSS, page 4

html-css-0075 · Medium mid

How do clip-path and mask-image shape an element, and how do they affect clicks?

clip-path cuts an element to a shape such as a circle or polygon with sharp edges, and mask-image shows or hides parts using an image or gradient, so it can fade. Clipped areas are not clickable, and shadows on the clipped element are cut off.

Full answer

clip-path cuts an element to a shape. Everything outside the shape is not drawn. Basic shapes are circle(), ellipse(), inset() (a rectangle with optional rounded corners) and polygon(), which takes a list of points as percentages or lengths. There is also path() for curves.

mask-image hides parts of an element by a picture, usually a gradient or an image with transparency. Where the mask is opaque the element shows, and where it is transparent the element is hidden, with smooth steps in between. A fade-out at the bottom of a card is one line: mask-image: linear-gradient(black, transparent).

The main difference: clip-path makes sharp edges and a fixed shape, while mask-image can fade, because it uses the transparency of an image.

Hit testing follows clip-path. The clipped-away area does not receive mouse clicks or hover, so a circle-clipped button is only clickable inside the circle. A shadow on the same element is clipped as well, so put box-shadow on a wrapper.

Older browsers may need the -webkit- prefix for mask. Check the support table for your audience.

The first shape is a circle. The second is a triangle with its tip at the top centre. The blue block fades from fully visible at the top to invisible at the bottom. The teal square is placed under the circle's top-left corner. Because the circle's corner is clipped away, a click there reaches the teal square, not the circle.

For responsive shapes, use percentages, and for complex outlines, create the shape in an SVG and reuse its path.

html
<style>
  body { margin: 0; }
  .shape { width: 100px; height: 100px; background: tomato; margin: 8px; display: inline-block; }
  .circle { clip-path: circle(50%); }
  .triangle { clip-path: polygon(50% 0, 100% 100%, 0 100%); }
  .fade {
    width: 200px; height: 80px; background: #0a6cff;
    -webkit-mask-image: linear-gradient(black, transparent);
    mask-image: linear-gradient(black, transparent);
  }
  .under { position: absolute; top: 8px; left: 8px; width: 100px; height: 100px; background: teal; }
</style>
<div class="under" id="under"></div>
<div class="shape circle" id="circle"></div>
<div class="shape triangle" id="tri"></div>
<div class="fade" id="fade"></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • clip-path uses circle(), inset(), polygon() and path() for hard edges.
  • mask-image uses an image or gradient and can fade.
  • Clipped-away areas do not receive clicks or hover.
  • Put box-shadow on a wrapper of a clipped element.
  • Check whether the -webkit- prefix is needed for mask in your target browsers.

Common mistakes

  • Putting a box-shadow on the element that has clip-path, so the shadow is cut off. Put the shadow on a wrapper.
  • Expecting a clipped-away area to still be clickable. It is not.
  • Using fixed pixel points in polygon() for shapes that must scale with the box.

Follow-ups

  • How do you add a shadow to a clipped shape?
  • How would you fade the bottom of a long text block with CSS only?

Tips

  • Contrast the two: clip-path is a hard shape, mask-image can fade.
  • Mention hit testing. Few candidates know the clipped area is not clickable.