SVG sprite generator

Combine icons into an SVG sprite

Drop a set of icons and get one sprite file of <symbol> elements. Reference any icon with a single <use> tag and style it with CSS.

Runs in this tab. Nothing is uploaded.

Symbols

Click an icon for its snippet
  • Add two or more SVG icons, or try the samples.

Usage


					

sprite.svg


					

How the sprite is built

  1. IDs from file names. Arrow Left.svg becomes icon-arrow-left. Two files that end up with the same name get -2, -3 and a warning. Change the prefix above, or leave it empty.
  2. Safe internal IDs. Gradients, masks and clip paths inside each icon are renamed (icon-flame-a), so two icons that both use id="a" no longer steal each other’s gradient.
  3. Clean symbols. Each icon keeps its viewBox (one is made from width and height if missing), plus fill and stroke settings from its root. Editor data, comments, scripts and external references are removed.

Inline the sprite once near the top of the page’s <body>, then show an icon with <svg><use href="#icon-name"></use></svg>. You can also link to the file, as in /sprite.svg#icon-name, as long as it is served from the same origin. With currentColor the icons take the CSS color of their container. The sprite is hidden with a zero-size style rather than display:none, which would break gradients in some browsers.

Questions

Should I use a sprite or separate SVG files?

A sprite means one request and one cache entry for a whole icon set, and icons can be coloured with CSS. Separate files are simpler when a page only needs one or two icons.

Why doesn’t my external sprite show up?

Browsers only allow <use> to reference files from the same origin, and the server must send the sprite as image/svg+xml. Inline the sprite in the page if it lives on a CDN.