Sub topic 6
SVG: icons, logos, and illustrations
This chapter assumes you know the difference between raster and vector images, and that you can open a layered file in Figma, Sketch, or Illustrator. It also assumes you have seen basic HTML tags such as <img>.
What SVG is
SVG (Scalable Vector Graphics) is a text-based format that describes shapes with math instead of pixels. A circle is stored as a center point and a radius. Because the browser redraws the shapes at whatever size it needs, an SVG stays sharp on a 1x screen, a 3x phone, and a projector. You don't export a 2x version.
SVG fits images made of clean shapes: icons, logos, simple illustrations, and charts. It is a poor fit for photographs, because describing every tone as a shape produces a huge, slow file.
A minimal example
This icon is a plus sign. The viewBox defines the internal coordinate grid. The width and height set the displayed size.
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" role="img" aria-label="Add">
<path d="M12 5v14M5 12h14" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
</svg>Three details matter here:
viewBoxlets the image scale. Without it, resizing the SVG crops it instead of scaling it.stroke="currentColor"makes the line take the text color of its surroundings. One icon file can then serve light mode, dark mode, and hover states.role="img"andaria-labelgive screen readers a name for the icon. Purely decorative icons should usearia-hidden="true"instead.
Preparing the file in your design tool
Most export problems come from the file, not from the export dialog.
- Name the frame tightly around the artwork so there is no stray empty space.
- Convert text to outlines if the font will not be available in the browser. Outlined text is no longer selectable, so use it for logos only.
- Expand or outline strokes only when you need exact shapes. Otherwise keep them as strokes, because they can be styled with CSS.
- Remove hidden layers, unused masks, and off-canvas objects. They are exported into the file.
- Set a fill or stroke color you can override. Pure black is common for icons that will be recolored.
Optimizing the output
Design tools add editor metadata and extra precision to SVG files. A tool such as SVGO removes this without changing the look.
npx svgo icon-add.svg -o icon-add.min.svgAlways open the result in a browser. Aggressive settings can remove a viewBox or merge shapes in ways that break scaling.
Common errors
| Problem | Cause | Fix |
|---|---|---|
| Icon is cropped when resized | Missing `viewBox` | Add `viewBox` matching the artboard size |
| Icon ignores the text color | Hard-coded `fill` values | Use `currentColor` |
| Text shows in the wrong font | Live text with an unavailable font | Convert text to outlines |
| File is unexpectedly large | Embedded raster image or heavy tracing | Re-draw the shapes or use a raster format |
Exercise
Your design has a logo, a hero photograph, and a set of 20 line icons. Which of these should be SVG, and why does the SVG not need a 2x export?
Show answer
The logo and the icons should be SVG, since they are made of clean shapes. The photograph should be JPEG, WebP, or AVIF. An SVG is redrawn from shape data at the displayed size, so it stays sharp on high-density screens without extra files.