Every element on a web page is a box. clip-path cuts what you see of that box into another shape: a triangle, a diamond, a circle. The part of the box outside the shape is not shown. Think of paper cut along a pattern: the box is the paper, the shape is the pattern, and the paper outside the pattern is thrown away.
Cut a triangle with polygon()
This rule cuts a box into a triangle:
.triangle {
width: 200px;
height: 200px;
background: coral;
clip-path: polygon(50% 0%, 100% 100%, 0% 100%);
}
clip-path:is the property that does the cutting.polygon(...)is the function for any shape you like. You write the corner points of the shape. The browser joins the points with lines, then joins the last point back to the first.- Each point is written
x y, separated by a space.xis the distance from the left side of the box, andyis the distance from the top. Both are percentages of the box's size. - One point and the next are separated by a comma. There are three points here:
50% 0%(top middle),100% 100%(bottom right corner),0% 100%(bottom left corner).
The result is a coral triangle with its tip at the top middle. A few other points for reference: 0% 0% is the top left corner, 100% 0% is the top right corner, and 50% 50% is the middle of the box.
A diamond: four points in the middle of the sides
A diamond has four corners. Each corner is in the middle of one side of the box. Write the four points in order around the box, clockwise:
.diamond {
clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
}
The order is: top middle 50% 0%, right middle 100% 50%, bottom middle 50% 100%, then left middle 0% 50%. The order matters, because the browser joins the points in the order you write them. If the points jump from place to place, the lines cross each other.
Circles, ellipses and inset()
For round shapes and for boxes with their edges cut off, there are ready-made functions:
clip-path: circle(50%);makes a circle. The number is the radius (the distance from the centre of the circle to its edge). On a square box,circle(50%)exactly touches all four sides of the box, so the corners of the box are cut off. A bigger number gives a bigger circle.clip-path: circle(50% at center);gives the same result. The wordatsets where the centre of the circle is, here the middle of the box. Withoutat, the centre is also the middle of the box.clip-path: ellipse(40% 60% at center);makes an ellipse (an oval). The first number is the horizontal radius, and the second is the vertical radius.clip-path: inset(10px 20px round 8px);cuts inward from the edges of the box. The numbers are read like the numbers ofmargin: four numbers go top first, then clockwise. With two numbers, as here, 10px is cut from the top and bottom, and 20px from the left and right. The wordroundrounds the corners, here by 8px.
Only the look is cut
An element that has been cut still keeps the space of its full box, so the other elements don't move. But clicks and hovers follow the cut shape: the part that was cut away can't be clicked or hovered. So if a box is cut into a circle, a click on a corner that is gone does not reach the box.
⚠️ When the cut is not what you planned
- Points without commas.
polygon(50% 0% 100% 100% 0% 100%)is not valid, so the wholeclip-pathdeclaration is ignored and the box stays whole. How to write it: a space betweenxandy, a comma between two points. - Shadows get cut too. Any
box-shadoworborderoutside the shape is cut away too. If you need a shadow, putfilter: drop-shadow(...)on the parent of the element you cut. - No size. The percentages in
polygon()are calculated from the element's size. An empty element with nowidthorheightcan be 0 tall, so nothing shows.
TL;DR: clip-path cuts what you see of an element into another shape, and the part outside the shape is not shown. polygon() builds any shape from corner points. Each point is written x y (percent from the left and from the top), points are separated by commas, and you write them in order around the shape. circle() makes a circle, ellipse() makes an oval, and inset() cuts a box inward from its edges. The element keeps the space of its full box, but the part that was cut away can't be clicked.
⚡ Cut a triangle
Write:
.triangle {
width: 200px;
height: 200px;
background: coral;
clip-path: polygon(50% 0%, 100% 100%, 0% 100%);
}
Guess first: the code above cuts a box into a triangle, from three points separated by commas. If you remove the commas, polygon(50% 0% 100% 100% 0% 100%), what shape does the box get?
The box stays whole. Without commas the value is not valid, so the browser ignores the whole clip-path declaration.
Meaning: clip-path cuts what you see of an element into another shape. polygon() joins corner points into one shape. Each point is written x y as percentages: x from the left, y from the top. Points are separated by commas. 50% 0% is the top middle.
⚡ Ready-made round shapes
Write:
clip-path: circle(50%);
Meaning: circle() makes a circle, and the number is the radius (the distance from the centre to the edge of the circle). ellipse() makes an oval, and inset() cuts inward from the edges of the box.
⚡ Only the look is cut
Guess first: a box is cut into a circle. Can you still click a corner that is gone?
No. The part that was cut away can't be clicked or hovered.
Meaning: The cut element still keeps the space of its full box, so the elements around it don't move.