How to Optimize SVG Files for the Web (Smaller, Cleaner, Faster)
A logo exported straight from Illustrator or Figma is often three to five times larger than it needs to be. The extra weight isn't the artwork. It's editor metadata, coordinate numbers carried to twelve decimal places, invisible layers, and sometimes an entire PNG embedded inside the "vector." Cleaning that out is one of the highest-return, lowest-risk optimizations you can do, because an SVG icon that ships at 2 KB instead of 9 KB is smaller than the HTTP overhead of requesting it.
This is a practical walkthrough of what bloats an exported SVG, how to strip it safely, and which "optimizations" to leave alone because they break things. If you're generating logos to optimize, our Free SVG Logo Maker guide covers the design side; this post is about what to do with the file afterward.
What actually makes exported SVGs huge
Open a raw export in a text editor and you'll usually find four kinds of junk.
Editor metadata. Illustrator writes an <metadata> block, sodipodi and inkscape namespaces, and a comment banner. Figma adds its own attributes. None of it renders. All of it ships to every visitor.
Absurd decimal precision. Coordinates like d="M12.34567890123 8.98765432109" describe a point far more precisely than any screen can display. Rounding to one or two decimals is visually identical and can cut path data by a third.
Hidden and empty layers. Design files accumulate hidden layers, empty groups, and off-canvas scratch shapes. They all export unless you cleaned up first.
Embedded rasters. This is the sneaky one. If someone placed a PNG into the artboard and exported as SVG, the "vector" now contains a base64-encoded bitmap in an <image> tag. The file is huge and doesn't scale. Search the source for data:image/png;base64 to catch it.
The optimization passes that matter
Cleaning an SVG is a sequence of transforms, each safe on its own. In rough order of payoff:
- Strip metadata, comments, editor namespaces, and the XML declaration.
- Round numeric precision to 1 or 2 decimal places.
- Remove hidden elements (
display:none), empty groups, and unused<defs>. - Collapse redundant groups and merge transforms into path data.
- Shorten and merge
idvalues, or remove unreferenced ones. - Convert inline
styleattributes to presentation attributes where shorter.
You can do all of this by hand for a single icon, but for anything more you want a tool.
Running SVGO (and its two real gotchas)
SVGO is the standard optimizer. Its defaults do most of the list above and typically cut file size 40 to 60 percent on a fresh export. You can run it as a CLI, a build step, or through many online interfaces.
The defaults are good, but two of them cause real problems, so know them before you batch-process a folder.
removeViewBox. This is the notorious one. SVGO's default configuration removes the viewBox attribute when a width and height are present. That breaks responsive scaling: without a viewBox, the SVG won't scale to its container and can render at a fixed size or collapse to nothing. Turn this off. In an svgo.config.js, set removeViewBox: false in the preset overrides. It's the single most common way people "optimize" an SVG into a broken one.
cleanupIds / prefixIds. If your SVG is injected inline into a page alongside other SVGs, shared id values (for gradients, clip paths, filters) can collide and cause one icon's gradient to leak into another. If you inline SVGs, keep id prefixing on so each file's references stay unique.
After optimizing, always look at the result. Which brings up the fastest way to check.
Preview the cleaned output before you ship it
The failure mode with SVG optimization is silent: the file gets smaller and looks fine in your editor's thumbnail, but a rounded viewBox or a stripped attribute broke it somewhere. Before committing an optimized icon, paste it into our SVG Viewer and compare it against the original. You see the rendered result and the source together, so you can confirm the artwork survived and eyeball how much markup you actually removed. Because it runs locally in the browser, you can check a client's assets without uploading them anywhere.
Minify and gzip: what's real and what's double-counting
Minifying an SVG (removing whitespace and newlines) helps a little. But if your server sends assets with gzip or Brotli compression, and it should, most of the whitespace savings are already captured by transport compression. Newlines and repeated attribute names compress extremely well.
So the honest split is this. Structural cleanup (removing metadata, rounding numbers, dropping hidden layers) reduces the actual information in the file and shrinks it even after gzip. Whitespace minification mostly overlaps with gzip and adds little on top. Do the structural work; don't obsess over stripping every space. Confirm compression is on by checking for a content-encoding: gzip or br response header on your .svg files.
Inline versus file: the tradeoff
You can serve an SVG two ways, and it changes what optimization you want.
As an external file (<img src="logo.svg"> or CSS background), the browser caches it once and reuses it across pages. Best for anything repeated site-wide, like a logo. Here you optimize for file size and let the cache do the rest.
Inline in the HTML (<svg>...</svg> directly in the markup), you save an HTTP request and can style or animate the SVG with CSS, but the bytes reload on every page because they're part of the document. Best for critical, above-the-fold icons or single-use illustrations. Here id collisions matter and every byte is in your HTML, so clean aggressively.
A rough rule: repeated assets go in external cached files, one-off critical icons go inline.
Don't skip accessibility on the way out
Optimizers can strip the elements that make an SVG readable to assistive tech, so add them back or protect them. A meaningful SVG (a logo, an informative icon) should carry a <title> as its first child, which screen readers announce, and optionally a <desc> for longer description. For an SVG used as a standalone image, add role="img" and an aria-label on the root so it's announced as one image rather than a pile of shapes.
A purely decorative icon should go the other way: mark it aria-hidden="true" so screen readers skip it entirely rather than reading out "graphic." The mistake is leaving it ambiguous, where assistive tech has to guess. For how vector graphics fit the wider image-format decision, see SVG vs PNG vs WebP for SEO.
Frequently Asked Questions
How much smaller can I make an SVG file?
A fresh export from a design tool typically shrinks 40 to 60 percent after structural optimization with SVGO defaults, and more if it contained embedded rasters or heavy metadata. The savings come from removing editor junk, rounding coordinate precision, and dropping hidden layers, not from removing the actual artwork.
Why did my SVG break after optimizing it?
The most common cause is SVGO's default removeViewBox, which strips the viewBox attribute and breaks responsive scaling. Set removeViewBox: false in your config. The second most common cause is id collisions between inlined SVGs that share gradient or clip-path IDs. Preview the optimized file to catch these before shipping.
Should I minify SVGs if my server already uses gzip?
Structural cleanup (metadata, precision, hidden layers) still helps because it reduces real information and shrinks the file even after gzip. Pure whitespace minification mostly overlaps with gzip and adds little, so prioritize the structural passes over stripping every space.
Is it better to inline SVGs or link them as files?
Link repeated assets like logos as external files so the browser caches them once across pages. Inline critical, above-the-fold, or single-use icons to save an HTTP request and allow CSS styling. Inlined SVGs need unique IDs to avoid collisions, and their bytes reload on every page.
What accessibility tags does an SVG need?
A meaningful SVG should have a <title> as its first child, plus role="img" and an aria-label on the root when used as a standalone image. A decorative icon should instead have aria-hidden="true" so screen readers skip it. The goal is to make each SVG either clearly informative or clearly decorative, never ambiguous.