Reading history

CSS clip-path Shapes

5 min read 0 views

Learn CSS clip-path: circle, ellipse, inset and polygon shapes, slanted banners, tag shapes and animating clip paths.

Every HTML element is a rectangle. With clip-path, it does not have to look like one. This property cuts an element to a shape, such as a circle, a triangle, a hexagon or a slanted banner, and hides everything outside it. It needs no images and works with gradients, photos and text. It can even be animated. This lesson covers the basic shape functions and some practical designs.

In this lesson you will learn

  • What clipping does to an element
  • circle(), ellipse(), inset() and polygon()
  • Reading polygon points as percentages
  • Slanted sections and tag shapes in real designs
  • Animating clip-path with transitions

You should know backgrounds and gradients and transitions.

The Shape Functions

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>clip-path shapes</title>
  <style>
    body { font-family: sans-serif; margin: 12px; font-size: 12px; }
    .row { display: flex; gap: 14px; flex-wrap: wrap; }
    .shape { width: 90px; height: 90px; background: linear-gradient(135deg, #ff7043, #ab47bc);
             display: grid; place-items: center; color: white; font-weight: bold; }
    figure { margin: 0; text-align: center; }
    .circle   { clip-path: circle(50%); }
    .ellipse  { clip-path: ellipse(50% 35%); }
    .triangle { clip-path: polygon(50% 0, 100% 100%, 0 100%); }
    .hexagon  { clip-path: polygon(25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%, 0 50%); }
    .inset    { clip-path: inset(10px round 16px); }
  </style>
</head>
<body>
  <div class="row">
    <figure><div class="shape circle">A</div>circle(50%)</figure>
    <figure><div class="shape ellipse">B</div>ellipse()</figure>
    <figure><div class="shape triangle">C</div>polygon (3 points)</figure>
    <figure><div class="shape hexagon">D</div>polygon (6 points)</figure>
    <figure><div class="shape inset">E</div>inset(... round)</figure>
  </div>
</body>
</html>

Result

  • circle(50%) keeps a circle whose radius is half the box. It is centred unless you add a position: circle(40% at 30% 50%).
  • ellipse(50% 35%) takes two radii: horizontal and vertical.
  • polygon() takes a list of points, each written as x y. Percentages are measured from the top-left corner: 0 0 is top-left, 100% 100% is bottom-right.
  • The triangle uses three points: top-middle 50% 0, bottom-right 100% 100% and bottom-left 0 100%.
  • inset(10px round 16px) trims 10px from every side and rounds the corners.

Clipping only changes what you see and where clicks land. The element still takes up its full rectangle in the layout, so nearby elements do not move closer.

Shapes in a Real Design

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>clip-path in a design</title>
  <style>
    body { font-family: sans-serif; margin: 0; font-size: 14px; }
    /* A hero banner with a slanted bottom edge */
    .hero {
      background: linear-gradient(120deg, #00897b, #1e88e5);
      color: white; padding: 22px 20px 50px;
      clip-path: polygon(0 0, 100% 0, 100% 75%, 0 100%);
    }
    .hero h1 { margin: 0 0 4px; font-size: 22px; }
    /* A badge shaped like a tag, and a hover animation on the clip */
    .tags { display: flex; gap: 10px; padding: 4px 20px; }
    .tag {
      background: #ffca28; padding: 6px 18px 6px 12px;
      clip-path: polygon(0 0, 85% 0, 100% 50%, 85% 100%, 0 100%);
      transition: clip-path 0.3s;
    }
    .tag:hover { clip-path: polygon(0 0, 100% 0, 100% 50%, 100% 100%, 0 100%); }
  </style>
</head>
<body>
  <header class="hero">
    <h1>Learn web design</h1>
    <p>Free lessons in HTML and CSS</p>
  </header>
  <div class="tags"><span class="tag">HTML</span><span class="tag">CSS</span><span class="tag">Hover me</span></div>
</body>
</html>

Result

  • The hero banner keeps the full top edge, but its bottom-right point is at 100% 75%. That makes a slanted lower edge, a popular look for landing pages.
  • Extra bottom padding stops the text from being cut off by the slant.
  • Each tag is a polygon with a point on the right, like a price label.
  • A clip-path can be animated when both shapes are polygons with the same number of points. Hover over a tag: its point flattens out smoothly.

Plan shapes on paper first

Sketch the shape in a square, label each corner with its percentage position, then list the points clockwise. Online “clip-path maker” tools also let you drag points and copy the CSS.

Common Mistakes

  1. Clipping away text or focus outlines. Leave enough padding, and check keyboard focus styles are still visible.
  2. Animating between polygons with a different number of points. The browser cannot blend them, so the change happens all at once.
  3. Expecting box-shadow to follow the shape. Shadows are clipped too. Use filter: drop-shadow() on a wrapper element instead.
  4. Forgetting commas between polygon points. Write polygon(50% 0, 100% 100%, 0 100%).

Practice Exercises

Exercise 1. Write a clip-path for a diamond (a square turned 45°).

Show answer
.diamond {
  clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
}

The four points are the middle of each side: top, right, bottom and left.

Exercise 2. Make a section whose top edge is slanted, rising from left to right.

Show answer
.section {
  clip-path: polygon(0 20%, 100% 0, 100% 100%, 0 100%);
}

The top-left point is lowered to 20%, while the top-right point stays at the very top.

Frequently Asked Questions

Can I clip an image into a circle?

Yes. clip-path: circle(50%) works on images. For simple round avatars, border-radius: 50% on a square image gives the same result and is more widely known.

Can clip-path use a custom SVG shape?

Yes. You can use clip-path: url(#shape) to point to an SVG <clipPath> element, or path("...") with SVG path data, for curves that polygons cannot draw.

Summary

  • clip-path hides everything outside a shape; the layout box stays the same.
  • circle(), ellipse(), inset() and polygon() cover most needs.
  • Polygon points are x y pairs measured from the top-left.
  • Polygons with the same number of points can be animated smoothly.

Next: CSS Web Typography.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *