SVGO Guide: How to Optimize SVG Files
By Žan Ožbot • Published • Updated • 6 min read
SVGO is the standard tool for optimizing SVG files. It parses the file, removes what browsers do not need, and rewrites the rest more compactly. This guide covers SVGO 4: how to run it, what it changes, and which settings can break a graphic.
Optimize an SVG online
The quickest way is SVGLite, which runs SVGO in your browser. Drop in one file or a batch and download the results. Files stay on your device, and there is nothing to configure.
Run SVGO from the command line
With Node.js installed, no setup is needed:
# One file
npx svgo icon.svg -o icon.min.svg
# Every SVG in a folder, including subfolders
npx svgo -r -f ./icons -o ./icons-min
# Repeat until nothing more can be saved, with 2 decimal places
npx svgo icon.svg --multipass --precision 2 -o icon.min.svgWithout -o, SVGO overwrites the original, so keep the source file in version control.
What SVGO removes
- Editor data: metadata, comments and namespaces from Illustrator, Inkscape or Sketch
- Defaults: attributes that repeat what the browser would do anyway
- Extra precision: coordinates rounded to three decimals by default
- Verbose paths: absolute commands become relative ones when shorter
- Redundant structure: empty groups, unused definitions and hidden elements
- Long colors and IDs: for example,
rgb(255, 0, 0)becomesred
How much does it save?
It depends on the source. The six cover images on this blog were exported by a design script and shrank by 49% to 55% with the settings SVGLite uses, with pixel-identical rendering in our test. A house icon that was already hand-tuned did not shrink at all.
See the full size table, which also covers gzip and Brotli.
A config to start from
Save this as svgo.config.mjs in your project root:
export default {
multipass: true,
plugins: [
{
name: "preset-default",
params: {
overrides: {
// keep IDs that your CSS or JavaScript refers to
cleanupIds: false,
},
},
},
// let CSS control the size (keep the viewBox)
"removeDimensions",
],
};In SVGO 4 the default preset keeps viewBox and title. In version 3 both were removed by default, so older configs that override removeViewBox to false are no longer needed.
Settings that can break an SVG
| Plugin | What can go wrong | Fix |
|---|---|---|
| cleanupIds | CSS, JavaScript or links that use the ID stop working; short IDs can clash when several SVGs are inlined | Disable it, or add prefixIds |
| mergePaths | You can no longer style or animate the shapes separately | Disable it |
| collapseGroups | Groups you animate may disappear | Disable it |
| removeHiddenElems | Elements you reveal later with CSS are deleted | Disable it |
| removeDesc | Some descriptions are removed, which can affect accessibility | Disable it for meaningful images |
| removeViewBox | The SVG stops scaling | Do not enable it |
SVGLite uses the default preset, including the first five plugins in this table. Keep the original of any SVG you script or animate.
Add SVGO to a build
A package.json script covers most projects:
{
"scripts": {
"svg": "svgo -r -f src/icons"
}
}In React, SVGR can run SVGO for you through @svgr/plugin-svgo. In Vite, use a plugin that wraps SVGO if you import SVGs as components.
Check the result
- Compare the original and optimized files at a large size
- Check gradients, masks and clip paths first, since they depend on IDs
- If something is missing, work through common SVG problems
- For savings beyond SVGO, see 10 ways to reduce SVG file size