HTML Reference Β· SVG
SVG shape elements
Reference for the SVG shape elements rect, circle, ellipse, line, polyline, polygon and path: every geometry attribute, the path command table and examples.
SVG draws with seven shape elements. Six of them are the basic shapes, each described by a few numbers: <rect>, <circle>, <ellipse>, <line>, <polyline> and <polygon>. The seventh, <path>, can draw anything the others can, plus curves and arcs, from a compact list of drawing commands.
Every shape goes inside an <svg> element (directly or inside a group), uses the coordinate system set by its viewBox, and is painted with fill and stroke (see SVG presentation attributes).
<svg viewBox="0 0 120 60" width="240"> <rect x="5" y="5" width="50" height="50" rx="8" fill="#38bdf8"/> <circle cx="90" cy="30" r="25" fill="#f472b6"/> </svg>
Try it
Shapes are painted in source order, so later shapes cover earlier ones: the post is drawn first and the sign hides its top end.
Basic shapes and their attributes
All geometry values are numbers in user units (the units of the viewBox). Percentages are allowed for most of them and refer to the viewport size.
| Element | Attribute | Default | Description |
|---|---|---|---|
<rect> |
x, y |
0 |
The top-left corner. |
width, height |
auto (0) |
The size. A zero or missing width or height draws nothing. | |
rx, ry |
auto |
Corner radii. If only one is set, the other uses the same value. Values larger than half the width or height are clamped. | |
<circle> |
cx, cy |
0 |
The center. |
r |
0 |
The radius. 0 draws nothing. |
|
<ellipse> |
cx, cy |
0 |
The center. |
rx, ry |
auto |
Horizontal and vertical radius. In SVG 2, auto copies the other radius. |
|
<line> |
x1, y1, x2, y2 |
0 |
Start and end point. A line has no inside, so it is only visible with a stroke. |
<polyline> |
points |
(none) | A list of x,y pairs, separated by spaces or commas: "10,10 50,40 90,10". The shape stays open. |
<polygon> |
points |
(none) | Like <polyline>, but the last point is joined back to the first. |
<path> |
d |
(none) | The path data: a list of commands (see the next section). |
| All shapes | pathLength |
(none) | The author's own total length for the outline. Stroke dashes and marker positions are then measured in these units. |
cx, cy, r, rx, ry, x, y, width and height are geometry properties in SVG 2, so you can also set them in CSS (circle { r: 20px; }) and animate them with transitions. The matching CSS d property for paths is not supported in every browser yet.
The third rectangle sets only rx="30", so ry is 30 as well. That is half of its 60-unit height, so its ends become full semicircles: a pill. The left polyline has no fill, so the browser fills it with the default black between its first and last point. Add fill="none" to every open shape that should only be a line.
Path commands
The d attribute of <path> is a series of commands. Each command is a letter followed by numbers. An uppercase letter uses absolute coordinates; a lowercase letter is relative to the current point (where the previous command ended). Commas and spaces between numbers are optional where the meaning is clear (M10 20 and M 10,20 are the same).
| Command | Parameters | Draws |
|---|---|---|
M / m |
x y |
Move to. Lifts the pen and starts a new subpath. Extra pairs after it are treated as L/l. Every path starts with M or m. |
L / l |
x y |
Line to a point. |
H / h |
x |
Horizontal line to a new x. |
V / v |
y |
Vertical line to a new y. |
C / c |
x1 y1 x2 y2 x y |
Cubic BΓ©zier curve to x y, pulled toward two control points. |
S / s |
x2 y2 x y |
Smooth cubic: the first control point is the mirror of the previous curve's second one, so the joint has no kink. |
Q / q |
x1 y1 x y |
Quadratic BΓ©zier curve to x y with one control point. |
T / t |
x y |
Smooth quadratic: the control point is mirrored from the previous Q/T. |
A / a |
rx ry rotation large-arc sweep x y |
Elliptical arc to x y. rotation tilts the ellipse (degrees); large-arc (0/1) picks the longer of the two possible arcs; sweep (0/1) picks the direction (1 = clockwise). |
Z / z |
(none) | Close path: a straight line back to the start of the current subpath, with a proper corner join. |
A command letter can be followed by several parameter sets: L 10 10 20 20 30 10 draws three lines.
The door uses relative commands (v-40 h30 v40): each one moves from wherever the previous command stopped, so the door can be moved by changing only its starting M.
Between two points there are two ellipses of the given size, and each offers a short and a long arc, so the two flags choose one of four arcs. If the radius is too small to reach the end point, the browser scales it up just enough.
Measuring and animating outlines
pathLength and the DOM methods on shapes help with "drawing" effects and with placing things along an outline:
| Member | Description |
|---|---|
getTotalLength() |
The length of the outline in user units (all shapes in current browsers). |
getPointAtLength(d) |
The {x, y} point at distance d along the outline. |
isPointInFill(point), isPointInStroke(point) |
Hit tests for a DOMPoint in the shape's user space. |
getBBox() |
The tight bounding box of the geometry (without the stroke). |
With pathLength="100" the dash pattern is measured as if the outline were 100 units long, so stroke-dasharray: 100 always covers exactly the whole curve, whatever its real length. getTotalLength() still reports the real geometric length.
Usage notes
- Pick the simplest shape. A
<circle>is easier to read and edit than the same circle written as two arcs. Use<path>for anything the basic shapes can't express. - Lines need a stroke.
<line>ignoresfill. A line, polyline or path with nostrokeis invisible (or, for open shapes, filled black). - Strokes are centered. Half of the
stroke-widthlies outside the geometry. A rectangle atx="0"with a 4-unit stroke loses 2 units of stroke at the edge of the viewBox. - Crisp 1-unit lines. A 1-wide horizontal line at
y="10"covers 9.5 to 10.5 and may look blurry;y="10.5"lines up with the pixel grid when one unit equals one pixel. - Accessibility. Individual shapes are usually skipped by screen readers. Give the whole graphic a name (
role="img"plusaria-labelor a<title>), or give a single meaningful shape a<title>child. - Optimizing. Path data exported by design tools is often long. Tools like SVGO shorten it by rounding numbers and switching to relative commands; check that rounding doesn't visibly change small icons.
Technical summary
| Elements | <rect>, <circle>, <ellipse>, <line>, <polyline>, <polygon>, <path> |
| Permitted content | Descriptive elements (<title>, <desc>, <metadata>) and animation elements (<animate>, <set>, <animateMotion>, <animateTransform>); never other shapes or text |
| Permitted parents | <svg> and SVG container elements such as <g>, <defs>, <symbol>, <a>, <pattern>, <clipPath>, <mask> and <marker> |
| Tag omission | Use a self-closing tag (<circle β¦ />) or an explicit end tag |
| Implicit ARIA role | None by default; a shape with an accessible name (a <title> or aria-label) is exposed as graphics-symbol |
| DOM interface | SVGRectElement, SVGCircleElement, SVGEllipseElement, SVGLineElement, SVGPolylineElement, SVGPolygonElement, SVGPathElement (all inherit from SVGGeometryElement) |
Browser support
All seven shapes and every path command are supported in every browser. Setting geometry such as r or width from CSS is newer; the CSS d property for paths is not yet available everywhere.
Related
<svg>β the root element and itsviewBox- SVG structural elements β
<g>,<defs>,<symbol>and<use> - SVG gradients and patterns β richer fills for shapes
- SVG presentation attributes β
fill,stroke,transformand text styling - Canvas 2D context β the pixel-based way to draw the same shapes
- Lesson: SVG Basics
- Lesson: SVG Paths, Text and Gradients