Open an SVG exported from a vector editor and the code often looks like it was written for the editor, not for your website. Before the first shape appears there can be an XML comment, a metadata block, elements named after the tool that made the file, and groups that contain nothing at all. None of it draws a single pixel, but every byte of it downloads with the file, on every page that uses the icon.
Optimizing SVG code means removing that dead weight and leaving the drawing as it was. This guide explains where the extra bytes hide, what a safe cleanup rule is allowed to touch, and how to run the free SVG Code Optimizer on Ovatly, which shows you both sizes and lists every rule that changed something.
Contents
- Quick answer
- Where the extra bytes come from
- The cleanup rules, one by one
- Why the picture cannot change
- How to optimize an SVG, step by step
- Worked example: an Inkscape export, cleaned
- Conservative on purpose: this is not SVGO
- Limitations and honest tradeoffs
- Common mistakes
- FAQs
- Conclusion
- References
Quick answer
Paste your SVG code into the SVG Code Optimizer, or load the .svg file, keep the default rules ticked, and press Optimize SVG. The defaults take comments out, remove editor leftovers and empty groups, shorten hex colors and collapse stray whitespace, while keeping the XML declaration. The result panel shows the before and after sizes, the saving percentage and the list of rules that actually fired, with previews of both versions so you can confirm the picture is unchanged before you download it.
Where the extra bytes come from
A vector editor saves a working document, not a delivery file. It writes down everything it might need when you reopen the project: its own namespaced elements, a metadata block, comments, and the group structure, including groups that ended up empty. Browsers ignore almost all of it when they render the file. They have to download it first, though.
The drawing itself, the paths and shapes, is usually a minority of an export's bytes for simple icons and logos. That is why a cleanup pass on a small file is worth doing: the savings repeat on every page view.
The cleanup rules, one by one
The optimizer works through a small set of rules, and you tick the ones you want:
- Comments out. XML comments carry notes for humans. Browsers never render them, so they are pure weight in a shipped file.
- Editor leftovers removed. Elements from the
sodipodiandinkscapenamespaces, and metadata blocks, describe the editing session, not the picture. Removing them shrinks the file without changing the drawing; you only lose the ability to recover those editor settings from this copy. - Empty groups dropped. A
<g>element with nothing inside it draws nothing. Exports accumulate them as layers are emptied and rearranged. - Hex colors shortened. Where a six-digit hex color can be written in three digits without changing the color, it is:
#ffffffbecomes#fff. The color is identical; only its spelling shrinks. - Stray whitespace collapsed. Indentation and blank lines between tags help humans read code and cost bytes on every download. Runs of whitespace between tags are collapsed to the minimum.
One rule is off by default: removing the XML declaration. It is kept unless you choose otherwise, because standalone files opened directly by other software sometimes rely on it.
Why the picture cannot change
The safety of this tool comes from what it refuses to touch. Path data, shapes, text and attribute values are never rewritten. The geometry of your drawing lives in path data and shape attributes, so if those bytes are untouched, the picture cannot change shape, move or lose a detail. Only the markup around the drawing is cleaned, plus the spelling of hex colors, which resolves to the same color.
That is also why the previews matter. The page shows the original and the optimized version side by side, so the claim that nothing changed is one you can check before downloading, not one you have to take on trust.
How to optimize an SVG, step by step
- Open the SVG Code Optimizer and paste your SVG code, or load an .svg file with the file box.
- Review the ticked rules. The defaults are conservative and are the right choice for most exports; add the XML declaration rule only if the file will live inline in HTML.
- Press Optimize SVG and read the result panel: both sizes, the saving percentage and the list of rules that actually changed something.
- Compare the previews. If they match, copy the optimized code or download it, and keep your original file as the editable source.
Worked example: an Inkscape export, cleaned
Take an exported icon of 393 bytes. It carries an XML comment, a metadata block, a sodipodi:namedview element, an empty group and a rectangle filled with #ffffff. Run it through the default rules and it comes out at 187 bytes, a 52.4 percent saving.
The result panel lists the five rules that fired, one for each kind of leftover. The rectangle itself is untouched except for its fill, which is now #fff, the same white in shorter spelling. More than half the file was never picture at all.
Conservative on purpose: this is not SVGO
A full minifier such as SVGO goes much further: it renames IDs, merges paths and rounds coordinates to fewer decimal places. Those tricks save more bytes, but they change the drawing's underlying data, which can break a shape, an animation hook or a CSS reference.
This optimizer deliberately does none of that. Its savings are smaller, and in exchange its behaviour is predictable: the list of rules that fired is the complete list of what changed. For icons and logos shipped on a website, that trade is usually the right one. If you need the aggressive end of optimization, run a full minifier on a copy and compare its output carefully, rather than expecting this tool to be something it is not.
Limitations and honest tradeoffs
The conservative rule set is the main limitation: an already clean, hand-written SVG may save almost nothing, because there is no editor baggage to remove. The tool also cannot judge whether a group is empty of meaning or just empty of elements, so check files whose structure matters to your CSS.
Animated and scripted SVGs need care. The optimizer does not touch attribute values, scripts or style contents, but whitespace inside a <style> block can be code, and the whitespace rule collapses runs of it. The page carries a warning for such files: compare the preview carefully and keep your original either way.
Common mistakes
- Optimizing the only copy. Keep the original export. The cleaned file is for shipping, not for editing, because the editor settings are gone from it.
- Assuming a big saving means a better file. The percentage mostly measures how much baggage the editor left. A small saving on a clean file is a good result.
- Removing the XML declaration on a standalone file. Files opened directly by other software may rely on it. Only tick that rule for inline use.
- Skipping the preview on an animated SVG. Whitespace inside style blocks can be meaningful. Look before you ship.
- Expecting minifier-level savings. No IDs are renamed and no paths are merged here. Predictability is the feature.
FAQs
Will optimizing change how my SVG looks? No. The rules remove markup around the drawing and respell values without changing them, for example #ffffff becomes #fff. Path data, shapes, text and attribute values are never rewritten, and the page shows before and after previews so you can confirm the picture is identical.
Is this the same as SVGO? No. This is a deliberately conservative rule set. It does not rename IDs, merge paths or round coordinates, so its savings are smaller than a full minifier, but every change is visible and predictable.
Can I choose which rules run? Yes. Each cleanup rule has its own tick box, and the defaults are conservative. You can leave a rule off if you want that part of the markup kept, and the result panel lists only the rules that actually changed something.
Why is the XML declaration kept by default? Standalone .svg files opened directly by other software sometimes rely on the declaration. If your SVG will be pasted inline into HTML instead, tick the declaration rule as well and save a few more bytes.
Is it safe to optimize an animated or scripted SVG? Be careful. The optimizer does not rewrite attribute values, scripts or style contents, but whitespace inside a style block can be code. Compare the preview carefully before using the output, and keep your original file either way.
Conclusion
An editor export is a working document wearing a delivery file's clothes. The SVG Code Optimizer strips the comments, leftovers and empty structure, shortens what can be shortened, and shows you the sizes, the rules that fired and both previews before you commit. When a download matters more than its fingerprint, our companion guide to checking a file's hash covers the verification side. For more guides in this series, see our File and Data Conversion guides.
References
- MDN Web Docs, on SVG and how browsers render it: https://developer.mozilla.org/en-US/docs/Web/SVG
- W3C, the SVG 2 specification: https://www.w3.org/TR/SVG2/