Skip to main content
ponderloop

SVG Optimizer

Paste SVG code or open an .svg file and it is optimized on the spot without changing how it looks: the example icon is 2,626 bytes and comes out at 780 bytes with the default settings, 1,846 bytes (70.3%) smaller. Metadata, editor data from Inkscape or Illustrator, comments, unused ids, empty and default attributes and surplus digits go, and the viewBox always stays. If your file has a <style>, a <script>, a <foreignObject> or animation, those are kept exactly and the rules that could break them stay off, with a notice. Both versions are drawn side by side as images, you get the gzip size, and the file is read on this page only.

Updated Runs in your browser: nothing you enter is uploadedHow this tool works

Stays in your browser

Replace the example: paste SVG code, or open or drop an .svg file (up to 1 MB). It is processed as you type.

The file is read by your browser and never uploaded or saved. Reload the page and it is gone.

Settings

Result

Optimized: 2,626 → 780 bytes, −1,846 bytes (−70.3%)

Preview

Both are drawn as images, so nothing inside the file can run. They should look the same.

The original SVG, as the browser draws it
Original
The optimized SVG, as the browser draws it
Optimized

Size

Before (UTF-8)
2,626 bytes (2.6 KB)
After (UTF-8)
780 bytes
Change
−1,846 bytes (−70.3%)
Before, gzip estimate
—
After, gzip estimate
—

What changed

  • XML declaration and DOCTYPE removed: 2
  • Comments removed: 1
  • Metadata elements removed: 1
  • Editor elements removed: 1
  • Editor attributes removed: 6
  • Unused namespace declarations removed: 5
  • Empty attributes removed: 1
  • Default values removed: 11
  • Unused ids removed: 8
  • Whitespace-only text nodes removed: 21
  • Numbers shortened or rounded: 41

Default values removed

  • display="inline" × 1
  • gradientUnits="objectBoundingBox" × 1
  • offset="0" × 1
  • opacity="1" × 1
  • opacity="1.0" × 1
  • stop-opacity="1" × 1
  • stop-opacity="1.0" × 1
  • x1="0%" × 1
  • x="0" × 1
  • y1="0%" × 1
  • y="0" × 1

Going on from here: turn the result into a data URI for CSS or HTML, or minify the stylesheet that an SVG with a style block keeps.

What each setting saves on the example icon

The example is 2,626 bytes. Each row switches on one setting alone (numbers at 3 decimals); the last row is the default combination.
SettingBytes afterSavedWhat it does
Remove the XML declaration and DOCTYPE2,474152Drops <?xml …?> and the DOCTYPE. A DOCTYPE that declares entities or attribute defaults the file uses is kept.
Remove comments2,56957Drops <!-- … --> comments, except those that start with an exclamation mark.
Remove metadata (RDF, license blocks)2,205421Drops <metadata> and RDF, Creative Commons and Dublin Core elements, then the prefixes nothing uses.
Remove editor data (Inkscape, Illustrator, Sketch)2,090536Drops elements, attributes and style declarations written by Inkscape, Illustrator, Sketch and Affinity.
Remove empty attributes2,6179Drops blank class, style and presentation or geometry attributes.
Remove default values (opacity="1", x="0")2,460166Drops attributes that repeat a default which is never inherited (opacity="1", x="0" on a rect).
Also inherited defaults (fill="black"): for images only2,57155Also drops defaults of inherited properties (fill="black", stroke-width="1") when nothing above them differs.
Collapse whitespace2,351275Drops indentation between elements and spaces inside tags; text content keeps every space.
Shorten numbers2,411215Rounds path data and lengths to the chosen decimals and writes every number in the fewest characters.
Remove unused ids2,488138Drops ids that no url(#…), href, xlink:href or aria-* attribute points at.
Remove width and height (the viewBox stays)2,59729Drops width and height from the root <svg>; the viewBox stays.
Default settings together7801,846Every setting except inherited defaults and dimensions, which change more than the file size.

Each rule on a small file

Every row is run through the tool as you see it. The input and the result show what is inside the <svg> unless the root matters; the settings column says which setting was on.
InputSettingsResultWhy
<!-- logo v2 --><path d="M1 1h8"/>Remove comments<path d="M1 1h8"/>A comment is not drawn. One that starts with an exclamation mark, such as a license line, stays.
<!--! MIT --><!-- draft --><path d="M1 1h8"/>Remove comments<!--! MIT --><path d="M1 1h8"/>The <!--! license --> form is kept on purpose.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" viewBox="0 0 10 10"><metadata><rdf:RDF/></metadata><path d="M1 1h8"/></svg>Remove metadata<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><path d="M1 1h8"/></svg>The <metadata> block and the RDF prefix it needed go. It holds license text, so keep a copy if you must credit the author.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" viewBox="0 0 10 10"><g inkscape:label="Layer 1" inkscape:groupmode="layer"><path d="M1 1h8"/></g></svg>Remove editor data<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"><g><path d="M1 1h8"/></g></svg>Attributes in the Inkscape namespace are matched by their address, not by the prefix, and the unused xmlns:inkscape goes too.
<path class="" fill="" d="M1 1h8"/>Remove empty attributes<path d="M1 1h8"/>A blank class or presentation attribute is ignored by browsers, which is what no attribute means too.
<g lang="" data-state="" href=""><path d="M1 1h8"/></g>Remove empty attributes<g lang="" data-state="" href=""><path d="M1 1h8"/></g>A blank lang, data-* or href is not the same as none, so these stay.
<rect x="0" y="0" width="5" height="5" opacity="1"/>Remove default values<rect width="5" height="5"/>x, y and opacity on a rect repeat the default.
<path fill="#000" stroke="none" d="M1 1h8"/>Remove default values<path fill="#000" stroke="none" d="M1 1h8"/>fill and stroke are inherited, so they stay unless the inherited-defaults setting is on.
<path fill="#000" stroke="none" stroke-width="1" d="M1 1h8"/>Inherited defaults on<path d="M1 1h8"/>Nothing above the path sets these, so black, none and 1 are what it would inherit anyway.
<g fill="red"><path fill="#000" d="M1 1h8"/></g>Inherited defaults on<g fill="red"><path fill="#000" d="M1 1h8"/></g>The group sets red, so the path's black is not redundant and stays.
<defs><linearGradient id="g"/></defs><path id="p" fill="url(#g)" d="M1 1h8"/>Remove unused ids<defs><linearGradient id="g"/></defs><path fill="url(#g)" d="M1 1h8"/>url(#g) uses g, nothing uses p.
<symbol id="icon"><path d="M1 1h8"/></symbol><g id="spare"/>Remove unused ids<symbol id="icon"><path d="M1 1h8"/></symbol><g/>Other files can link to a symbol as sprite.svg#icon, so symbol, view and nested svg ids stay.
<path d="M 1.50000 2.00000 L 8.12345 9.00060"/>Shorten numbers, 3 decimals<path d="M1.5 2L8.123 9.001"/>Rounded half away from zero on the digits as written, then written in the fewest characters.
<path d="M1 1a2 2 0 011 1"/>Shorten numbers, 3 decimals<path d="M1 1a2 2 0 0 1 1 1"/>The two arc flags are single characters, so "011" is read as 0, 1, 1 and written with spaces.
⏎ <g> ⏎ <text x="1" y="5">a <tspan>b</tspan> c</text> ⏎ </g> ⏎ Collapse whitespace<g><text x="1" y="5">a <tspan>b</tspan> c</text></g>Indentation between elements goes; the spaces inside the text are part of what is drawn and stay.
<style>.a{fill:red}</style><path id="p" class="a" opacity="1" d="M1 1h8"/>All defaults, <style> present<style>.a{fill:red}</style><path id="p" class="a" opacity="1" d="M1 1h8"/>Selectors in the style may use the id, the class or the opacity, so ids, empty attributes and default values stay.
<foreignObject x="0" y="0" width="10" height="10"><p xmlns="http://www.w3.org/1999/xhtml"> Hi <b>there</b> </p></foreignObject>All defaults, <foreignObject> present<foreignObject x="0" y="0" width="10" height="10"><p xmlns="http://www.w3.org/1999/xhtml"> Hi <b>there</b> </p></foreignObject>Embedded HTML is copied untouched, whitespace included.
<svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 48 48"><path d="M8 8h32"/></svg>Remove width and height<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 48 48"><path d="M8 8h32"/></svg>The viewBox keeps the proportions; the image now fills whatever size the page gives it.

Decimal places against file size

The example icon with the default settings and only the decimal places changed. The error is the most a single number can move; relative path commands add the errors of the numbers before them.
Decimal placesBytes afterSavedLargest error per number
07261,900±0.5
17621,864±0.05
27761,850±0.005
37801,846±0.0005
47901,836±0.00005
57901,836±0.000005
67901,836±0.0000005

How numbers are rounded

Half away from zero, worked on the digits as written (not on a binary float), so 2.675 at two places is 2.68 even though a JavaScript toFixed gives 2.67.
Written as0 decimals1 decimal2 decimals3 decimals6 decimals
1.00051111.0011.0005
2.67532.72.682.6752.675
-0.00040000-.0004
123.4567123123.5123.46123.457123.4567
0.51.5.5.5.5
9.9995101010109.9995
1e-50000.00001
1234567.891512345681234567.91234567.891234567.8921234567.8915

Attribute values counted as the default

An attribute is removed only when its value equals the initial value below; "0px", "0.0" and "1.00" count as 0 and 1. A property that is inherited is removed only with the inherited-defaults setting.
AttributeDefault valueInheritedApplies toRemoved by
x on <svg>0no<svg>Remove default values
y on <svg>0no<svg>Remove default values
preserveAspectRatio on <svg>xMidYMid meetno<svg>Remove default values
x on <rect>0no<rect>Remove default values
y on <rect>0no<rect>Remove default values
x on <image>0no<image>Remove default values
y on <image>0no<image>Remove default values
preserveAspectRatio on <image>xMidYMid meetno<image>Remove default values
x on <use>0no<use>Remove default values
y on <use>0no<use>Remove default values
preserveAspectRatio on <symbol>xMidYMid meetno<symbol>Remove default values
cx on <circle>0no<circle>Remove default values
cy on <circle>0no<circle>Remove default values
cx on <ellipse>0no<ellipse>Remove default values
cy on <ellipse>0no<ellipse>Remove default values
x1 on <line>0no<line>Remove default values
y1 on <line>0no<line>Remove default values
x2 on <line>0no<line>Remove default values
y2 on <line>0no<line>Remove default values
x1 on <linearGradient>0no<linearGradient>Remove default values
y1 on <linearGradient>0no<linearGradient>Remove default values
y2 on <linearGradient>0no<linearGradient>Remove default values
x2 on <linearGradient>100%no<linearGradient>Remove default values
gradientUnits on <linearGradient>objectBoundingBoxno<linearGradient>Remove default values
spreadMethod on <linearGradient>padno<linearGradient>Remove default values
gradientUnits on <radialGradient>objectBoundingBoxno<radialGradient>Remove default values
spreadMethod on <radialGradient>padno<radialGradient>Remove default values
offset on <stop>0no<stop>Remove default values
clipPathUnits on <clipPath>userSpaceOnUseno<clipPath>Remove default values
opacity1noany elementRemove default values
stop-opacity1noany elementRemove default values
flood-opacity1noany elementRemove default values
stop-colorblack (also #000, #000000, rgb(0,0,0))noany elementRemove default values
flood-colorblack (also #000, #000000, rgb(0,0,0))noany elementRemove default values
displayinlineno<svg> <g> <path> <rect> <circle> <ellipse> <line> <polyline> <polygon> <text> <tspan> <image> <use> <a> <switch>Remove default values
clip-pathnonenoany elementRemove default values
masknonenoany elementRemove default values
filternonenoany elementRemove default values
mix-blend-modenormalnoany elementRemove default values
isolationautonoany elementRemove default values
vector-effectnonenoany elementRemove default values
dominant-baselineautonoany elementRemove default values
baseline-shiftbaselinenoany elementRemove default values
unicode-bidinormalnoany elementRemove default values
text-decorationnonenoany elementRemove default values
fillblack (also #000, #000000, rgb(0,0,0))yesany elementAlso inherited defaults
fill-opacity1yesany elementAlso inherited defaults
fill-rulenonzeroyesany elementAlso inherited defaults
clip-rulenonzeroyesany elementAlso inherited defaults
strokenoneyesany elementAlso inherited defaults
stroke-opacity1yesany elementAlso inherited defaults
stroke-width1yesany elementAlso inherited defaults
stroke-linecapbuttyesany elementAlso inherited defaults
stroke-linejoinmiteryesany elementAlso inherited defaults
stroke-miterlimit4yesany elementAlso inherited defaults
stroke-dasharraynoneyesany elementAlso inherited defaults
stroke-dashoffset0yesany elementAlso inherited defaults
visibilityvisibleyesany elementAlso inherited defaults
text-anchorstartyesany elementAlso inherited defaults
font-stylenormalyesany elementAlso inherited defaults
font-weightnormal (also 400)yesany elementAlso inherited defaults
letter-spacingnormalyesany elementAlso inherited defaults
word-spacingnormalyesany elementAlso inherited defaults
shape-renderingautoyesany elementAlso inherited defaults
text-renderingautoyesany elementAlso inherited defaults
image-renderingautoyesany elementAlso inherited defaults
color-interpolationsRGByesany elementAlso inherited defaults
color-interpolation-filterslinearRGByesany elementAlso inherited defaults
pointer-eventsvisiblePaintedyesany elementAlso inherited defaults
paint-ordernormalyesany elementAlso inherited defaults
directionltryesany elementAlso inherited defaults
marker-startnoneyesany elementAlso inherited defaults
marker-midnoneyesany elementAlso inherited defaults
marker-endnoneyesany elementAlso inherited defaults
cursorautoyesany elementAlso inherited defaults

Namespaces treated as editor data or metadata

Matched by the namespace address the prefix points at, so a renamed prefix is still found.
SourceNamespace addressRemoved by
Inkscapehttp://www.inkscape.org/namespaces/inkscapeRemove editor data
Sodipodi (Inkscape)http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtdRemove editor data
Adobe Illustrator and Adobe extensionsany address starting with http://ns.adobe.com/Remove editor data
Sketchany address starting with http://www.bohemiancoding.com/sketch/Remove editor data
Serif Affinityhttp://ns.serif.com/, or any address starting with http://www.serif.com/Remove editor data
RDFhttp://www.w3.org/1999/02/22-rdf-syntax-ns#Remove metadata
Creative Commonshttp://creativecommons.org/ns# or http://web.resource.org/cc/Remove metadata
Dublin Corehttp://purl.org/dc/elements/1.1/ or http://purl.org/dc/terms/Remove metadata

Worked examples: eight typical files

Small files written in the style of common tools, run with the default settings. Sizes are UTF-8 bytes.
FileBytes beforeBytes afterSavedWhat stays
Inkscape leaf icon1,7786651,113 (62.6%)The gradient the fill uses and its id; the viewBox.
Illustrator export with DOCTYPE entities1,363924439 (32.2%)The DOCTYPE, because xmlns="&ns_svg;" needs its entity; the foreignObject block untouched.
Sketch export1,078569509 (47.2%)The title and desc text, and the stroke and fill that differ from the default.
Figma-style stroke icon52850820 (3.8%)Everything that matters: root fill="none", the clip path and its id.
Icon sprite with symbols816674142 (17.4%)The ids of the symbols, the view and the nested svg.
Bar chart styled with a <style> block90384162 (6.9%)The style block, every id, class and opacity (a notice says why).
Animated spinner (SMIL)82172398 (11.9%)The animation elements, their ids and begin targets.
Text logo with gradient, clip path and mask918786132 (14.4%)The spaces inside the text, the gradient chain, mask and clip path references.

When the optimizer holds back

What the file contains decides what is allowed to run on it. These cases are shown as notices under the result.
The file hasKept as writtenSwitched off for that file
A <style> element or an xml-stylesheet linkThe stylesheet, every id, class and attributeUnused ids, empty attributes, default values
A <script> elementThe script and every attribute it could readUnused ids, empty attributes, default values
Event attributes such as onclickThe attributes and what they callUnused ids, empty attributes, default values
Animation (<animate>, <set>, <animateTransform>)The animation and the ids it targetsUnused ids, empty attributes, default values
A <foreignObject>Everything inside it, whitespace and ids includedAll rules, inside it only
<use> elementsInherited properties on shapes that may be instancedInherited defaults
Ids on <symbol>, <view> or a nested <svg>Those idsUnused ids, for them only
A DOCTYPE with entities or attribute defaultsThe DOCTYPEProlog removal, for the DOCTYPE only
Text, tspan, title, descEvery space and line break insideWhitespace collapse, inside them only

How to optimize an SVG file online

  1. Add your SVG. Paste the code into the box, or press Open an .svg file (or drop a file on the box). Files up to 1,048,576 bytes (1 MB) are accepted.
  2. Choose the settings. The defaults remove metadata, editor data, comments, unused ids, empty and default attributes and extra digits. Change the decimal places for path data, or turn a setting off if you need what it removes.
  3. Read the notices. If the file has a style block, a script, animation or embedded HTML, the notices say what was kept and which settings were skipped for safety.
  4. Compare the preview. The original and the optimized SVG are drawn next to each other as images. They should look the same; the sizes and the gzip estimate show what you saved.
  5. Copy or download. Copy the optimized code, or download it as an .svg file named after your input. Nothing is uploaded at any step.

Frequently asked questions

Will optimizing change how my SVG looks?
The settings that are on by default only remove things a browser does not draw (comments, metadata, editor data, blank attributes, ids nothing refers to) or write the same value in fewer characters. The one lossy step is rounding numbers: three decimals is far below a pixel for icons drawn on a 24 to 100 unit grid, but a very large or very small viewBox may need more or fewer. Compare the two previews, and lower the setting if you see a difference.
Why was nothing removed from my file with a style block?
A <style> element can select by id, class, attribute and element name, so removing an id, a class or an attribute that looks unused or redundant could change which rule applies. With a <style> (or a linked stylesheet, a script, event handlers or animation) the optimizer keeps the file's ids, empty attributes and default values, and still removes comments, metadata, editor data and whitespace and rounds numbers. The style text itself is copied exactly; minify the CSS separately.
When should I turn off "Remove unused ids"?
Turn it off if something outside the file refers to its ids: a page that links to sprite.svg#icon, CSS or JavaScript on the page that inlines this SVG and selects #logo, or a tool that expects layer names. Ids on <symbol>, <view> and nested <svg> elements are kept automatically because sprites are linked that way. Ids that url(#id), href, xlink:href or aria-labelledby inside the file point at are always kept.
Why are fill="black" and stroke-width="1" still there?
Both properties are inherited, so a value that equals the default is only redundant when nothing around the SVG sets it. If the SVG is inlined in a page whose CSS says svg { fill: currentColor }, an explicit fill="#000" on a shape is what keeps it black. The setting "Also inherited defaults" removes them when you know the SVG is used as an image or a file; it still keeps a value when an ancestor in the file sets another one, and does nothing when the file has <use> elements.
How many decimal places should I keep?
For icons on a 16 to 64 unit grid, 2 or 3 is plenty. For larger drawings, maps and charts with a viewBox in the thousands, 1 or 2 is enough, because one unit is a small fraction of the picture. For tiny viewBoxes such as 0 0 1 1, keep 4 to 6. The error table above gives the largest movement of a single number; because relative path commands (lowercase letters) add up, very long relative paths drift a little more than absolute ones.
Is this different from SVGO?
SVGO is a command-line optimizer with a long list of plugins that can also convert shapes to paths, merge paths and move attributes between elements. This page implements a smaller set of rules that never restructure the drawing: it removes, respells and rounds, and leaves the element tree, the order of elements and the path commands as they are. That makes the result easy to diff against your source, at the price of leaving some bytes on the table.
What does the preview prove?
Both versions are handed to the browser as images, the same way an <img> tag would show them, so scripts in the file cannot run and nothing is loaded from other addresses. If the two pictures look alike at the size you use, the optimization did not change the drawing there. An <img> does not run the animation scripts or fetch fonts and pictures that the file refers to; if your SVG depends on those, test the downloaded file in its real page too.
What is the gzip number?
It is an estimate made with your browser's built-in compression of the text before and after. Web servers usually send SVG compressed, so the compressed size is what a visitor downloads, and it shrinks less than the raw size because repeated markup compresses well already. A server may use a different gzip level or Brotli, so treat the figure as a guide to the comparison, not a promise. If the browser has no compression support the line is left out.
What happens when I remove width and height?
The image no longer has a fixed size of its own. It fills the width a page gives it (an <img> with no CSS size shows at a default size) and keeps its proportions from the viewBox. This is the usual choice for responsive icons. If the file has no viewBox but plain pixel sizes, one is made from them first; if neither can be used, the sizes are kept and a notice says so.
Why does my SVG say it cannot be read?
The messages name the line and column. The usual causes are a closing tag that does not match, an & that is not part of an entity (write &amp;), an entity such as &nbsp; that XML does not define (use &#160;), a prefix like xlink: used without declaring xmlns:xlink, a missing xmlns="http://www.w3.org/2000/svg" on the <svg> (browsers will not draw the file as an image without it), and an XML declaration that is not the very first thing in the file. The browser's own parser is asked as well, and its message is shown if it disagrees.
Does it remove the title and desc elements?
No. <title> and <desc> are what screen readers announce and what a browser shows as a tooltip, so they are treated as content. An aria-labelledby that points at a title keeps the title's id. If you want them gone for a decorative image, delete them in the code box first.
Is my file sent anywhere, and what is the size limit?
The code is processed by script on this page, held in memory for as long as the page is open and dropped when you reload; the drawing of both versions goes through blob addresses made in your browser. Input is limited to 1,048,576 bytes (1 MB), measured as UTF-8, so the page stays responsive; a larger file is refused before anything is read.

How it works

The text is read into a tree by a small XML 1.0 reader that keeps every string exactly as written (entity references are never decoded) and resolves namespaces, because editor data is recognised by its namespace address and not by its prefix. The browser's DOMParser (image/svg+xml) is asked about the same text, and a file either one rejects is not processed. Each setting is a removal or a respelling. Whether a rule may run is decided by the file: a style element, a linked stylesheet, a script, an event handler or animation turns off the rules that depend on ids, classes and attribute values; a foreignObject is copied untouched; xml:space and text elements keep their whitespace. Defaults are the initial values from the SVG property index; properties that are inherited are handled separately because a value equal to the default is only redundant when no ancestor, no CSS and no instancing <use> supplies another. An id counts as used when a url(#id), href, xlink:href, aria-labelledby or similar attribute anywhere in the file points at it. Numbers are rounded as decimal text, half away from zero, never through binary floating point; path data is read with the SVG path grammar, including arc flags that touch the next number, and written with the fewest separators. Opacities, offsets, transforms and the viewBox are only respelled (1.0 to 1), never rounded, because rounding them would change the picture. After the rules run, the result is parsed again; if it were not well-formed the page would refuse to show it. Sizes are UTF-8 byte counts.

Sources

More: Web, URLs, images and networkAll developer toolsAll toolsHome & DIY calculators