Skip to content
Tutorials Center
My learning

Topics

Lesson 56 of 139 ยท Backgrounds and Effects

Clip Paths and Masks

Cut elements into circles, polygons and custom shapes with clip-path, then fade and shape them with mask-image, gradients and SVG icons that you can recolor.

Every element is a rectangle, but it does not have to look like one. Rounded corners get you circles and pills. For anything else, such as a hexagon avatar, a slanted hero section, an arrow-shaped badge or an image that fades out at the bottom, CSS gives you two tools.

clip-path cuts the element along a shape: everything inside the shape is visible, everything outside is gone. It is a hard edge, like scissors. mask-image uses an image, often a gradient, to decide how visible each pixel is, so it can create soft fades as well as hard shapes. In this lesson you will use both, and learn when to pick which.

clip-path with basic shapes

clip-path accepts a shape function. These are the ones you will use most:

Function Example Shape
inset() inset(10px 20px round 12px) A rectangle shrunk in from each edge, optionally rounded.
circle() circle(40% at 50% 50%) A circle with a radius and a center point.
ellipse() ellipse(50% 35% at center) An ellipse with two radii and a center.
polygon() polygon(50% 0, 100% 100%, 0 100%) Any shape with straight edges, listed as x y points.

Points and positions are measured from the top-left corner of the element's border box, so 0 0 is the top-left, 100% 0 the top-right and 100% 100% the bottom-right. Percentages make the shape scale with the element.

Basic clip-path shapes

Change the numbers in any shape and watch it update. Firefox and Chrome DevTools can even show the polygon points as handles you can drag.

The slanted section

A popular use of polygon() is a hero or banner with a slanted bottom edge. Only one corner moves:

CSS
.hero {
  clip-path: polygon(0 0, 100% 0, 100% 85%, 0 100%);
}

The right side ends at 85% of the height and the left at 100%, so the bottom edge slopes. Leave enough padding at the bottom that no text sits in the clipped corner.

What clipping really removes

Clipping is more than a visual effect:

  • Clipped areas are not clickable. Pointer events only hit the visible part of the element, so a circular clipped button reacts only inside the circle.
  • Borders, outlines and box shadows are clipped too. A box-shadow sits outside the shape, so it disappears. If you need a shadow, put filter: drop-shadow() on a parent of the clipped element; the filter runs on the parent's rendered result, after the child was clipped.
  • Layout is unchanged. The element still takes up its full rectangle. Clipping only changes what is drawn.
  • It creates a stacking context, like many visual effects. More on that in z-index and Stacking Contexts.

Animating clip-path

Two shapes of the same type can be animated into each other, as long as polygons have the same number of points. That makes reveal effects easy:

A circle reveal on hover

The radius grows from 14% to 75% while the center stays fixed, so the image seems to flood out of the small circle. A good habit: respect users who prefer less motion with the prefers-reduced-motion media query, covered in Motion and Performance.

Paths, SVG and newer shapes

Straight edges and circles cover most needs. For curves, you have a few more options:

  • clip-path: path("M 0 0 L 100 0 ...") takes SVG path data. It works everywhere, but the coordinates are in pixels only, so the shape does not scale with the element.
  • clip-path: url(#my-clip) points to an SVG <clipPath> element in the page. With clipPathUnits="objectBoundingBox" the coordinates run from 0 to 1 and the shape scales.
  • clip-path: shape(...) is a newer function with path-like commands that accept percentages and other CSS units. It is supported in recent Chrome, Edge and Safari; Firefox support is still arriving, so treat it as an enhancement.

You can also use a reference box keyword such as clip-path: margin-box or combine it with a shape, like circle(50%) content-box, to measure the shape from a different box than the default border box.

mask-image: soft edges and fades

A mask uses an image as a stencil. Wherever the mask is opaque, the element shows; wherever it is transparent, the element is hidden; in between, it is partly visible. Only the mask's alpha (transparency) matters by default, not its colors.

That means a gradient from black to transparent makes a perfect fade:

Fading an image and a scroll list with masks

The image is fully visible down to the middle, then fades out. The list fades over 20 pixels at each end. Because the mask makes pixels truly transparent, the page behind shows through. That beats faking a fade with a white gradient overlay, which only works on a white background.

mask-image accepts any gradient: try radial-gradient(circle, black 40%, transparent 70%) on the image for a vignette.

Mask properties

Masks have a family of properties that mirror the background properties you already know:

Property Like Example
mask-image background-image url(icon.svg), a gradient, or several layers
mask-size background-size contain, cover, 24px 24px
mask-repeat background-repeat no-repeat, repeat-x
mask-position background-position center, right 10px top
mask-mode โ€” alpha (default for images) or luminance
mask background The shorthand for all of the above

mask-mode: luminance uses brightness instead of transparency: white shows, black hides. That is handy when your mask image is an opaque black-and-white picture.

Recoloring icons with masks

Masks have one more superpower. If you put an SVG icon in mask-image and give the element a background-color, the icon takes on that color. Change the color on hover, use currentColor, or theme it with a custom property, all without touching the SVG:

One icon, any color

The SVG is inlined as a data: URL here. In a real project you can point to a file, such as mask: url("/icons/heart.svg") .... Note that browsers fetch mask images with CORS rules, so icons from another domain need the right headers.

Prefixes and support. clip-path with basic shapes works unprefixed in all current browsers. The unprefixed mask properties arrived in Chrome and Edge in version 120 (late 2023); older Chromium browsers only know -webkit-mask-image and friends. Writing both forms, as in these examples, covers everyone.

clip-path or mask?

  • Use clip-path for hard-edged geometric shapes: polygons, circles, slanted sections. It is simple, fast and animates well.
  • Use mask-image for soft edges and fades, for shapes that come from an image or icon, and whenever you need partial transparency.

Both hide content visually only. Hidden text is still read by screen readers, and the element keeps its full space in the layout.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which clip-path value creates a triangle pointing up?

    The polygon has three points, top center, bottom right and bottom left, which make an upward triangle.
  2. 2. What happens to the box-shadow of an element with a clip-path?

    Everything outside the clip shape is hidden, including shadows. Put filter: drop-shadow() on a parent instead.
  3. 3. With the default mask mode, which part of a mask-image decides visibility?

    By default, opaque parts of the mask show the element and transparent parts hide it. Use mask-mode: luminance to use brightness instead.
  4. 4. You want a long article preview to fade into the page, whatever the page color. What is the best tool?

    A mask makes the bottom truly transparent, so it works on any background. The overlay only matches one color, and clip-path cuts with a hard edge.

Summary

  • clip-path cuts an element along a shape: inset(), circle(), ellipse(), polygon(), path(), an SVG url(), or the newer shape().
  • Clipped areas are invisible and not clickable, shadows get clipped too, and the layout space stays the same.
  • Shapes of the same type animate smoothly, which makes reveal effects easy.
  • mask-image uses an image's transparency as a stencil; gradients give soft fades, and SVG icons plus background-color give recolorable icons.
  • The mask properties mirror the background ones; write the -webkit- versions too for older browsers.

Next: The display Property โ€” start the layout chapter with the property that decides how every box behaves.