SVG Sprite Optimizer

Optimize SVGs and package them into a symbol sprite or CSS

This tool works best on a larger screen (desktop).

    About SVG Sprite Optimizer

    This tool takes a batch of SVG icons and does two jobs. First it slims each file down: it drops the XML declaration, comments, metadata and Inkscape, Sodipodi and Sketch editor attributes, collapses whitespace and trims decimal places. Then it combines the results either into one symbol-based sprite or into a set of CSS classes carrying data URIs.

    It is not SVGO. It does not recompute path data; it removes text according to fixed rules. That makes the output predictable, but it also means you should know which rule can break what.

    How symbol and use make a sprite

    A symbol element is a template that is never rendered on its own. Give each symbol an id and a viewBox, collect them inside one SVG, and anywhere on the page a use element can point its href at an id such as #icon-home to draw a copy. You avoid one HTTP request per icon, and an icon used twenty times still ships its path data once.

    The tool derives each id from the file name: lower-cased, anything other than letters and digits turned into hyphens, prefixed with icon-, and prefixed again if it would start with a digit. If the source has no viewBox, one is built from width and height, or assumed to be 0 0 24 24. When use points at a separate file (sprite.svg#id), browsers only allow same-origin references, so a sprite hosted on another domain such as a CDN will not resolve. In that case, inline the sprite in the HTML body.

    Colliding ids across files

    SVGs exported from design tools routinely name their gradients and clip paths paint0_linear, clip0 or just a. Merge several such files into one sprite and the same id appears more than once. The browser resolves each reference to the first match in the document, so the second icon quietly gets painted with the first icon's gradient.

    The tool prefixes every internal id inside a symbol with the symbol id and a double underscore — icon-home__clip0, for example — and rewrites the references to match: url(#…), href="#…", and animation begin and end values. References to ids that are not defined inside that file are left alone.

    What is safe to strip — and what isn't

    Comments, the XML declaration, metadata blocks and editor-only attributes have no effect on rendering, so they are safe to remove. viewBox is a different matter: it is what keeps the proportions right when the icon is resized. SVGO leaves both viewBox removal and title removal out of its preset-default for this reason, and this tool keeps viewBox too.

    title and desc are kept. Unlike metadata blocks, they are the accessible name a screen reader announces for the icon, so they stay inside each symbol in the sprite as well. Some browser and screen reader combinations don't read the title of an icon pulled in with use, though, so for icons that carry meaning it is still safer to add a name on the HTML side with aria-label or visually hidden text.

    Whitespace collapsing removes all whitespace between tags. Shape-only icons are unaffected, but an SVG that sets words with text and tspan elements can lose the spaces between them. Check any SVG containing text by eye after optimising.

    Choose decimal precision by viewBox size

    The precision slider (0–4, default 2) rounds every decimal number in the document. The error is measured in user units, so how visible it is depends on the viewBox. On a 24×24 icon, two decimal places means at most 0.005 units — 0.005 px when drawn at 24 px, which nobody can see. At 0 the error can reach 0.5 units, and on a 24 px icon lines may wobble by half a pixel.

    The flip side: SVGs drawn in a tiny coordinate system, say viewBox="0 0 1 1", visibly lose their shape even at two decimal places. If curves look faceted in the preview, raise the precision.

    Symbol sprite or CSS sprite?

    A symbol sprite renders as inline SVG, so icons drawn with fill="currentColor" follow the surrounding text colour, and you can change colour and stroke width from CSS. The CSS sprite embeds each icon as a data URI in background-image. An SVG loaded that way is a separate document and ignores the page's CSS and currentColor, which suits fixed-colour logos and illustrations.

    The data URIs are URL-encoded rather than base64. Base64 inflates the markup by about a third and compresses poorly with gzip, while URL-encoded SVG keeps its text structure and compresses better in transfer.

    Worked examples

    Merging outline icons into a sprite

    Input
    Three icons of the form <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"> … </svg>
    Output
    <symbol id="icon-home" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"> … </symbol>

    Along with viewBox, presentation attributes on the root svg such as fill, stroke and stroke-width move to the symbol. The paths inside inherit them, so outline icons like Feather or Lucide keep their line style instead of turning into solid black shapes, and because stroke is currentColor they follow the surrounding text colour.

    Trimming path coordinates at precision 2

    Input
    d="M12.34567 7.89123 L3.10000 4.50000"
    Output
    d="M12.35 7.89 L3.1 4.5"

    Each number is rounded to two decimals and trailing zeros are dropped. Integers such as 24 are left untouched.

    Two files with the same name

    Input
    home.svg, Home.svg
    Output
    icon-home, icon-home-2

    Duplicate ids get -2, -3 and so on. Tidy file names beforehand and the ids will be easier to find in your HTML.

    How to use it

    1. Drop several SVG files.
    2. Set the decimal precision and check the saving for each file.
    3. Copy or download the symbol sprite or the CSS sprite.

    Frequently asked questions

    My icons turned solid black after spriting.

    Attributes such as fill="none" and stroke on the root svg are carried into the symbol, so icons normally keep their look. If they still turn black, the original most likely set its line style in a style block inside the svg or in an external CSS class. In that case set fill and stroke with CSS on the svg element where you use the icon.

    I reference an external sprite.svg with use and nothing appears.

    External references only work same-origin. If the sprite lives on another domain or you opened the page via file://, inline the sprite in the HTML. Also check the path and the spelling of the #id.

    I can't change the colour of CSS sprite icons.

    An SVG in background-image does not receive page CSS or currentColor. Use the symbol sprite when colours need to change.

    Can this replace SVGO?

    It handles metadata cleanup and sprite building, but it does not rewrite path commands or merge shapes. If every byte matters, run SVGO first and merge the results here.

    Sources

    Last reviewed:

    Privacy and how data is handled