Guide
SVG by hand: viewBox, coordinates and path commands
Most SVG is exported from a design tool and never read again, which is why so many icons are 5 KB of noise around a 200-byte shape. Once you understand the coordinate system and the path mini-language, you can read, fix and write SVG directly. This guide covers both, with examples you can paste into any browser.
The smallest useful SVG
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
<circle cx="12" cy="12" r="10" fill="none" stroke="currentColor" stroke-width="2"/>
<path d="M8 12l3 3 5-6" fill="none" stroke="currentColor" stroke-width="2"
stroke-linecap="round" stroke-linejoin="round"/>
</svg>A circled tick in about 300 bytes. Three ideas carry nearly all SVG work: the viewBox defines the drawing's own coordinate system, shapes and paths draw in those coordinates, and currentColor makes the icon take the text colour of wherever it is placed.
viewBox: the drawing's own coordinates
viewBox="min-x min-y width height" says which rectangle of an infinite canvas is visible. width and height on the <svg> element say how big that rectangle appears on the page. The browser scales one onto the other.
With viewBox="0 0 24 24" and width="48", every unit becomes two pixels, and the icon stays sharp because nothing is rasterised until it is drawn. Drop width and height entirely and the SVG fills its container, which is usually what you want for responsive images; set the size in CSS instead.
The first two numbers move the window. viewBox="-12 -12 24 24" puts the origin in the centre, which makes symmetric drawings easier to write: a circle at cx="0" cy="0" sits in the middle.
The y axis points down, as on screens: increasing y moves toward the bottom. Angles in rotations and arcs therefore run clockwise.
preserveAspectRatio: when the shapes disagree
If the viewBox is 24 by 24 but the element is 48 by 24, the drawing cannot fill both dimensions without distortion. preserveAspectRatio decides what happens:
xMidYMid meet(the default): scale uniformly until the whole viewBox fits, and centre it. Like CSSobject-fit: contain.xMidYMid slice: scale uniformly until the element is covered, cropping the overflow. Likeobject-fit: cover.none: stretch each axis independently. Useful for backgrounds and dividers that should fill any width.
The xMin, xMid, xMax and YMin, YMid, YMax parts choose which edge to align, for example xMinYMid meet to keep a logo against the left edge.
Basic shapes
| Element | Attributes | Notes |
|---|---|---|
<rect> | x, y, width, height, rx, ry | rx and ry round the corners |
<circle> | cx, cy, r | |
<ellipse> | cx, cy, rx, ry | |
<line> | x1, y1, x2, y2 | Needs a stroke to be visible |
<polyline> | points | Open series of straight segments |
<polygon> | points | Closed automatically |
<path> | d | Everything else: curves, arcs, compound shapes |
The path mini-language
A path's d attribute is a sequence of commands, each a letter followed by numbers. Upper-case letters use absolute coordinates; lower-case letters are relative to the current point, which makes shapes easy to move and often shorter to write.
| Command | Parameters | Draws |
|---|---|---|
| M / m | x y | Move to a point without drawing (starts a sub-path) |
| L / l | x y | Straight line to a point |
| H / h | x | Horizontal line |
| V / v | y | Vertical line |
| C / c | x1 y1 x2 y2 x y | Cubic Bézier with two control points |
| S / s | x2 y2 x y | Smooth cubic: first control point mirrors the previous one |
| Q / q | x1 y1 x y | Quadratic Bézier with one control point |
| T / t | x y | Smooth quadratic: control point mirrored |
| A / a | rx ry rotation large-arc sweep x y | Elliptical arc |
| Z / z | Close the sub-path with a straight line to its start |
A command letter can be followed by several sets of numbers, which repeats it: L10 0 10 10 0 10 draws three lines. After an M, extra pairs are treated as L. Commas are optional, and a minus sign can separate numbers, which is why minified paths look like M2 8l4 4 8-8.
Curves, one at a time
Straight lines and a square:
<path d="M4 4 H20 V20 H4 Z"/>
<!-- the same square with relative commands -->
<path d="M4 4 h16 v16 h-16 z"/>A cubic Bézier starts at the current point, is pulled toward two control points and ends at the target. The control points are where the curve's tangents point at each end:
<!-- an S-shaped curve from (2,12) to (22,12) -->
<path d="M2 12 C 8 2, 16 22, 22 12" fill="none" stroke="black"/>S continues a curve smoothly by reflecting the previous control point, which is how a wave of any length stays seamless:
<path d="M0 12 C 3 4, 9 4, 12 12 S 21 20, 24 12" fill="none" stroke="black"/>Arcs, the confusing one
A rx ry x-axis-rotation large-arc-flag sweep-flag x y draws part of an ellipse from the current point to (x, y). Given two points and two radii there are generally two ellipses and four possible arcs; the two flags pick one:
- large-arc-flag: 1 picks the arc spanning more than 180°, 0 the shorter one.
- sweep-flag: 1 draws clockwise (positive angle direction, given that y points down), 0 counter-clockwise.
<!-- a semicircle: from (2,12) to (22,12) with radius 10 -->
<path d="M2 12 A10 10 0 0 1 22 12" fill="none" stroke="black"/>
<!-- a pie slice: centre, out to the edge, arc, back to centre -->
<path d="M12 12 L12 2 A10 10 0 0 1 22 12 Z" fill="tomato"/>If the radii are too small to reach between the two points, the renderer scales them up just enough. That is why A1 1 0 0 1 22 12 from (2,12) still draws a semicircle.
Fill, stroke and fill-rule
fillpaints the inside,strokethe outline. An open path is still filled as if it were closed, so setfill="none"for lines.- A stroke is centred on the path, half inside and half outside, which is why a 2-unit stroke on a shape touching the viewBox edge looks clipped.
stroke-linecap(butt, round, square) shapes the ends of open paths;stroke-linejoin(miter, round, bevel) shapes the corners.fill-rule="evenodd"cuts holes where sub-paths overlap, the usual way to draw a ring or a letter O in one path. The default,nonzero, decides by the direction each sub-path is drawn.
Keeping hand-written and exported SVG small
- Pick a viewBox on a sensible grid (16 or 24 units for icons) and round coordinates to one decimal place or whole numbers; extra precision is invisible.
- Remove editor metadata, comments, empty groups,
ids nothing references, and default attribute values such asfill-opacity="1". - Prefer relative commands and let consecutive commands share a letter.
- Use
currentColorinstead of hard-coded colours so one file works in light and dark themes. - Collapse nested
transforms into the coordinates when you can; a path with a transform matrix is hard to read and edit.
SVGO automates most of this from the command line. Splice takes the other route: it keeps the markup tidy while you edit, so the code pane stays readable and editing a path node changes only that node's numbers. To embed a finished icon in CSS without an extra file, see Base64 images and data URIs: for SVG, URL-encoding beats Base64.