Skip to main content

CSS Effects

Clipping & Masking

Written by Updated Clipping & Masking exercises

Control visible regions, alpha masks, and the contour around which inline content flows. The examples distinguish visible clipping, alpha masking, basic shapes, SVG paths, and shape-outside for text wrapping.

Three ways to break out of the rectangle. clip-path cuts an element to a shape with a hard edge. A mask fades parts of it in and out. shape-outside makes text wrap around a curve instead of a box.

None of them change the space the element takes in the layout; they only change what you see, and in the case of shape-outside, how text flows around it. Gradients make good masks, so CSS Gradients is useful background. Related visual effects are in CSS Filters and Layered Backgrounds.

Cut an element to a shape

Example

css

.portrait {
  clip-path: circle(45% at 50% 42%);
}

.tag {
  clip-path: polygon(0 0, calc(100% - 1rem) 0, 100% 50%,
    calc(100% - 1rem) 100%, 0 100%);
}

The portrait is cut to a circle, centered a little above the middle. The tag gets a pointed right end from a polygon: each pair of numbers is one corner, measured from the top-left. Anything outside the shape is invisible and cannot be clicked, but the element still takes up its full rectangle in the layout.

Fade with a mask

Example

css

.fade {
  mask-image: linear-gradient(to bottom, black 70%, transparent);
}

.icon {
  background: currentColor;
  mask: url("/samples/star.svg") center / contain no-repeat;
}

A mask uses an image or gradient as a stencil: where the mask is solid, the element shows; where it is transparent, the element disappears. The first rule fades the bottom 30% of a text block away, a common "read more" effect. The second turns any SVG shape into an icon that takes the text color, because the background is currentColor and the star decides where it shows.

Wrap text around a curve

Example

css

.figure {
  float: inline-start;
  shape-outside: circle(50%);
  clip-path: circle(50%);
  margin-inline-end: 1.5rem;
}

shape-outside only works on floated elements. Here the floated image is round, and text flows along its curve instead of around a square box. The clip-path makes the image itself look round to match.

Shape and Mask Properties

PropertyPurposeExample
clip-pathClips painting to a basic shape or path.clip-path: circle(45%)
mask-imageUses an image's alpha or luminance as a mask.mask-image: linear-gradient(...)
mask-sizeSets the rendered size of mask layers.mask-size: cover
shape-outsideDefines the contour around a float.shape-outside: circle(50%)

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Clipping & Masking example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

.portrait {
  clip-path: circle(45% at 50% 42%);
}

.tag {
  clip-path: polygon(0 0, calc(100% - 1rem) 0, 100% 50%,
    calc(100% - 1rem) 100%, 0 100%);
}

.fade {
  mask-image: linear-gradient(to bottom, black 70%, transparent);
}

.icon {
  background: currentColor;
  mask: url("/samples/star.svg") center / contain no-repeat;
}

.figure {
  float: inline-start;
  shape-outside: circle(50%);
  clip-path: circle(50%);
  margin-inline-end: 1.5rem;
}
  </style>
</head>
<body>
  <main><div class="portrait" style="width:180px;height:180px;background:linear-gradient(135deg,#06b6d4,#7c3aed)"></div><p class="tag">Clipped tag shape</p><div class="icon" style="width:80px;height:80px;color:#d97706"></div></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
clip-path55793.59.1
mask shorthand, unprefixed1201205315.4

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Good to know

  • Find polygon coordinates with a tool. Firefox DevTools has a visual clip-path editor, and online generators make it easy.
  • Masks hide content visually, not from screen readers. Do not mask away text people need, and keep long text readable when zoomed.

Clipping & Masking exercises

A circle

Cut the card to a circle with clip-path circle(50%).

The HTML your code works on
<div class="card">Card</div>
<h2 class="title">Title</h2>

Result

Check your answer to see it here.