Skip to content

Menu

SVG Cheat Sheet: viewBox, Path Commands, Shapes and Optimizing

Checked against SVG MDN SVG reference and W3C SVG 2; optimizing examples run with SVGO 4.1.0 on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for front-end developers who hand-edit icons, tweak exported SVGs, or need to know why a graphic will not scale. Attribute rows follow the MDN SVG reference and the W3C SVG 2 specification, read on 28 Nov 2026. The optimizing rows were run with SVGO 4.1.0, the current release on npm.

When to use what

How you put an SVG on the page decides what you can do with it. Inline <svg> in the HTML is the only option that lets page CSS and JavaScript reach inside the graphic, so use it for icons that change colour on hover or with a theme. <img src="icon.svg"> keeps the markup clean and caches well, but MDN notes that SVG loaded as an image runs no scripts and cannot load external files. CSS background-image has the same limits and suits decoration only, because it has no alt text.

For scaling, put a viewBox on every SVG you ship and treat width and height as the default display size. Without a viewBox, preserveAspectRatio does nothing and the drawing will not scale with its box. If you want a graphic to fill a hero area and crop, use slice; if it must stay fully visible, keep the default meet.

Run exported files through an optimizer before committing them. Design tools leave editor metadata, empty groups and long decimals behind. Our SVG optimizer does this in the browser, and the SVG optimization guide explains which plugins are safe to turn off. For React projects, the SVG to JSX converter fixes attribute names like stroke-width for you.

preserveAspectRatio only has an effect when a viewBox is set (the <image> element is the exception).

viewBox="0 0 24 24"

Define a 24 by 24 user coordinate system starting at the top-left corner

viewBox="-1 -1 202 40"

Start the coordinate system at negative values to give strokes on the edge room to show

preserveAspectRatio="xMidYMid meet"

Default: scale uniformly, center, and keep the whole viewBox visible

preserveAspectRatio="xMidYMid slice"

Scale uniformly to cover the viewport and crop whatever overflows

preserveAspectRatio="none"

Stretch the drawing to fill the box, distorting it if the ratios differ

preserveAspectRatio="xMinYMax meet"

Keep the whole drawing visible and pin it to the bottom-left of the viewport

Uppercase commands take absolute coordinates; lowercase ones are relative to the current point. Parameter groups can repeat after one command letter.

M x y / m dx dy

Move the pen without drawing; starts a new subpath

L x y / l dx dy

Draw a straight line to a point

H x / V y

Draw a horizontal or vertical line using only one coordinate

C x1 y1 x2 y2 x y

Cubic Bezier with two control points to the end point

S x2 y2 x y

Smooth cubic: the first control point mirrors the previous curve's

Q x1 y1 x y / T x y

Quadratic Bezier with one control point, and its smooth follow-on

A rx ry angle large-arc sweep x y

Elliptical arc; the two flags pick which of four possible arcs is drawn

Z

Close the subpath with a straight line back to its start

Paint properties are defined in https://www.w3.org/TR/SVG2/painting.html. All of these can also be set from CSS when the SVG is inline.

<rect x="2" y="2" width="20" height="20" rx="4"/>

Rectangle with rounded corners set by rx

<circle cx="12" cy="12" r="10"/>

Circle from a center point and radius

<ellipse cx="12" cy="12" rx="10" ry="6"/>

Ellipse with separate horizontal and vertical radii

<line x1="0" y1="0" x2="24" y2="24"/>

Single line segment; it needs a stroke to be visible

<polyline points="4 12 9 17 20 6"/>

Open shape through a list of points, the classic check mark

<polygon points="12 2 22 22 2 22"/>

Closed shape through a list of points

fill="none" stroke="currentColor" stroke-width="2"

Outline icon whose colour follows the surrounding text colour

stroke-linecap="round" stroke-linejoin="round"

Round line ends and corners, common for icon sets

The <title> advice comes from https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/title.

<img src="logo.svg" alt="Acme">

Load SVG as an image: cached, but no scripts and no external resources inside

background-image: url(icon.svg)

Decorative SVG from CSS; it has no alt text

<svg>...</svg> inline

Only embedding that page CSS and JavaScript can style and script

<svg role="img" aria-labelledby="t1"><title id="t1">Chart</title>

Give an inline SVG an accessible name

From the docs, not run by us
<title> as first child

MDN recommends this placement for compatibility with SVG 1.1; browsers often show it as a tooltip

aria-hidden="true"

Hide a purely decorative inline icon from screen readers

From the docs, not run by us

The default plugin list is at https://svgo.dev/docs/preset-default/. Flags below are from svgo --help on 4.1.0.

npx svgo icon.svg -o icon.min.svg

Optimize one file with the default preset and write a new file

svgo -rf icons/ -o dist/icons/

Optimize every SVG in a folder tree

From the docs, not run by us
svgo --multipass icon.svg

Run the plugins repeatedly until the output stops shrinking

From the docs, not run by us
svgo -p 2 icon.svg

Round numbers to two decimal places, overriding plugin settings

From the docs, not run by us
svgo --datauri enc icon.svg

Output a URI-encoded data URI for use in CSS

From the docs, not run by us
svgo.config.mjs

Config file SVGO reads by default; add or disable plugins there

Worked example: Shrink an exported icon with SVGO 4.1.0

Design tools export icons like this one, with an XML declaration, a generator comment, an empty group and padded decimals:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Generator: Some Editor 1.0 -->
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
    <title>Check</title>
    <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
        <path d="M 4.000 12.000 L 9.000 17.000 L 20.000 6.000" stroke="#000000" stroke-width="2.000"/>
    </g>
</svg>

Running npx svgo icon.svg -o icon.min.svg printed 0.452 KiB - 57.2% = 0.193 KiB and wrote:

<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><title>Check</title><path fill="none" fill-rule="evenodd" stroke="#000" stroke-width="2" d="m4 12 5 5L20 6"/></svg>

The file went from 463 to 198 bytes. SVGO dropped the declaration, comment, unused xlink namespace and version, merged the group's attributes into the path, and rewrote the path with relative commands (m4 12 5 5 is a move followed by an implicit relative line). The viewBox and <title> survived, so the icon still scales and keeps its accessible name. If you then want it to follow text colour, change stroke="#000" to stroke="currentColor" by hand.

Common mistakes

  • Shipping an SVG with width and height but no viewBox.

    Add a viewBox that matches the drawing's coordinates. Without it the graphic will not scale with CSS and preserveAspectRatio is ignored.

  • Trying to recolour an SVG loaded through <img> with page CSS.

    Page styles cannot reach inside an image. Inline the SVG and use currentColor, or ship a second file in the other colour.

  • Writing stroke-width and class in JSX.

    React expects strokeWidth and className. Convert the markup with a tool or by hand before pasting it into a component.

  • Using a lowercase command after copying a path into a new position and wondering why the shape jumps.

    Lowercase commands are relative to the current point. Use uppercase M for the first move if the path must start at a fixed coordinate.

  • Leaving an informative inline SVG with no accessible name.

    Add role="img" and a <title> (or aria-label), and mark purely decorative icons with aria-hidden="true" instead.