When used as a document format there is usually a compelling reason that makes SVG the only solution. When used as an image format, it is sometimes less obvious whether it would be best to use SVG or a raster image format for any given image. The vector format SVG and raster formats like PNG both have their place. When choosing whether or not to use SVG it is best to understand the advantages and disadvantages of both.
A lot of SVG files (particularly those generated by SVG editors) ship without being cleaned up and can contain a ton of junk that bloats the file size and slows down rendering. In general the best way to combat this is to first run SVG files through a linter such as svgo (see the Tools section below). However, when authoring SVGs by hand here are some best practices to help keep them 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:
This path:
<path d=" M5,5 L1,1z ">
can be cut down to this:
<path d="M5,5 L1,1z">
Similarly, this polygon:
<polygon points=" 0,0 4,4 4,0 "/>
can be cut down to this:
<polygon points="0,0 4,4 4,0"/>
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 tag names, if possible. You should also put the shortest attributes first, so they are easier to spot.
Vector editors (Inkscape, Adobe Illustrator, Sketch) usually add a bunch of metadata in SVG files while saving them. Metadata can mean many things, including:
sketch:foo
, illustrator:foo
, sopodi:foo
, …)xmlns:sketch
, xmlns:sopodi
, …)In addition to non-standard editor metadata, standard compliant metadata also exists. Typical examples of this are <title>
and <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, in most of the cases, the filename is quite descriptive 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 a source of many issues, and the SVG WG recommends not to include them. See SVG Authoring guidelines.
CDATA sections are used to avoid parsing some text as HTML. Most of time, CDATA isn't needed, for example, the content in <style>
tags doesn't need to be wrapped in a CDATA section as the content inside the tag is already correctly parsed as CSS.
There are two kinds of invisible shapes: The off-screen ones and the uncolored ones.
The offscreen shapes are hard to spot, even with an automated tool, and are usually context aware. Those kinds 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 two conditions: they must be devoid of any fill (or a transparent one) or stroke.
<svg
> elementThe root <svg>
element can also host many useless attributes. Here's an example taking into account the list below:
version
x="0"
and y="0"
enable-background
(unsupported by Gecko and now deprecated by the Filter Effects specification)id
(id on root element has no effect)xmlns:xlink
attribute when there are no xlink:href
attributes used throughout the filexml:space
when there is no text used in the fileclip-rule
attribute when the element is not a descendant of a <clipPath>
fill-rule
attribute when the element is a descendant of a <clipPath>
Here are some examples for excessive number precision:
As for descriptive IDs:
There's usually no need to set the default style value unless you're overriding a style. Here are some commonly seen examples:
style="display: none;"
on <defs>
elements (a <defs>
element is hidden by default)type="text/css"
on <style>
elementsstroke: none
or stroke-width: 0
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 while 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:
<g transform="translate(-62, -310)"><shape transform="translate(60, 308)"/></g>
can be cut down to:
<shape transform="translate(-2,-2)"/>
because: -62+60 = -310+308 = -2
These rules are optional, but they help speeding up the SVG.
<use>
tag when that <use>
tag is being referenced only once in the whole file.Tools can help to 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-awareness.
To this date, there doesn't seem to be a tool that handles all of the above. However, there are some utilities that cover parts of this document:
<title>
" options on): https://jakearchibald.github.io/svgomg/