# Clip-Path & Masking

> Cut elements into custom shapes or fade them out smoothly using alpha channels.

Subject: CSS | Lesson 35 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/clip-path-and-masking
Previous: [Filters & Blend Modes](https://codewithanimation.in/css/filters-and-blend-modes.md) | Next: [Scroll Snap & Smooth Scroll](https://codewithanimation.in/css/scroll-snap-and-behavior.md)

## What are clipping and masking?

Clipping and masking are two different ways to hide parts of an element without actually removing it from the DOM.

**Clipping** (`clip-path`) uses vector math to draw a sharp, hard-edged boundary. Anything outside the shape is completely invisible.

**Masking** (`mask-image`) uses the alpha channel (transparency) of an image or a CSS gradient. This allows for soft, feathered edges and partial transparency.

## Why not just use a transparent PNG?

If you want a hexagonal image, you could edit it in Photoshop and save it as a PNG. But if you use `clip-path: polygon(...)` in CSS, you can apply that exact same hexagon shape to a lightweight JPEG, a video, or an entire `<div>` full of text!

Furthermore, clipping paths are fully animatable. You can smoothly transition a square into a circle just by changing the CSS.

## How do you use them?

To clip: use `clip-path: circle(50% at 50% 50%)` to draw a circle in the center of the element, or `clip-path: polygon(...)` for custom jagged shapes.

To mask: use `mask-image: linear-gradient(to bottom, black 50%, transparent 100%)`. The parts where the gradient is solid black will be fully visible, and the parts where it is transparent will fade away.

## Common mistake: Pointer events trap

With `clip-path`, the hidden parts of the element physically stop receiving mouse events (clicks pass right through them to whatever is underneath!). But with `mask-image`, the hidden parts are visually gone but *still receive clicks*, which can trap users.

## Tip: Vendor prefixes

Masking is still notoriously reliant on vendor prefixes in modern browsers. Always write `-webkit-mask-image` directly alongside standard `mask-image` to ensure it works across Chrome, Safari, and Edge.

## Try it: Try it

Toggle between a vector Clip-Path and an alpha Mask-Image to see the difference between sharp geometry and soft gradients.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/clip-path-and-masking

## Check yourself

### Question 1

If you need a soft, feathered fade-out at the bottom of a scrolling text box, which property must you use?

- A. clip-path
- B. mask-image
- C. overflow: hidden

Correct answer: B

Only `mask-image` respects alpha channels (like a transparent gradient), which is required to create a soft fade.

- Why not A: clip-path can only create sharp, pixel-perfect cuts. It cannot do soft fades.
- Why not C: This just cuts off the content sharply at the border box.

### Question 2

How does clip-path interact with mouse clicks (pointer-events)?

- A. The entire original bounding box still catches clicks.
- B. Only the visible parts inside the clipped shape catch clicks.
- C. clip-path disables pointer events completely.

Correct answer: B

Because `clip-path` is a geometric cut, browsers optimize it by entirely dropping pointer events for the clipped-away regions, allowing you to click the elements beneath.

- Why not A: This is how mask-image behaves, but not clip-path.
- Why not C: It just restricts them to the visible shape, it doesn't disable them entirely.

### Question 3

Which of the following is a valid clip-path function?

- A. clip-path: polygon(50% 0%, 100% 100%, 0% 100%);
- B. clip-path: square(50px);
- C. clip-path: linear-gradient(black, transparent);

Correct answer: A

`polygon()` accepts a comma-separated list of X/Y coordinates to draw any complex shape. The example draws a perfect triangle.

- Why not B: There is no square() function, use inset() or polygon() for squares.
- Why not C: Gradients are used for mask-image, not clip-path.

## Remember this

- clip-path = sharp edges, disables clicks on hidden parts, fast performance.
- mask-image = soft edges, catches clicks on hidden parts, heavier performance.
- Always include -webkit-mask-image alongside mask-image.
- Use clip-path: circle() or ellipse() to quickly round profile pictures.
