The SVG format has the advantage of being able to describe complex images while being lightweight and to scaling them very well at all resolutions. Unfortunately, a lot of SVG files (often generated by SVG editors) ship without being cleaned up, so the benefit of them being lightweight is gone. Here are some best pratices to keep SVGs lightweight. These rules are based on some real examples seen in Mozilla's code.
In addition to trailing whitespace at the end of lines, there are a few more cases more specific to SVGs:
- Trailing whitespaces in attribute values (usually seen in path definitions)
- Excessive whitespace in path or polygon points definition
Similarly, this polygon:
You should only use line breaks for logical separation or if they help make the file readable. You should avoid line breaks between every single element or within attribute values. It's recommended to put the attributes on the same line as their tagnames if possible. You should also put the shortest attributes first so they are easier to spot.
… Metadata can mean many things, including:
- The common "Created with editor" comments
- Non-standard editor specific tags and attributes (
- The XML namespace definition that comes with the latter (
In addition to non-standard editor metadata, standard compliant metadata also exists. Common examples of this are
<desc> tags. Although this kind of data is supported by the browser, it can only be displayed when the SVG is opened in a new tab. Plus, the filename is descriptive enough most of the time. So it's recommended to remove that kind of metadata, since it doesn't bring much value.
You shouldn't include DOCTYPEs in your SVGs either, they are source of many issues and the SVG WG recommends not to include them. See SVG Authoring guidelines.
There are 2 kinds of invisible shapes: the offscreen ones and the uncolored ones.
The offscreen shapes are hard to spot, even with an automated tool, and are usually context aware. Those kind of shapes are visible, but off the SVG view box. Here's an example of a file with offscreen shapes.
On the other hand, the uncolored ones are easier to spot, since they usually come with styles making them invisible. They must meet 2 conditions: they must have no fill (or a transparent one) and no stroke.
<svg> element can also host many useless attributes. Here's an example taking into account the list below:
- Empty tags, this may be obvious, but those are sometimes found in SVGs
Here are some examples for excessive number precision:
- t →
As for descriptive IDs:
Group similarly styled shapes under one
<g> tag, this avoids having to set the same class/styles on many shapes.
Editors can sometimes do excessive grouping when exporting SVGs. This is due to the way editors work.
Avoid multiple-level nesting of groups, these make the SVG less readable.
Some editors use
<g> tags to do nested transforms, which is usually not needed. You can avoid this by doing basic algebra, for example:
These rules are optional, but they help speeding up the SVG.
- Instead of using CSS/SVG transforms, apply directly the transform on the path/shape definition
Tools can help clean SVG files. Note however that some of the rules stated above can be hard to detect with automated tools since they require too much context-awereness.
To this date, there doesn't seem to be a tool that handles all of the above. However, there are some existing utilities that cover some parts of this document:
- Mostly complete command line tool: https://github.com/svg/svgo
- GUI for command line tool (use with "Prettify code" and "Remove
<title>" options on): https://jakearchibald.github.io/svgomg/
- Good alternative to SVGO/SVGOMG: http://petercollingridge.appspot.com/svg-editor