How to Optimize SVG Safely: Smaller Files, Same Image
Short answer: to optimize SVG files without breaking them, start from the fact that SVG files exported from design tools carry comments, editor metadata and a DOCTYPE that a browser does not need. Paste your file into the SVG Optimizer and it strips them. The built-in example drops from 421 bytes to 289 bytes, a saving of 31%, and the picture stays the same.
What the optimizer removes
- comments;
- metadata blocks;
- the DOCTYPE;
- extra whitespace (in minify mode).
What it deliberately leaves alone
Safe optimization is as much about what is not touched.
- Path geometry is not rewritten. The shapes are not simplified, so the drawing cannot change shape.
- IDs are kept, because CSS, scripts and
<use>references may depend on them. - Number rounding is off by default. You can set a precision if you accept slightly less exact coordinates.
Four modes
- Optimize: removes the unneeded parts.
- Beautify: re-indents the file so it is readable.
- Minify: compacts it to the smallest form.
- Validate: checks that the file is well-formed SVG.
The preview is safe
The preview shows the image through an <img> element using a data URI. Images loaded that way never run scripts, so even an untrusted SVG cannot execute code while you look at it. That is a protection for the preview, not a cleaning step: the tool is not a security sanitiser, and an SVG with scripts should not be published as-is. For inspecting what an SVG file hides, see also the Image Metadata Viewer and the guide on removing hidden metadata from images.
How to optimize SVG with the SVG Optimizer
- Open the SVG Optimizer.
- Paste your SVG code.
- Choose optimize, beautify, minify or validate.
- Compare the before and after sizes and look at the preview.
- Copy or download the result, and test it on your page before replacing the original.
Limits to know
- It does not simplify or redraw paths, so some tools can shrink a file further.
- Rounding is off unless you enable it.
- It is not a security sanitiser.
Frequently asked questions
How do I reduce SVG file size? Remove comments, metadata and the DOCTYPE, and minify the markup.
Will optimizing change how my SVG looks? Not with the default settings, because paths are not rewritten and rounding is off.
Does it keep my IDs? Yes.
Is the SVG preview safe? It is rendered as an image, which never runs scripts.
Should I still compress other images? Yes. Use the Image Compressor / Converter for photos and bitmaps, and read how to compress JPEG, WebP and PNG images without losing quality you need.