SpunkArt
Color, art and images

How to read and write SVG by hand

SVG is a text format for vector images. Every line, circle and curve is a short instruction you can read and edit in a plain text editor, and the result stays sharp at any size.

A complete small SVG

<svg viewBox="0 0 24 24" width="48" height="48">
  <rect x="2" y="2" width="20" height="20" rx="4" fill="#1a5fb4"/>
  <circle cx="12" cy="12" r="5" fill="#ffffff"/>
  <path d="M7 19 L17 19" stroke="#ffffff" stroke-width="2" stroke-linecap="round"/>
</svg>

This draws a blue rounded square with a white dot and a short white line under it. Pasted straight into an HTML page, it works as written. Saved as its own .svg file, the root element also needs an xmlns attribute naming the W3C SVG namespace (the address that ends in www.w3.org/2000/svg), or browsers will not display it as an image.

The viewBox: your private coordinate grid

viewBox="min-x min-y width height" defines the drawing’s own units. The width and height attributes set how large it appears. The browser scales one onto the other.

scale = displayed width ÷ viewBox width

Example. The icon above has a 24-unit viewBox shown at 48 pixels, so scale = 48 ÷ 24 = 2. The circle’s radius of 5 units is drawn 10 pixels wide on each side of its centre, and the 2-unit stroke is 4 pixels thick. At 24 pixels the same file draws everything at scale 1.

The y axis points down, as in most screen graphics. The point (0, 0) is the top-left corner of the viewBox, and y = 24 is the bottom edge.

If the width-to-height ratio of the viewBox and the displayed box differ, preserveAspectRatio decides what happens. The default, xMidYMid meet, keeps the drawing’s shape and centres it, leaving empty space at the sides.

Basic shapes

ElementKey attributesDraws
rectx, y, width, height, rxRectangles, rounded with rx
circlecx, cy, rCircles
ellipsecx, cy, rx, ryOvals
linex1, y1, x2, y2One straight segment (needs a stroke)
polygonpointsClosed shapes from a list of points
pathdAnything, using the commands below

Path commands

The d attribute of a path is a list of commands, each a letter followed by numbers. Uppercase letters use absolute coordinates; lowercase letters are relative to the current point.

  • M x y: move the pen without drawing.
  • L x y: line to a point. H x and V y draw horizontal and vertical lines.
  • C x1 y1 x2 y2 x y: cubic Bézier curve with two control points.
  • Q x1 y1 x y: quadratic curve with one control point.
  • A rx ry rotation large-arc sweep x y: part of an ellipse.
  • Z: close the shape back to its start.

Reading a path. M4 12 L10 18 L20 6 is a check mark: start at (4, 12), draw down-right to (10, 18), then up-right to (20, 6). Written with relative commands it becomes M4 12 l6 6 l10 -12: the second segment moves 10 right and 12 up from where the first ended.

Fill and stroke

fill paints the inside of a shape and stroke paints its outline. A stroke is centred on the path, so half of it falls outside. A 2-unit stroke on a rectangle that touches the viewBox edge loses 1 unit of its width to clipping. Leave a margin, as the example does with its 2-unit inset.

Setting fill="currentColor" makes an inline icon take the text color of its parent, so one icon can follow your link and hover colors without extra files.

Keep files tidy

  1. Remove editor metadata, comments and empty groups that drawing apps leave behind.
  2. Round coordinates. Two decimal places are plenty for icons; a 24-unit icon rarely needs any.
  3. Merge shapes that share a fill into one path where you can.
  4. Convert text to paths if the file must look the same on machines without your font.
  5. Add a <title> inside the SVG when it carries meaning, so screen readers can announce it.

Common mistakes

  • Missing viewBox. Without it the drawing will not scale; it is cropped or left small instead.
  • Fixed width and height in CSS-driven layouts. If CSS sets the size, you can drop the attributes and keep only the viewBox.
  • Using SVG for photos. Photographs have no clean shapes, so an SVG version is huge or just wraps a bitmap. Use JPEG, WebP or AVIF for them.

Questions people ask

Is it safe to accept SVG uploads from users?

Be careful. An SVG file can contain scripts and links. Sites that accept uploads usually sanitise the file or serve it from a separate domain, and never inline it into their own pages as is.

Do I need a drawing program?

For icons and simple shapes, a text editor is enough. For complex illustrations, draw in a vector app and then tidy the exported code by hand.