Optimizing SVG files

Shrink SVG with SVGO and server compression without breaking viewBox, titles or ids. Includes config examples and the trade-offs of each setting.

Most SVG files are bigger than they need to be. Editors write metadata, comments, long decimals and unused definitions. Removing that is safe in most cases, but a few optimizations change behavior, and those are the ones to understand before you automate anything.

Where the weight comes from

An SVG is text, so it shrinks in two ways: by making the markup itself smaller, and by compressing it for transfer. Do both, in that order.

Typical waste in an exported file:

  • Editor namespaces, metadata and comments.
  • Coordinates with many decimal places.
  • Groups and attributes that repeat values or restate defaults.
  • Unused ids and empty containers.

SVGO (SVG Optimizer) is the standard tool for this. Its documentation describes it as a Node.js library and command-line application that removes redundant data such as editor metadata, comments, hidden elements and suboptimal values, and says the aim is that the optimized file renders the same as the original. It requires Node.js 16 or later.

Run SVGO

The docs show global installs with npm, Yarn or pnpm:

npm install -g svgo

The README shows these CLI forms:

# one or more files
svgo one.svg two.svg -o one.min.svg two.min.svg

# a folder, recursively
svgo -rf path/to/directory_with_svgs -o path/to/output_directory

# all options
svgo --help

For a build script, the Node API has an optimize() function that takes the SVG string and a config object and returns the optimized string in result.data. The docs recommend passing path along with the string.

The multipass option runs the optimizations repeatedly until the result stops changing. The README documents it as a config and API option. It can find a little more, at the cost of build time.

Configure it deliberately

If you do not set a plugins list, SVGO runs preset-default. Create a config file, which the README names svgo.config.mjs, so the behavior is explicit and reviewed:

export default {
  multipass: true,
  plugins: [
    {
      name: 'preset-default',
      params: {
        overrides: {
          // keep <desc> so descriptions survive
          removeDesc: false,
        },
      },
    },
    'prefixIds',
  ],
};

The overrides object switches off a plugin that is part of the preset by setting its name to false. Pass the file with --config path/to/config.mjs.

The preset-default page lists 33 plugins. Reading that list is worth ten minutes, because three entries affect accessibility and layout:

  • removeDesc is in the preset. It strips <desc> elements, which are a source of accessible descriptions. If your graphics rely on them, override it as above. Check the accessibility guide for why.
  • removeTitle is not in the preset. The docs warn that it may have significant accessibility implications, and say removal is reasonable for purely decorative SVG or when accessibility is handled elsewhere, for example with aria-label in the HTML. Leave it off for informative graphics.
  • removeViewBox is not in the preset list. The plugin removes viewBox when it matches the width and height, and its page warns it prevents the SVG from scaling, so it may not fill its container or may clip. If you want an SVG to resize with CSS, keep the viewBox. See the viewBox guide and responsive SVG. Check your own SVGO version’s preset list rather than assuming.

See what path rounding does

The convertPathData plugin shortens path data by switching between relative and absolute commands, removing redundant commands and rounding numbers. Its floatPrecision option sets the number of decimal places. The docs do not state a default, so set it yourself if you care.

Rounding reduces precision, and the effect depends on the size of the viewBox. In the example, the thick cobalt curve uses three decimals, and the thin magenta curve on top uses whole numbers. At this size they are nearly the same line, which is why rounding works well for icons.

Edit the magenta path to remove more precision, and watch where the lines start to part. Now imagine the same file shown at 2000 units wide. The same rounding would be visible. Choose precision for the largest size the graphic will appear at, not the one you see in your editor. The paths guide explains the commands used here.

Avoid id collisions when inlining

cleanupIds is part of the default preset. It removes unused ids and minifies ids that are referenced. The docs say it can cause problems when you inline multiple SVGs in the same parent document, because minified ids from different files can clash. Two fixes from the docs: enable prefixIds, or set minify to false. Disabling minification will not repair clashes already present in the source files. The prefixIds docs recommend reproducible prefixes such as the file name over counters or random strings, because unpredictable ids can cause mismatches between server-rendered and client-rendered HTML in React-based tools. See SVG in frontend frameworks.

Gradients, clip paths and masks all use ids, so a clash shows up as the wrong gradient or a missing mask. Here is a small sprite-like case: one definition referenced by id.

If two inlined SVGs on one page both defined id="brand-fade" with different stops, the browser would pick one definition for both. Prefixing each file’s ids prevents that.

Scripts and untrusted files

SVGO has a removeScripts plugin that deletes <script> elements, strips event attributes such as onload and onclick, and unwraps <a> elements. Its docs recommend it for SVG from untrusted sources and warn that it breaks interactive SVG. It is not in the preset list I read, so you must enable it. I would treat it as an extra layer, not as your only protection for user uploads. The security guide covers sanitizing properly.

Compress for transfer

After minifying, let the server compress the response. MDN’s page on Content-Encoding lists gzip, br (Brotli) and zstd, and explains that the client advertises what it accepts in Accept-Encoding and the server states its choice in Content-Encoding. It also advises against re-compressing already compressed media such as zip or JPEG files, which can increase size. SVG is text, so it is a good candidate for compression. Confirm in your browser’s network panel that your SVG responses carry a Content-Encoding header, because servers differ in which file types they compress by default.

When not to optimize

  • If the file is edited by designers in a tool that expects its own metadata, keep the original and optimize a copy in the build.
  • If the SVG is animated by script that finds elements by id, disable id minification or preserve those ids with the preserve option listed in the cleanupIds docs.
  • If an SVG is below a few hundred bytes, optimizing may not matter. Spend the time elsewhere.

A short checklist

  1. Keep the originals. Optimize in the build, not by hand.
  2. Review preset-default and decide on removeDesc, removeTitle and removeViewBox explicitly.
  3. Choose a floatPrecision that survives your largest display size.
  4. Prefix ids on any SVG that will be inlined with others.
  5. Compare before and after in a browser, at several sizes.
  6. Confirm compression on the server.

Sources

Checked against these references on 2026-11-21.