← Guides

Why SVG Files Get Large and How to Optimize Them

Why SVG files get large

SVG is XML, so a simple drawing can be expressed in a few hundred bytes. Real exports rarely are. A 24 px icon frequently arrives at 20–60 KB because the design tool writes its working state alongside the artwork: layer names, hidden groups, empty <defs> blocks, gradients nothing references, masks that mask nothing, XML comments, and private namespaced attributes such as inkscape:label or sodipodi:nodetypes. Coordinates keep six or seven decimal places because the tool stores whatever the canvas produced. Fills and strokes are repeated on every element even when a parent already sets them. Auto-traced artwork adds thousands of short path segments, and each segment carries its own rounding noise.

  • Editor metadata — authoring information that no renderer reads.
  • Unused definitions — gradients, clip paths, markers and filters left over from deleted layers.
  • Redundant structure — single-child groups, empty text nodes, wrappers that only repeat inherited attributes.
  • Verbose numbers — long decimals, leading zeros, absolute coordinates where relative ones are shorter.
  • Repeated styles — the same font-family or fill written on dozens of elements, sometimes as inline style strings.

None of this affects the rendered picture, but all of it is downloaded, parsed and kept in memory. That is why an SVG can look trivial and still cost more than a compressed photograph.

What SVGO actually changes

SVGO is not an image converter. It does not rasterise the drawing, resample it, or approximate it with another format. It parses the document into a syntax tree, runs a sequence of plugins over that tree, and serialises the surviving nodes back to XML. Each plugin owns one transformation: remove comments, drop metadata, delete editor namespaces, merge duplicate style declarations, shorten colour notation, round numbers, convert simple shapes to paths, collapse groups whose attributes add nothing, merge adjacent paths that share a style.

The default preset is a chain of roughly thirty such plugins. Because the work happens at the node level, text stays text, gradients stay gradients, and strokes keep their geometry unless a plugin is explicitly allowed to approximate. The result is still an ordinary SVG document; no browser needs to know it was optimised.

Two consequences follow. First, savings come from removing information, not from re-encoding, so a file that was already minimal will barely change. Second, some plugins are structural: they change how the document is organised for editing, even when pixels are identical.

Precision and visual loss

Path data is a list of commands and coordinates. An export may contain 12.345678 where 12.35 would render identically at any realistic size. Every digit costs a byte, and long decimals also prevent two nearly identical segments from being recognised as duplicates. Rounding is the setting that most directly trades bytes for fidelity.

  • 4 decimals — effectively lossless for most artwork; useful for detailed maps or technical drawings.
  • 2 decimals — the usual compromise for icons and logos; differences are far below one device pixel.
  • 1 decimal — acceptable for simple geometric marks at large sizes, risky for thin strokes.
  • 0 decimals — rounds to integers; only suitable for deliberately blocky shapes.

Precision is a ceiling, not a guarantee. The path plugins also rewrite commands, switch between absolute and relative notation, delete redundant moves and occasionally simplify curves. A 0.4 px shift is invisible on a 512 px illustration and obvious on a 16 px favicon, because what matters is the error relative to the viewBox, not the error in absolute units. Preview at the size where the file will actually appear, and compare against the original whenever the artwork is important.

Attributes you can safely remove

Several categories are almost always safe. Comments and metadata carry authoring information for humans and tools, not rendering instructions. Editor namespaces only matter if you reopen the file in the same editor. Empty groups, empty text nodes and definitions that nothing references can go. Attribute values that equal the renderer's default can be dropped. Repeated styles can be merged into a class or lifted to a parent. Colours can be shortened from #ffffff to #fff, and numbers can lose leading or trailing zeros.

A few things deserve more care. Keep viewBox: removing it breaks scaling and is one of the most common causes of “the icon disappeared”. Be careful with width and height, which control the intrinsic size in HTML even when a viewBox exists. Keep title, desc, role and aria-* attributes: they exist for assistive technology and search engines, and saving a few bytes is a poor trade. Keep ids when external CSS, JavaScript or SMIL animations reference them by name; renaming ids inside the file can silently break code outside it.

Animations, filters and other exceptions

Animated SVG is the clearest case where aggressive optimisation can backfire. SMIL elements such as animate and animateTransform reference targets by id. CSS animations may target classes or ids from an external stylesheet. Filters and masks reference primitives by id inside <defs>. Collapsing groups or merging paths can break those relationships even when the first frame looks correct, because the damage appears only when the animation starts or a media query switches styling.

Files containing foreignObject, embedded raster images, scripts or event handler attributes also deserve manual review. Optimisers are designed around static vector graphics, and the more a document behaves like an application, the less predictable the result. Finally, remember that optimisation is lossy in a narrow sense: geometry may be rounded and groups may disappear. Keep the editable source in version control before replacing production assets, and test one full animation cycle in a browser rather than judging from a static preview.

Transfer compression is a different layer

Optimisation and HTTP compression solve different problems, and their percentages do not add up. SVG is repetitive text, so gzip typically removes 60–80% of it and Brotli a little more. If you optimise first, the file reaching the compressor is smaller, but it compresses slightly less efficiently because the easy repetition is already gone. The absolute transferred size still drops.

Do not advertise a 70% minification and a 75% gzip saving as a combined 145%. Benchmark the final bytes on the wire. Serve .svg with Content-Encoding and a long cache lifetime, and prefer external files for icons reused across pages so the browser can cache them once. Inlining a small icon into HTML makes it part of the document: it is compressed with the page but cannot be cached separately. Avoid base64 unless you must, because base64 adds roughly a third to the size before compression.

Automating optimization in CI

Hand optimisation drifts. A designer re-exports an asset, the old cleaned file is replaced, and nobody notices until a performance audit. A small CI step keeps the guarantee. Commit a shared configuration so the local run and the build produce the same bytes, then run SVGO over the asset directory.

// svgo.config.mjs
export default {
  multipass: true,
  plugins: [
    {
      name: 'preset-default',
      params: {
        floatPrecision: 2,
        overrides: { cleanupIds: false },
      },
    },
  ],
};
npx svgo --config svgo.config.mjs -r public/icons -o public/icons

Three habits make the pipeline trustworthy. First, keep the configuration in review: a plugin added for one file affects every file. Second, treat the optimised output as a build artifact when the source is editable elsewhere, rather than overwriting the only copy. Third, add a visual check — screenshot comparison or a manual pass at the final display size — because byte counts say nothing about whether a hairline stroke survived rounding.

When you just need to inspect one file, paste it into the SVG optimizer to see the before and after sizes with a live preview. For the raster side of the same problem, the guide to lossy versus lossless image compression explains why photographs and vector icons need different strategies.

Try it yourself: open the SVG Optimizer →