日本語

Clipping Elements into Curved Shapes with CSS shape()

​​CSS​HTML,Playwright

Writing the shape() function in clip-path clips an element into shapes with curves, such as waves and arches. You can write the coordinates with % or calc(), so the shape stretches and shrinks with the element's width. With path(), which takes an SVG path string, the coordinates are fixed in px, so this is not possible.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened pages with only HTML and CSS in Playwright and checked the shapes using screenshot pixels and getComputedStyle() values. I did not test the display in Firefox, Safari, or on real smartphones. The browser versions that support each feature are the values from the compatibility data (@mdn/browser-compat-data 8.1.8).

How to write shape()

In shape(), you first write the starting point with from, then list the commands that draw lines, separated by commas. Coordinates are written as "x y", and the top-left corner of the element is 0 0. % is a percentage of the element's width and height.

css
.triangle {
  clip-path: shape(from 0 0, line to 100% 0, line to 50% 100%, close);
}

In this example, a line is drawn from the top-left to the top-right, then a line is drawn to the middle of the bottom edge, and close returns to the starting point. The element is clipped to a triangle that connects both ends of the top edge and the middle of the bottom edge. shape() can be written not only in clip-path but also in offset-path, which moves an element along a line.

The available commands are listed below. Each command starts from the point where the previous command ended.

Command Line drawn
line to x y Straight line to that point
hline to x, vline to y Horizontal-only or vertical-only straight line
curve to x y with control-point Bézier curve
smooth to x y Bézier curve that joins smoothly with the previous curve
arc to x y of radius Part of a circle or ellipse (arc)
move to x y Moves to a point without drawing a line
close Returns in a straight line to the starting point

If you change to to by, you can write the end point as the distance to move from the current point. You cannot omit the commas between commands. In the test environment, when I checked a value without the comma after from 0 0 with CSS.supports(), it returned false.

Making the bottom edge a wave with curve and smooth

Here, I make the bottom edge of a heading into a wave shape.

css
.hero {
  clip-path: shape(
    from 0 0,
    hline to 100%,
    vline to calc(100% - 30px),
    curve to 50% calc(100% - 30px) with 75% 100%,
    smooth to 0 calc(100% - 30px),
    close
  );
}
The top image is a wide light-blue band with a Japanese label meaning "news". Its bottom edge goes from right to left as one trough and one crest. The bottom image is a tall light-blue frame with a Japanese label meaning "entrance". Its top edge is a semicircular arch.
The wave and arch from the CSS in this article, shown in Chromium. The arch is 160px wide, 220px high, with an 80px radius.

A horizontal line is drawn from the top-left to the top-right, and the right edge is drawn down to 30px above the bottom. From there, curve draws a curve to the middle of the bottom edge. The 75% 100% after with is the control point that sets the shape of the curve. The curve does not pass through the control point, but it is pulled toward it, so it bulges downward.

If you write one control point after with, you get a quadratic Bézier curve. If you write two separated by /, you get a cubic Bézier curve.

The following smooth draws a curve that joins smoothly with the previous curve, without you writing a control point. The CSS Shapes specification says that the control point of the previous curve is reflected to the opposite side around the current point. In this example, 75% 100% reflected around the middle of the bottom edge becomes 25% calc(100% - 60px). As a result, a crest on the left half is created with the same size as the trough on the right half.

A diagram of the bottom edge wave of the light-blue band, with dashed lines and points drawn on top. Black dots are at the right end, the middle, and the left end. Below the middle dot is a label meaning "end point of the first segment". Below the right-side trough is a black square labeled "control point of curve (75% 100%)". Above the left-side crest is a pink square labeled "point that smooth uses automatically". Dashed lines run from each square to the black dots on both sides.
A 368px-wide, 200px-high element with a wave shape, with the points that set the curve drawn over it. The points and dashed lines were added by hand for the image.

In the test environment, I applied this to a 600×200px element and measured the position of the bottom edge. The two ends and the middle were 169px from the top, the trough on the right was 184px, and the crest on the left was 154px. I also compared it with the shape written as curve to 0 calc(100% - 30px) with 25% calc(100% - 60px) instead of smooth. The bottom edge positions were identical at all nine points I measured.

Even when I changed the height to 120px, the depth of the wave did not change. The two ends were 89px, the trough was 104px, and the crest was 74px, so the wave still went 15px up and down. This is because the wave is drawn within the 30px band above the bottom edge, using calc(100% - 30px).

Making the top edge an arch with arc

To draw part of a circle, use arc. Write the radius after of.

css
.arch {
  width: 200px;
  height: 300px;
  clip-path: shape(
    from 0 100%,
    vline to 100px,
    arc to 100% 100px of 100px cw,
    vline to 100%,
    close
  );
}

From the bottom-left, a line is drawn up the left edge to 100px from the top, and then an arc with a 100px radius is drawn to the same height on the right edge. In the test environment, the top edge was at 0px in the middle column and at 47px in the column 15px from the left and right ends. The top becomes a rounded arch.

You specify the direction of the arc with cw (clockwise) or ccw (counterclockwise). If you omit it, ccw is used. In the example above, when I changed cw to ccw or omitted it, the arc bulged downward, and the middle column was blank from the top down to 200px.

Two tall frames surrounded by dotted lines. The left one, with cw, has a top edge bulged into a semicircle, forming an arch. The right one, with ccw (the default when omitted), has a semicircle cut out from the top, so only the bottom remains light blue.
A 160px-wide, 200px-high element with cw and ccw arcs. The dotted lines show the range of the element.

If the radius is too small to connect the two points, the radius is enlarged until the points can be connected. The MDN page for shape() also describes this behavior. On a 400×100px element, when I connected two points 200px apart horizontally with an arc of radius 60px, the depth of the arc was 99px. That is the same depth as a semicircle with a 100px radius.

Notes when writing the radius as one % value

If you write the radius as a single % value, such as of 50%, it is not a percentage of the element's width. The CSS Shapes specification says that, as with circle(), it is a percentage of the diagonal length divided by √2 (sqrt(width² + height²) / sqrt(2)).

For a 400×100px element, this size is about 291.55px, so 50% of it is about 145.77px. If you connect two points 200px apart horizontally with this radius, the calculated depth of the arc is 39.7px. In the test environment, the measured depth was 39px. If the radius were 50% of the width (200px), the calculated depth would be 26.8px, which does not match the measured value.

If you write two values, such as of 50% 50%, the horizontal radius is 50% of the width and the vertical radius is 50% of the height. For the same two points, the depth of the arc was a shallow 6px.

The difference from path() is that it scales with the element's size

Curved shapes could already be written with path(). path() takes the same string as the d attribute of SVG, and its coordinates are in px.

css
/* Wave written for a 300px-wide, 150px-high element */
.wave-path {
  clip-path: path("M 0 0 H 300 V 120 Q 225 150 150 120 T 0 120 Z");
}

In the test environment, I applied this path() and the shape() wave from the previous section to elements of the same size and compared them. For a 300px-wide, 150px-high element, the bottom edge positions were identical at all five points I measured.

When I changed the width to 500px, a difference appeared. The shape() wave spread across the full width, but the path() shape kept its size for a 300px width, and nothing was displayed in the columns at 375px and 499px from the left.

Two bands stacked vertically. The top one, labeled "path() written for 240px width", is cut off at about two-thirds of the way across, and the right side is white. The bottom one, labeled "shape() written with %", has the wave band spread across the full width.
A 368px-wide element with a path() written for 240px width and a shape() written with %, each applied to the element.

MDN lists the advantage of shape() as being writable in the usual CSS syntax. You can use units such as %, rem, and em, and math functions such as calc(), max(), and abs(). path() is a single string, and its units are limited to px. If you want to clip an element whose width is not fixed with a curve, shape() is the better choice.

Conditions for animating a shape

You can change the shape() shape gradually with transition. In the following example, when .on is added, the straight bottom edge bulges downward.

css
.box {
  transition: clip-path 1s linear;
  clip-path: shape(from 0 0, hline to 100%, vline to 70%,
    curve to 0 70% with 50% 70%, close);
}

.box.on {
  clip-path: shape(from 0 0, hline to 100%, vline to 70%,
    curve to 0 70% with 50% 100%, close);
}

In the test environment, after adding .on, I stopped the animation at 500ms and read the value with getComputedStyle(). The control point was 50% 85%, which is exactly halfway between 70% and 100%.

For the intermediate shapes to be computed, the number, type, and order of commands in the two shapes must match. The interpolation section of the CSS Shapes specification also requires that the difference between to and by, and the number of control points for curve and smooth, match. In the test environment, between a triangle (two line commands) and a quadrilateral (three line commands), and between shapes where line to and line by were swapped, no intermediate values were produced, and the final shape was already in place at 50%.

Between path() and shape(), intermediate shapes are also produced if the commands match. In the test environment, the intermediate values were returned in shape() form, with coordinates such as calc(50% + 100px), which mix px and %.

Browser support

According to the compatibility data, shape() is supported from the following versions.

Browser Support
Chrome, Edge From 135
Chrome (Android) From 135
Firefox From 148
Safari From 18.4
Safari (iOS) From 18.4

For browsers that do not support it, you can provide a different shape using @supports.

css
.hero {
  clip-path: polygon(0 0, 100% 0, 100% 85%, 0 100%); /* Shape with straight edges only */
}

@supports (clip-path: shape(from 0 0, line to 100% 0)) {
  .hero {
    clip-path: shape(
      from 0 0,
      hline to 100%,
      vline to calc(100% - 30px),
      curve to 50% calc(100% - 30px) with 75% 100%,
      smooth to 0 calc(100% - 30px),
      close
    );
  }
}

In Chromium 153 in the test environment, the shape() value inside @supports was applied.

​Posted by 86