SVG Optimization: How to Shrink SVG Files Without Breaking Them
SVG is the best format for logos, icons and simple illustrations on the web: sharp at any size and usually tiny. But SVGs exported straight from design tools are often several times larger than necessary. They’re full of editor metadata, overly precise numbers and structure that only matters inside the editor. Cleaning them up is quick and usually cuts file size by a third or more.
What’s inside a typical exported SVG
Open an SVG from Illustrator, Inkscape, Sketch or Figma in a text editor and you’ll often find:
- an XML declaration (
<?xml version="1.0" encoding="UTF-8"?>) and sometimes a DOCTYPE, - a comment such as “Generator: …”,
- a
<metadata>block, sometimes with RDF licensing information, - editor namespaces and attributes like
inkscape:label,sodipodi:namedvieworsketch:type, <title>and<desc>elements with layer names,- IDs on nearly every element (“Layer_1”, “Rectangle-Copy-3”),
- empty or redundant groups (
<g>wrappers with no attributes), - coordinates with six or more decimal places:
cx="60.0000032", - default attribute values that don’t change anything.
Browsers ignore most of this, but they still have to download and parse it.
Safe optimizations
| Optimization | Typical saving | Risk |
|---|---|---|
| Remove XML declaration, comments, metadata | Small to medium | None for inline/web use |
| Remove editor namespaces and attributes | Medium (large for Inkscape files) | None in browsers |
| Round coordinates to 1–3 decimals | Large on complex paths | Low at sensible precision |
| Remove unused IDs | Small to medium | Low, if references are checked |
| Collapse empty groups | Small | Low |
| Collapse whitespace | Small to medium | Low (careful with <text>) |
The SVG optimizer above applies these safely by parsing the SVG structure rather than doing blind text replacement, and shows the original and optimized versions side by side so you can confirm nothing changed.
Optimizations that can break things
- Too little precision. Rounding to zero decimals on a small 24×24 icon can visibly distort curves. Use 1–2 decimals for icons and 2–3 for detailed illustrations and maps.
- Removing IDs that are referenced. Gradients, clip paths, masks, filters and
<use>elements refer to other elements by ID. Remove only IDs that nothing references. - Removing
<title>. For SVGs used as meaningful standalone images, the title can serve as the accessible name. Keep it, or providearia-labeloralttext in HTML. - Removing
viewBox. Some tools offer this, but without a viewBox the SVG can’t scale responsively. Keep it. - Merging paths and converting shapes to paths. These can save bytes but make the file harder to edit and animate. Fine for final icons, not for files you’ll keep working on.
How much can you save?
Results vary a lot by source:
| Source | Typical reduction |
|---|---|
| Inkscape (Inkscape SVG format) | 40–70% |
| Illustrator exports | 20–50% |
| Figma exports | 10–40% (Figma output is already fairly clean) |
| Hand-written or icon-library SVGs | 0–15% |
On a simple icon that saves a few hundred bytes; on a detailed illustration with long path data, it can save tens of kilobytes.
Optimization versus compression
Like CSS and HTML, SVG is text, so servers usually compress it with gzip or Brotli during transfer. Compression handles repetitive markup well, so the network saving from optimization is smaller than the raw file-size saving. Optimization still helps: rounding numbers removes information compression can’t, and smaller files parse faster. It’s the same relationship we describe in our CSS minification guide.
Inline SVG versus SVG files
| Method | Pros | Cons |
|---|---|---|
Inline <svg> in HTML | No request; style with CSS (currentColor), animate parts | Repeated on every page; not cached separately |
<img src="icon.svg"> | Cached; simple | Can’t style internals with page CSS |
| CSS background (data URI or file) | Good for decorative icons | Not accessible content; limited styling |
SVG sprite with <use> | One cached file for many icons | Slightly more setup |
Optimize before inlining: every byte of inline SVG is repeated in every HTML page that contains it.
Making icons themeable
A simple step after optimizing: replace hard-coded colors in icon SVGs with currentColor:
<path d="…" fill="currentColor"/>The icon then takes the text color of its parent, so it adapts to hover states, dark mode and different contexts without separate files.
Accessibility reminders
- Decorative inline SVGs: add
aria-hidden="true"so screen readers skip them. - Meaningful SVGs used as
<img>: providealttext. - Meaningful inline SVGs: add
role="img"and an accessible name, for example with<title>oraria-label.
For build pipelines
For ongoing projects, automate optimization with SVGO, the standard open-source SVG optimizer, which is also built into many bundlers and icon tools. It has a large set of plugins, including path merging and transform flattening, with safe defaults. Use an online optimizer for one-off files, quick checks or learning what an optimizer changes.