DevTools Logo

SVG Cheat Sheet

Quick reference for scalable vector graphics: viewBox, shapes, paths, transformations, gradients, and optimization basics.

Visual Tools
svg
vector
graphics

SVG (Scalable Vector Graphics) describes images as markup — resolutions don't matter. Unlike raster images, an SVG scales cleanly at any size and can be styled, animated, and manipulated with CSS and JavaScript.

viewBox & Coordinate System

viewBox="min-x min-y width height" defines the user coordinate system; the element then scales to whatever CSS size it is given.

html
<svg viewBox="0 0 100 100" width="200" height="200" xmlns="http://www.w3.org/2000/svg">
  <rect x="10" y="10" width="40" height="30" fill="teal" />
</svg>
Table
AttributeMeaning
viewBoxInternal coordinate system
preserveAspectRatioHow to fit the viewBox into the rendered box
width / heightRendered size (can be CSS)
xmlnsRequired namespace on inline SVG

Basic Shapes

Table
ElementAttributesExample
<rect>x, y, width, height, rx, ry<rect x="5" y="5" width="50" height="20" rx="4"/>
<circle>cx, cy, r<circle cx="25" cy="25" r="20"/>
<ellipse>cx, cy, rx, ry<ellipse cx="25" cy="25" rx="30" ry="15"/>
<line>x1, y1, x2, y2<line x1="0" y1="0" x2="100" y2="100"/>
<polyline>points<polyline points="0,0 50,40 100,0"/>
<polygon>points (closed)<polygon points="50,0 100,80 0,80"/>
<path>dSee below

Path Commands

d holds a sequence of single-letter commands. Uppercase = absolute, lowercase = relative.

Table
CommandNameExample
M x yMove toM 10 10
L x yLine toL 90 10
H x / V yHorizontal/Vertical lineH 90
C x1 y1 x2 y2 x yCubic BézierC 20 0, 80 0, 90 50
S x2 y2 x ySmooth cubicS 90 90, 50 90
Q x1 y1 x yQuadratic BézierQ 50 0, 90 50
T x ySmooth quadraticT 50 90
A rx ry rot larc sweep x yArcA 30 30 0 0 1 90 50
ZClose pathZ
html
<path d="M 10 10 H 90 V 90 H 10 Z" fill="none" stroke="teal" stroke-width="2" />

Fill, Stroke & Presentation

Table
AttributePurposeExample
fillInterior colorfill="teal", fill="none"
strokeOutline colorstroke="#333"
stroke-widthOutline thicknessstroke-width="2"
stroke-linecapEnd caps (butt, round, square)stroke-linecap="round"
stroke-linejoinCorner joints (miter, round, bevel)stroke-linejoin="round"
stroke-dasharrayDashed patternstroke-dasharray="4 2"
opacityElement transparencyopacity="0.5"
fill-ruleWinding (nonzero, evenodd)fill-rule="evenodd"

Transform & Gradients

html
<g transform="translate(50,50) rotate(45) scale(1.5)">
  <rect width="20" height="20" fill="teal" />
</g>
html
<defs>
  <linearGradient id="grad" x1="0" y1="0" x2="1" y2="0">
    <stop offset="0%" stop-color="#0d9488" />
    <stop offset="100%" stop-color="#0ea5e9" />
  </linearGradient>
</defs>
<rect width="100" height="40" fill="url(#grad)" />

Optimization Basics

  • Remove unused ids, comments, and redundant attributes.
  • Prefer integers/one decimal over long floats.
  • Use <path> over many tiny shapes.
  • Drop xmlns only outside inline contexts (it's required inline).
  • Run files through an optimizer (SVGO) before shipping.

Common Pitfalls

[!WARNING] Two ids with the same value in one page break gradient/filter references — prefix ids per icon instance or use unique names.

[!TIP] Set viewBox first, then CSS dimensions. Without viewBox, absolute width/height is the only way to scale and it won't resize responsively.

References