SVG paths: reading and writing the d attribute

Learn the SVG path commands (M, L, C, Q, A, Z), absolute versus relative coordinates, and how arc flags work, with live editable examples.

The <path> element is the general-purpose shape in SVG. MDN states that all the basic shapes (<rect>, <circle>, <ellipse>, <line>, <polyline> and <polygon>) can be created with a path. It has two attributes of its own: d, which holds the shape, and pathLength, a number that specifies the total length of the path in user units. Almost all the interest is in d.

The d attribute is a tiny drawing language

The d attribute is a string of commands. Each command is a letter followed by numbers. Think of a pen on paper: move the pen, draw a line, draw a curve, close the shape.

<path d="M 20 100 L 60 20 L 100 100 Z" fill="#1f3fe0"/>

This reads: move to (20, 100), draw a line to (60, 20), draw a line to (100, 100), close the path. The Z draws a straight line back to the start of the subpath.

Edit the numbers below. Move a coordinate and the triangle changes shape.

The command set

MDN lists these commands. Uppercase letters use absolute coordinates, and lowercase letters use coordinates relative to the current point.

Command Name Parameters
M / m Move to x y
L / l Line to x y
H / h Horizontal line x
V / v Vertical line y
C / c Cubic Bézier x1 y1 x2 y2 x y
S / s Smooth cubic Bézier x2 y2 x y
Q / q Quadratic Bézier x1 y1 x y
T / t Smooth quadratic Bézier x y
A / a Elliptical arc rx ry angle large-arc sweep x y
Z / z Close path none

A few details from MDN that save debugging time:

  • M moves the current point without drawing. If you give extra coordinate pairs after an M, they are treated as implicit L commands.
  • Commands are case-sensitive. MDN’s example: M 10,10 l 5,5 starts at absolute (10, 10), then draws a line 5 units right and 5 down.
  • Relative negative x values move left, and relative negative y values move up.
  • MDN’s examples write coordinate pairs both ways, as M 10,30 and as M 10 30. Use whichever reads better and stay consistent.

Absolute or relative

Absolute commands are easier to read and edit by hand. Relative commands make a shape easy to move, because only the first M carries a position. Relative commands also tend to produce shorter strings, which matters for file size. See optimization.

The two paths below draw the same square. The second uses relative commands and starts at a different point, so you can shift it by changing only the first two numbers.

Curves

SVG has two kinds of Bézier curve. A quadratic (Q) has one control point. A cubic (C) has two, which gives finer control. The control points are not on the curve. They pull it toward them, like magnets.

The smooth variants save typing. MDN explains that S takes its first control point as the reflection of the previous curve’s second control point, so two cubic segments join without a kink. If the previous command was not a cubic curve, that control point is the current point. T does the same thing for quadratic curves.

In the playground, the dashed lines show the control points of a cubic curve. Look at the numbers after C and change 150 10 and 200 110 to reshape it.

If you are drawing in a design tool, the exported d string will mostly be C commands. Tools produce them from the Bézier handles you drag.

Arcs and their flags

The arc command is the one people avoid, because it has seven parameters: A rx ry angle large-arc-flag sweep-flag x y. MDN describes them as follows:

  • rx, ry: the two radii of the ellipse.
  • angle: rotation of the ellipse in degrees, relative to the x axis.
  • large-arc-flag: 1 chooses the large arc, 0 the small arc.
  • sweep-flag: 1 draws clockwise, 0 counterclockwise.
  • x y: the end point. The center of the ellipse is calculated for you.

Given a start point, an end point and two radii, there are four arcs that fit. The two flags choose which one you get. Keep that in mind when an arc “goes the wrong way”: flip the sweep flag first, then the large-arc flag.

Below, all four combinations connect the same two points.

Fills, strokes and open paths

A path can be open or closed. Z closes it by drawing a straight line back to the start of the subpath. Leave it off for an open shape such as a curve or a squiggle. Use stroke-linecap and stroke-linejoin to control line ends and corners.

Working with paths in practice

  • Draw in a tool, tidy in code. Vector editors export correct but verbose paths. Open the file and remove what you do not need.
  • Learn to read paths. You rarely write long ones by hand, but being able to spot an M in the middle (a new subpath) or a missing Z fixes many bugs.
  • Use the right shape. For a plain rectangle or circle, <rect> and <circle> are clearer than a path. Reach for <path> when the outline is irregular.
  • Reuse with transforms. Move, scale or rotate a path with transforms instead of recomputing coordinates.
  • Animation. MDN lists d as animatable, so a path can change over time. See animation.

When not to use a path

A path is the wrong tool for a large amount of body text, because you lose selectable, searchable and translatable characters. Use <text> and keep real characters. For photographs and textures, see SVG vs PNG and JPEG. To trim or hide parts of a drawing without editing its outline, see masks and clipping.

To place paths precisely in a layout, you will need the viewBox.

Sources

Checked against these references on 2026-11-21.