SVG (Scalable Vector Graphics) is the preferred format for icons, logos, illustrations, and diagrams on the web. Unlike raster images (PNG, JPEG), SVGs are resolution-independent, scale perfectly at any size, and can be styled with CSS and manipulated with JavaScript. However, SVG files exported from design tools often contain significant amounts of unnecessary data.
A typical SVG exported from Adobe Illustrator or Inkscape can be 2-5x larger than necessary. This bloat comes from editor metadata, XML namespaces, comments, empty groups, default attribute values, and verbose coordinate data. Optimizing SVGs removes this unnecessary data without changing the visual output.
Understanding SVG structure helps you identify what can be safely removed. A typical SVG from a design tool looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"
"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Generator: Adobe Illustrator 27.0 -->
<svg xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:i="http://ns.adobe.com/AdobeIllustrator/10.0/"
version="1.1" x="0px" y="0px"
width="100px" height="100px"
viewBox="0 0 100 100"
xml:space="preserve">
<metadata>
<i:pgf>...long metadata...</i:pgf>
</metadata>
<title>icon</title>
<desc>Created with Illustrator</desc>
<g id="Layer_1">
<g>
<circle fill="#326CE5" cx="50" cy="50" r="40"/>
</g>
</g>
</svg>
After optimization, this becomes:
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 100 100"><circle fill="#326CE5" cx="50" cy="50" r="40"/></svg>
SVG optimization targets several categories of unnecessary data:
| Category | Examples | Impact |
|---|---|---|
| XML declarations | <?xml ...?>, DOCTYPE | Not needed for inline or img SVGs |
| Comments | <!-- Generator: ... --> | No visual impact |
| Metadata | <metadata>, <title>, <desc> | Can be large; title/desc for accessibility |
| Editor data | inkscape:*, sodipodi:*, i:* | Tool-specific, never rendered |
| Empty groups | <g></g> | No content, no visual effect |
| Hidden elements | display:none, visibility:hidden | Not visible but adds file size |
| Default attributes | fill-opacity="1", stroke="none" | Same as browser defaults |
| Whitespace | Spaces, newlines, indentation | No visual impact |
The first and often most impactful optimization is removing metadata and comments:
<!-- Remove: XML processing instruction -->
<?xml version="1.0" encoding="UTF-8"?>
<!-- Remove: DOCTYPE declaration -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" ...>
<!-- Remove: Generator comments -->
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<!-- Remove: Metadata elements -->
<metadata>
<rdf:RDF>
<cc:Work>...</cc:Work>
</rdf:RDF>
</metadata>
If your SVG is used as a meaningful image (not decorative), keep <title> and <desc> for accessibility. Screen readers use these elements to describe the SVG content. For decorative icons, they can be safely removed and replaced with aria-hidden="true" on the SVG element.
Design tools add their own namespaces and attributes that serve no rendering purpose:
<!-- Inkscape: remove these namespaces and attributes -->
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.0.dtd"
inkscape:version="1.2"
sodipodi:docname="icon.svg"
<!-- Illustrator: remove these -->
xmlns:i="http://ns.adobe.com/AdobeIllustrator/10.0/"
xmlns:x="http://ns.adobe.com/Extensibility/1.0/"
<!-- Common unnecessary attributes -->
xml:space="preserve"
enable-background="new 0 0 100 100"
x="0px" y="0px"
version="1.1"
After removing unnecessary elements, minify the remaining SVG code:
<!-- Before: readable but large -->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
<path
d="M12 2 L22 12 L12 22 L2 12 Z"
fill="#326CE5"
stroke="none"
/>
</svg>
<!-- After: minified -->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 2 22 12 12 22 2 12Z" fill="#326CE5"/></svg>
<!-- Before -->
fill="#ffffff"
fill="#000000"
fill="#336699"
<!-- After -->
fill="#fff"
fill="#000"
fill="#369"
Remove attributes that match browser defaults:
<!-- Default values: can be removed -->
fill-opacity="1" <!-- default is 1 -->
stroke-opacity="1" <!-- default is 1 -->
opacity="1" <!-- default is 1 -->
fill-rule="nonzero" <!-- default is nonzero -->
stroke="none" <!-- default is none -->
stroke-width="1" <!-- default is 1 -->
stroke-linecap="butt" <!-- default is butt -->
stroke-linejoin="miter" <!-- default is miter -->
stroke-dasharray="none" <!-- default is none -->
stroke-dashoffset="0" <!-- default is 0 -->
Path data (d attribute) is often the largest part of an SVG. Optimization techniques include:
<!-- Before: verbose path data -->
d="M 10.000 20.000 L 30.000 20.000 L 30.000 40.000 L 10.000 40.000 Z"
<!-- After: optimized -->
d="M10 20h20v20H10z"
<!-- Techniques used:
1. Remove unnecessary spaces
2. Remove trailing zeros (10.000 -> 10)
3. Use relative commands (l -> lowercase)
4. Use shorthand commands (H, V for horizontal/vertical)
5. Remove spaces between command and number
-->
Reducing decimal precision can significantly shrink path data without visible quality loss:
<!-- Before: 6 decimal places -->
d="M12.345678 23.456789L34.567890 45.678901"
<!-- After: 2 decimal places -->
d="M12.35 23.46L34.57 45.68"
<!-- After: 1 decimal place (for icons) -->
d="M12.3 23.5L34.6 45.7"
For production workflows, integrate SVG optimization into your build process:
// Install
npm install svgo
// CLI usage
npx svgo input.svg -o output.svg
// svgo.config.js
module.exports = {
plugins: [
'preset-default',
'removeDimensions',
{ name: 'removeViewBox', active: false },
{ name: 'removeTitle', active: false },
],
};
// webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.svg$/,
use: [
{ loader: 'file-loader' },
{
loader: 'svgo-loader',
options: {
plugins: [{ name: 'preset-default' }],
},
},
],
},
],
},
};
// vite.config.js
import { defineConfig } from 'vite';
import svgr from 'vite-plugin-svgr';
export default defineConfig({
plugins: [
svgr({
svgrOptions: {
svgo: true,
svgoConfig: {
plugins: ['preset-default'],
},
},
}),
],
});
Choose the right SVG loading strategy based on your use case:
<!-- Inline: no HTTP request, can style with CSS -->
<button>
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16"
viewBox="0 0 16 16" fill="currentColor">
<path d="M8 0L16 8 8 16 0 8z"/>
</svg>
Click me
</button>
<!-- img tag: cacheable, no CSS access -->
<img src="/icons/diamond.svg" alt="Diamond" width="16" height="16">
<!-- CSS background: cacheable -->
.icon { background-image: url('/icons/diamond.svg'); }
<!-- use tag: cacheable, limited styling -->
<svg><use href="/icons/sprite.svg#diamond"/></svg>
When optimizing SVGs, preserve accessibility attributes:
<!-- Decorative SVG: hide from screen readers -->
<svg aria-hidden="true" xmlns="..." viewBox="...">
<path d="..."/>
</svg>
<!-- Meaningful SVG: add title and role -->
<svg role="img" aria-labelledby="icon-title"
xmlns="..." viewBox="...">
<title id="icon-title">Search</title>
<path d="..."/>
</svg>
<!-- Interactive SVG: add focusable and label -->
<button aria-label="Close">
<svg aria-hidden="true" focusable="false"
xmlns="..." viewBox="...">
<path d="..."/>
</svg>
</button>
Our free SVG Optimizer lets you optimize SVG files directly in your browser without installing any software.
Paste your SVG code and optimize it instantly. Remove metadata, editor data, and whitespace to reduce file size by 30-70% -- all in your browser.
Try the SVG OptimizerLearn CSS minification techniques and performance optimization.
Learn JavaScript minification for smaller bundles and faster pages.
Format, validate, and beautify XML documents online.