The fourth entry in this series on the CSS shape() function covers the two remaining commands: close and move. Both are simple in practice and likely to see little use, but each solves a problem that is otherwise awkward to express in a shape() definition.
Closing the path, implicitly or explicitly
Earlier parts established that a shape() always begins with a from command to set the first point. The matching convention at the other end is a close command. None of the shapes drawn so far needed one, because the browser handles the final segment on its own. A triangle written with two line commands still renders as a triangle; the edge connecting 100% 100% back to 0 0 is implicit.
clip-path: shape(from 0 0, line to 100% 0, line to 100% 100%)
Adding an explicit closing line produces the same result, and so does appending close:
clip-path: shape(from 0 0, line to 100% 0, line to 100% 100%, close)
Since the browser closes the shape unaided, neither extra command is required here. That makes close look redundant, and for most shapes it is — three prior articles were written without it.
The command earns its place mid-definition. Consider a shape that starts at the center and then builds four triangles, returning to the center between each one. Rather than writing a line back to that point every time, a single close sends the path to the initial point automatically:
clip-path: shape(from 0 0, line to 100% 0, line to 100% 100%, line to 0 0)
Beyond the shorter code, the behavior is dynamic: if the center value changes, the close command follows it. That makes it a worthwhile optimization for many shapes.
Drawing disconnected parts with move
Shapes are usually a single continuous outline, but occasionally they consist of separate, unconnected pieces. The move command handles that case. Its syntax resembles from, except it appears partway through a definition rather than at the start.
Take four triangles that do not touch. It would be reasonable to assume four elements are needed, each with its own shape(). In fact the whole arrangement is one shape: after closing a triangle, a move repositions the start point and the next triangle is drawn from there.
clip-path: shape(
from center,
line to 20% 0, hline by 60%, line to center, /* triangle 1 */
line to 100% 20%, vline by 60%, line to center, /* triangle 2 */
line to 20% 100%, hline by 60%, line to center, /* triangle 3 */
line to 0 20%, vline by 60% /* triangle 4 */
)
The close before each move is optional, so the pattern can be trimmed:
clip-path: shape(
from center,
line to 20% 0, hline by 60%, close,
line to 100% 20%, vline by 60%, close,
line to 20% 100%, hline by 60%, close,
line to 0 20%, vline by 60%
)
Two use cases worth knowing
Inverting a shape
A known clip-path: polygon() technique inverts a polygon to produce a cut-out. The same effect is achievable in shape() by combining the main shape with a rectangle covering the element's boundaries. Two shapes means move is required.
clip-path: shape(
from 50% 40%, line to 20% 0, hline by 60%, close, /* triangle 1 */
move to 60% 50%, line to 100% 20%, vline by 60%, close, /* triangle 2 */
move to 50% 60%, line to 20% 100%, hline by 60%, close, /* triangle 3 */
move to 40% 50%, line to 0 20%, vline by 60% /* triangle 4 */
)
Draw the main shape, move to 0 0, then draw the rectangle. A CSS variable can switch between the normal and inverted versions: with --i undefined, var(--i,) is empty and the main shape renders; assign it the rectangle and the cut-out appears.
clip-path: shape(
from X1 Y1, ..., close, /* shape 1 */
move to X2 Y2, ..., close, /* shape 2 */
...
move to Xn Yn, ... /* shape N */
)
With a rounded hexagon, one adjustment is sometimes necessary: an evenodd keyword at the beginning of shape(). Without it, the inverted shape can fail to be visible in some cases.
clip-path: shape(
from X1 Y1, ..., /* shape 1 */
move to X2 Y2, ..., /* shape 2 */
...
move to Xn Yn, ... /* shape N */
)
Spacing can also be controlled. Setting the reference box to content-box plus some padding shrinks the visible area, since padding and border are excluded. Rewriting the rectangle to re-include the padded area restores full coverage, which is why the --i expression is verbose. This gives a variable-driven way to make a shape smaller without touching the shape's own geometry, demonstrated with the CSS-Tricks logo. The same example is available in the SVG-to-CSS converter, which outputs the shape() code for you.
Repetition without duplication
The by directive is what makes repetition cheap: it sets coordinates relative to the previous point, so a shape built entirely from by can be reused verbatim at any X Y origin. A simple circle illustrates the idea:
.shape {
clip-path: shape(from ...., move to 0 0, hline to 100%, vline to 100%, hline to 0);
}
From X Y, an arc moves upward by 50px, then a second arc returns to X Y using the same offset downward. Whatever X Y is, the circle is identical, just repositioned.
.shape {
clip-path: shape(from .... var(--i,));
}
.invert {
--i:,move to 0 0, hline to 100%, vline to 100%, hline to 0;
}
Because nothing varies but the origin, the shape can live in a custom property and be stamped out as many times as needed:
.shape {
clip-path: shape(evenodd from .... var(--i,));
}
.invert {
--i:,move to 0 0, hline to 100%, vline to 100%, hline to 0;
}
Swap the --sh value for any other shape and the pattern still holds; the demos include three different shapes drawn this way, plus an inverted version produced by appending the rectangle. There is no duplicated code and every parameter is adjustable through CSS variables — something path() cannot do, since it does not accept variables.
A three-dot loader built with the same technique rounds out the examples, and shows how far the approach stretches once animation is added.



