Web development

SVG sprite, components or icon font: how to add icons to a website without losing performance

The four ways to put icons on a website (inline SVG, sprite, React or Vue components, icon font) compared on performance, accessibility and maintenance.

There are four usual ways to add icons to a website: inline SVG (the XML inside the HTML), SVG sprite (one file with a <symbol> per icon referenced with <use>), components (a React, Vue or Astro component per icon) and icon font (each icon is a glyph in a typeface). For a modern site the recommendation is clear: inline SVG or components for sites with few icons per page, a sprite when the same icons repeat many times, and icon fonts only for compatibility with a system that already uses them.

This article compares the four techniques on performance, accessibility, styling and maintenance, with the minimal code for each.

1. Inline SVG

The icon goes straight into the HTML:

<button type="button">
  <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true" focusable="false">
    <path d="M5 12h14M12 5l7 7-7 7" />
  </svg>
  <span>Next</span>
</button>
  • Performance: zero extra requests; the icon paints with the HTML, no flicker. The cost is that the XML repeats on every occurrence and bloats the document; with 300-byte line icons, a hundred icons are 30 KB before compression, very little after gzip or Brotli because the text is repetitive.
  • Styling: total. currentColor inherits the text colour; you can change stroke-width, animate paths with CSS and respond to the parent’s :hover.
  • Accessibility: if the icon accompanies text, aria-hidden="true"; if it stands alone, give it an accessible name with a <title> inside the SVG plus role="img", or better with aria-label on the button or link that contains it.
  • Maintenance: copying XML by hand does not scale. The usual approach is a template that embeds it at build time (that is what this blog does: a component reads the SVG from the Lucide package and inserts it), which gives you the advantages of components without JavaScript on the client.

2. SVG sprite with symbol and use

All icons in one sprite.svg file, each inside a <symbol> that is later instantiated with <use>:

<svg xmlns="http://www.w3.org/2000/svg" style="display:none">
  <symbol id="icon-arrow-right" viewBox="0 0 24 24"><path d="M5 12h14M12 5l7 7-7 7" /></symbol>
  <symbol id="icon-search" viewBox="0 0 24 24"><circle cx="11" cy="11" r="8" /><path d="m21 21-4.3-4.3" /></symbol>
</svg>

And at each use:

<svg width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true">
  <use href="sprite.svg#icon-arrow-right" />
</svg>
  • Performance: the sprite downloads once and is cached; each use is about 100 bytes. It is the lightest option when the same icons appear dozens of times (lists, tables, menus). If the sprite is referenced as an external file, there is one extra request, but first paint is not blocked.
  • Styling: currentColor and inherited properties work; what you cannot do is style internal parts of the symbol separately from the document, because they live in the shadow tree of the <use>.
  • Limitation: <use> with an external file requires the same origin (it does not work from a CDN on another domain without injecting the sprite into the HTML).
  • Maintenance: building the sprite by hand is tedious; the AIMRAN Icons pack creator builds it from a selection of icons (with the usage snippet included), and in a project with a build step there are plugins (svg-sprite, vite-plugin-svg-icons) that do it automatically.

3. React, Vue or Astro components

One component per icon, returning the inline SVG with the props applied:

import { ArrowRight } from './icons';

<ArrowRight width={20} height={20} aria-hidden="true" />;
  • Performance: on the server or at build time, the result is inline SVG (technique 1). On the client, every component adds to the JavaScript bundle; with tree shaking only the icons you import travel, but a whole library imported carelessly can add hundreds of KB. Import individual icons, never the full index.
  • Styling and accessibility: those of inline SVG.
  • Maintenance: the best. Typing, autocomplete, one place to change the set. SVGR converts SVG files into React components at build time; AIMRAN Icons exports a .tsx file with the chosen icons directly (or one .vue per icon) for projects without that pipeline.

4. Icon font

Each icon is a glyph in an OTF or WOFF2 font and is shown with a class that inserts the character through ::before:

@font-face { font-family: 'my-icons'; src: url('my-icons.woff2') format('woff2'); font-display: block; }
.icon-search::before { font-family: 'my-icons'; content: '\e900'; }
  • Performance: one font request; until it arrives, with font-display: block the icon is invisible and with swap a placeholder character appears (the classic box) that later jumps. In other words, a badly loaded icon font causes CLS.
  • Styling: one colour only (they are glyphs), with no control over stroke width or internal parts. Line icons cannot be converted into a font without first outlining the strokes; that is why the AIMRAN Icons generator skips stroke icons when building a font and says so.
  • Accessibility: a screen reader may read the private-use character as garbage without aria-hidden; and if the user forces their own fonts (a real accessibility option), the icons disappear.
  • When it makes sense: when the existing system already uses one (Font Awesome in a WordPress theme, for instance) and replacing it would cost more than keeping it.

Comparison

Criterion Inline SVG Sprite Components Icon font
Extra requests 0 1 (cacheable) 0 on the server; JS on the client 1 (font)
Weight with many repeated icons High uncompressed, low with gzip Minimal As inline Low
Colour with CSS Yes, any part Yes, inherited Yes One colour
Line icons Yes Yes Yes No (must be outlined)
CLS risk No No No Yes if the font is slow
Accessibility Good with aria-hidden or a name Good Good Fragile
Maintenance Needs a build step to avoid copying XML Sprite generator The best Regenerate the font on every change

What to choose

  • Static or server-rendered site (Astro, Next, Nuxt, WordPress with a custom theme): components that turn into inline SVG. That is what this blog does.
  • Interface with many repeated icons (dashboards, tables, long lists): SVG sprite, inline in the HTML if small or as a cached file if large.
  • Client-side React or Vue application: components imported individually from a set (Lucide, Tabler, Heroicons…) or from a pack generated for the project.
  • Legacy project with an icon font: keep it until a planned migration; if you add new icons, add them as SVG already.

Whichever technique, keep one set with the same stroke width and grid, and check the licence of every icon before packaging it: AIMRAN Icons shows it in each icon’s detail and includes it in a LICENSES.txt file inside every pack.

Conclusion

Inline SVG (generated by components at build time) for most sites, a sprite when icons repeat at scale, components in client-side applications and icon fonts only by inheritance. All four paths start from the same SVG files; what matters is not copying them by hand and not mixing sets.

Sources and references

  1. MDN: <symbol> element developer.mozilla.org
  2. MDN: <use> element developer.mozilla.org
  3. MDN: aria-hidden developer.mozilla.org
  4. MDN: font-display developer.mozilla.org
  5. web.dev: Best practices for fonts web.dev
  6. SVGR: transform SVGs into React components react-svgr.com

Related tools

Herramientas gratuitas de AIMRAN Tools que funcionan en tu navegador, sin registro.

Ver todas las herramientas
  • AIMRAN Icons: icon search

    Over 360,000 freely licensed icons from 222 sets, searchable in Spanish, English, French and German; download as SVG, PNG, WebP, AVIF, ICO or PDF.