How to reduce SVG file size
Updated
SVGs exported from design tools routinely carry twice the markup they need: editor metadata, hidden layers, long decimals and verbose path data. Most of it can go without changing a pixel — but a few popular optimizations do break things. Here is what to remove, and what to keep.
Where the extra bytes come from
- Editor metadata: Illustrator, Inkscape, Sketch and Figma add their own namespaces, comments and attributes.
- Precision: coordinates like 12.000003814697266 where 12 would draw the same.
- Verbose path data: absolute commands and repeated command letters, where relative, shorter forms exist.
- Empty or hidden elements, unused definitions and redundant groups.
- Whitespace and indentation.
Safe optimizations
These shrink a file without any visible change, and are what a safe optimizer preset does:
- Remove comments, metadata and editor-only attributes.
- Round numbers to a precision that still draws identically at the size you use (usually 2–3 decimals).
- Rewrite path data in its shortest form.
- Collapse groups that do nothing and remove empty elements.
- Minify whitespace.
Optimizations that can break things
- Removing the viewBox: the SVG can no longer scale — it is stuck at its width and height. Keep the viewBox.
- Removing or renaming ids: breaks anything that refers to them — CSS, JavaScript, sprite <use> references, links to #fragments.
- Removing "unused" ids and definitions: a sprite's <symbol> ids are referenced from other pages, not from the sprite itself, so an optimizer that strips unreferenced ids breaks every <use>.
- Merging paths: changes how overlapping shapes paint and loses separate elements you may animate.
- Over-rounding: icons drawn on a small viewBox (0 0 24 24) visibly distort at 0 decimals.
If an optimized file looks different, turn these off one by one and compare.
Then compress on the server
SVG is text and compresses very well: gzip or Brotli typically cut an optimized SVG by another 60–80% in transit. Most hosts and CDNs do this automatically for image/svg+xml — check that the response has a Content-Encoding header.
Simplify the artwork itself
The biggest wins are sometimes in the drawing: an embedded bitmap (<image href="data:image/png…">) can be most of the file; traced artwork with thousands of tiny shapes can be re-traced with fewer colours; and text converted to outlines is much larger than the text itself.