Image optimisation

How to optimise SVG files without breaking them

Run SVGs through SVGO, keep the viewBox, round to two or three decimal places and compare before and after. Here is what optimisation removes and what can break.

To optimise SVG files safely, run them through SVGO (or a tool built on it), round coordinates to two or three decimal places, keep the viewBox, and compare the result with the original before you replace it. That removes the editor clutter that design tools add, often cutting icons to a fraction of their exported size, without changing how they look.

The catch is that SVGs are code, and some optimisations can break things that depend on that code: animations, CSS selectors, sprites and screen reader labels. This guide covers what optimisation removes, what to watch for, and the settings that are safe for most sites.

Key takeaways

  • Optimisation strips metadata, comments, hidden elements and default values, and shortens numbers.
  • Decimal precision has the biggest effect. Two or three places suit most icons and logos.
  • Keep the viewBox so the SVG scales. SVGO 4 no longer removes it by default.
  • Be careful with IDs, merged paths and titles if your SVG is animated, styled with CSS or used in a sprite.
  • Always compare before and after, and serve SVGs with gzip or Brotli compression.

What SVG optimisation removes

An icon exported from Figma, Illustrator or Inkscape contains far more than the drawing. Typical extras include:

  • Editor metadata and namespaces, such as sodipodi or inkscape attributes and generator comments.
  • Hidden layers, empty groups and unused definitions.
  • Attributes set to their default value, which the browser would assume anyway.
  • Coordinates with six or more decimal places, such as 12.000000 or 4.2500003.
  • Transforms that can be folded into the path data, and shapes that can be written more compactly as paths.

Here is a small example before and after:

<!-- Before -->
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generator: Design App 12.0 -->
<svg xmlns="http://www.w3.org/2000/svg" width="24px" height="24px" viewBox="0 0 24 24" version="1.1">
  <title>arrow</title>
  <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
    <g id="arrow" fill="#000000">
      <path d="M12.000000,4.000000 L10.590000,5.410000 L16.170000,11.000000 L4.000000,11.000000 L4.000000,13.000000 L16.170000,13.000000 L10.590000,18.590000 L12.000000,20.000000 L20.000000,12.000000 Z"/>
    </g>
  </g>
</svg>

<!-- After -->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><title>arrow</title><path d="m12 4-1.41 1.41L16.17 11H4v2h12.17l-5.58 5.59L12 20l8-8z"/></svg>

That small icon drops from 542 bytes to 154, about 72% smaller, and renders identically. The same kinds of savings apply to larger files, where the clutter adds up across hundreds of paths.

Decimal precision: the biggest win

Path data is mostly numbers, so how many decimal places you keep matters more than any other setting. A 24 × 24 icon drawn to six decimal places is specifying positions far smaller than a pixel.

PrecisionGood forRisk
3 decimal placesDetailed illustrations, mapsVery low
2 decimal placesMost icons and logosLow
1 decimal placeSimple geometric iconsCurves can shift slightly
0 decimal placesPixel-aligned shapes onlyVisible distortion on curves

Start at 2 or 3 and only go lower if the before-and-after comparison shows no change.

What can break, and how to avoid it

The viewBox

The viewBox is what lets an SVG scale to any size. Older SVGO setups removed it when width and height were present, which left icons that wouldn’t resize with CSS. SVGO 4 dropped removeViewBox from its default preset for exactly this reason. If you remove anything, remove the fixed width and height and keep the viewBox.

IDs used by CSS, JavaScript or sprites

Optimisers shorten or remove IDs they think are unused. That is safe for a standalone icon, but it breaks anything that targets those IDs from outside the file: CSS like #logo-mark { fill: red; }, a script that animates #wheel, or a sprite referenced with <use href="#icon-search">. If your SVG is referenced like this, turn off ID clean-up.

Merged paths

Merging several paths into one saves bytes, but if you animate or colour parts separately, they need to stay separate. Keep merging off for animated SVGs and multi-colour icons that are themed with CSS.

Titles and accessibility

A <title> inside an SVG gives screen readers a name for it. SVGO 4 also stopped removing titles by default. Keep them for meaningful icons and illustrations; for purely decorative SVGs, use aria-hidden="true" on the element instead.

Tip: SVG files can contain scripts and event handlers. If you accept SVG uploads from users or paste third-party SVGs inline, remove them. An SVG shown with an <img> tag can’t run scripts, but inline SVG can.

Safe settings for SVGO

If you run SVGO yourself, this configuration is a good default for icons that are also styled or referenced from CSS:

// svgo.config.mjs (SVGO 4)
export default {
  multipass: true,
  floatPrecision: 2,
  plugins: [
    {
      name: 'preset-default',
      params: {
        overrides: {
          cleanupIds: false, // keep IDs used by CSS, JS or <use>
        },
      },
    },
    'removeDimensions', // drop width/height, keep viewBox
  ],
};

For standalone icons nobody references by ID, you can leave cleanupIds on for slightly smaller files.

Don’t forget server compression

SVG is text, so it compresses very well with gzip or Brotli. Most hosts compress HTML, CSS and JavaScript automatically but some miss image/svg+xml. Check the response headers for content-encoding: gzip or br when an SVG loads. Optimising the file and compressing it in transit work together.

Optimise your SVGs

Drop your files into the SVG optimiser. It runs SVGO in your browser, so nothing is uploaded, shows each file before and after with the size saving, and lets you choose the precision and whether to keep titles. Scripts are removed by default. If you need a PNG version of an SVG for an app store or social image, the image converter renders it at 2×, 3× or 4×. And if the SVG is your logo, the favicon generator can turn it into a full icon set; the favicon sizes guide explains which files you need.

Source: SVGO: migration from v3 to v4.