SVG Symbol Sprite Generator
Operator: Abdulla Al Fahad. How tools are built and checked
Icons that each define a gradient with id="g" will fight as soon as they sit in the same document: whichever comes first wins, and the second icon quietly paints with the wrong colours. A sprite built carelessly is full of such clashes. Here every inner id is renamed to icon-red-g and icon-blue-g, every url(#g) and href that pointed at the old id is updated, and ids that nothing refers to are dropped. The second common failure is a stroke icon that goes blank. Feather-style icons keep fill="none" and stroke="currentColor" on the root <svg>, and a <symbol> does not inherit from the element it replaced. Those attributes are moved onto the symbol, so the icon keeps drawing.
Key features
- Symbol sprite from up to 200 SVG files or pasted markup, with editable symbol ids and a prefix
- Inner ids renamed per icon, with url(#id), href and style references rewritten, and unused ids removed
- currentColor for single-colour icons, or for every fill and stroke; gradients and patterns are never altered
- Presentation attributes on the root svg (fill, stroke, stroke-width, line caps and joins, fill rule, opacity) carried onto the symbol
- Path and polygon numbers rounded to 0 to 4 decimals and re-joined with the fewest separators
- Only plain drawing elements survive: scripts, event handlers, foreignObject, embed, object, iframe, audio, video, animation, remote or data links, remote CSS and editor metadata are removed, and every removal is reported per icon
- Preview grid at 16 to 96 px with your colour, HTML, CSS and React snippets, a demo page and the sprite file
What happens to each icon on its way into the sprite
Input is SVG text up to 500 KB per icon, 200 icons and 5 MB in all. Files are read as text and a file that looks binary is refused. Each file must contain an <svg> element; XML declarations, comments, doctypes and processing instructions are dropped. A viewBox is required to size a symbol: when it is missing the page builds one from numeric width and height, and when those are missing too it assumes 0 0 24 24 and tells you to check the preview. Symbol ids come from the file names, lower-cased and reduced to letters, digits and hyphens, with the prefix in front (icon- by default), and duplicates get -2, -3. An id must start with a letter or underscore and contain only letters, digits, hyphens, underscores and dots; a name that fails is flagged instead of being written. A prefix-less file name that starts with a digit gets i- in front.
Rounding keeps decimals with Math.round on a power of ten, so 2 decimals moves any coordinate by at most 0.005 units, and 0 decimals by at most half a unit, which will visibly distort a 24 unit icon. The tokenizer reads arc flags the way the SVG grammar requires, as single characters that can be run together, as in a1 1 0 011 1, so rounding never merges a flag into the next number. Data that does not tokenise is left exactly as it was.
How it works
A small tolerant XML reader builds a tree for each icon, so cleanup works on elements and attributes, not on regular expressions over raw text. It keeps a fixed list of drawing elements (shapes, text, gradients, patterns, clip paths, masks, markers and filters) and drops anything else with its children, including elements in foreign namespaces (Inkscape, Sodipodi, Sketch and similar). <a> links are unwrapped, and an href on <use>, <image> or any other element survives only as #id inside the icon, so remote and data: links never reach the sprite. A url() in a presentation attribute is kept only as url(#id). Colour detection looks at fill and stroke attributes and at the same properties inside style attributes; replacement writes currentColor in both places.
Previews are drawn as <img> elements with each icon as a standalone SVG in a data address, with your preview colour set on the root so currentColor resolves to it. An image element cannot run scripts, and the cleanup does not rely on that: the same cleaned markup goes into the sprite file and the demo page, and the demo page carries a Content-Security-Policy that blocks every fetch except inline styles and data: images.
Typical uses
- A design system with 140 icons in one folder: drop them all, keep the icon- prefix, choose currentColor for single-colour icons, and a 30-odd KB sprite is served once and cached, instead of 140 requests.
- Two imported brand icons whose gradients are both id="a": after scoping they become icon-one-a and icon-two-a, and each logo keeps its own colours when the sprite is inline.
- A React app with a union type of icon names: the React snippet lists every name from your file set as a union, so a misspelt name fails at compile time.
Building one
- Drop your SVG files, or paste one under Paste SVG markup, and check the list for ids, colours and notes.
- Pick the colour mode and rounding, and rename any id the list flags.
- Check the preview grid at your colour and size, switching the background to dark if the icons are meant for it.
- Download the sprite, copy the snippet for HTML, CSS or React, or download the demo page to see all icons at once.
Example
A 24 x 24 stroke icon with a root stroke colour (296 bytes)
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="#333333" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><title>Home</title><path d="M 3.000 9.0000 l 9 -7.00 l 9 7 v 11 a 2 2 0 0 1 -2 2 H 5 a 2 2 0 0 1 -2 -2 z"/><polyline points="9 22 9 12 15 12 15 22"/></svg>Symbol at 2 decimals, id icon-home (250 bytes)
<symbol id="icon-home" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 9l9-7l9 7v11a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/><polyline points="9 22 9 12 15 12 15 22"/></symbol>
path data 76 -> 46 characters; the whole icon 296 -> 250 bytes; the title is removed (note: Removed: 1 metadata)Tips for best results
- Inline the sprite only when you need gradients or CSS to reach inside the icons; a sprite referenced by file URL cannot be styled except through currentColor and inherited properties.
- Use 3 or 4 decimals for icons drawn on a 1000-unit grid and displayed small, since 2 decimals is more than enough there, and 0 only for pixel-grid icons whose coordinates are already whole numbers.
- If an icon keeps its original colours in single-colour mode, open its row: the note names why, such as two colours or a gradient.
Things to check before you rely on it
The default removes <title> and <desc>, and the snippets mark each icon aria-hidden, so an icon that is the only content of a button or link has no accessible name: the "Home" in the example is gone. Give the button an aria-label or visible text, or add role="img" and aria-label to the <svg> that uses the symbol. Ticking Keep <title> and <desc> retains the text inside the symbol, but screen-reader support for a <title> inside a <symbol> reached through <use> is inconsistent, so the label on the using element is the dependable fix.
A <style> block inside an icon is rebuilt rather than kept as written: @media, @keyframes, @font-face and @import rules are dropped, as is any declaration with a url() that is not #fragment. What remains keeps its class names, which are not renamed, so two icons that define the same class with different rules will affect each other in an inline sprite. Rewrite them as attributes first. currentColor also cannot recolour an icon that gets its colour from a gradient, because that colour lives in the gradient's stops.
Common errors and how to fix them
- No <svg> element was found
- The file is not an SVG, or its markup is damaged before the first <svg tag. Open it in a text editor and check that it starts with <svg or an XML declaration.
Best practices
- Put the ids in your component code, not the file names: rename a symbol in the list once, and the React union and snippets follow.
- Keep source icons on one grid (24 x 24 or 16 x 16). A symbol scales to whatever box the <svg> that uses it has, so mixed grids give mixed visual weights.
What cleanup removes from each icon
Every removal is counted and reported under the icon's row as a Removed: line, so nothing disappears without a note. Editor leftovers are the largest source of weight, and scripts and remote references are the ones that matter for safety.
- XML declarations, comments, doctypes and unused xmlns and version attributes.
- Elements and attributes in editor namespaces such as Inkscape and Sodipodi, plus <metadata>, <title> and <desc> unless Keep is ticked.
- <script>, <foreignObject>, <embed>, <object>, <iframe>, <audio>, <video>, animation elements, any other element outside the drawing list, and every on... event attribute.
- href and xlink:href values that are not #id, including remote and data: links; an <image> or <use> left with no link is deleted.
- Inside <style> and style attributes: @import and every other at-rule, and any url() that is not #fragment. In presentation attributes such as fill, url() survives only as url(#id).
- Fixed width and height on the root svg, so the symbol scales to whatever box uses it.
Frequently asked questions
Is this the same as a PNG sprite sheet?
No. A PNG sprite sheet packs raster frames into one image and picks them with background-position. This makes one SVG of <symbol> elements that stays sharp at any size, and each icon is drawn with <use href="#id">.
Can I reference the sprite from another domain?
No. A <use> that points at an external file works only when the file is on the same origin as the page, so a sprite on a CDN must be fetched and pasted inline, or served from your own host. The inline snippet with the hiding wrapper is built for that case.
Why is the hiding wrapper zero-sized instead of display:none?
A gradient or clip path inside an element with display:none does not render in several browsers, so icons using them would appear blank. A 0 x 0 absolutely positioned svg stays out of sight but keeps those references working.