Internal vs External SVG Symbols Using the Use Element
The SVG <use> element allows developers to
instantiate reusable vector graphics defined inside
<symbol> or <g> elements. The
primary difference between referencing an internal versus an external
SVG symbol lies in where the source definition is stored: internal
references point to an ID within the same HTML/SVG document, whereas
external references fetch the symbol from a standalone SVG file over the
network. This distinction impacts browser caching, cross-origin
security, initial page weight, and maintenance workflows.
Internal SVG Symbol Referencing
An internal SVG reference retrieves a symbol defined directly within
the same HTML or SVG document. The symbol is typically housed inside a
hidden SVG container within a <defs> block on the
page.
Syntax
<!-- Hidden SVG sprite in the HTML document -->
<svg style="display: none;">
<symbol id="icon-user" viewBox="0 0 24 24">
<path d="..." />
</symbol>
</svg>
<!-- Internal reference -->
<svg class="icon">
<use href="#icon-user"></use>
</svg>Key Characteristics
- Zero Additional HTTP Requests: Because the SVG symbols are inlined into the HTML, no extra network requests are required to fetch icons.
- No CORS Restrictions: Internal references work
under all environments, including the
file://protocol and across subdomains, as no cross-origin asset loading is involved. - No Browser Caching for the Sprite: Inlining the SVG increases the HTML payload size. Because the markup is part of the HTML file, the SVG code cannot be cached independently of the page.
- Immediate Rendering: Icons render synchronously with the HTML, eliminating the risk of a Flash of Unstyled Content (FOUC).
External SVG Symbol Referencing
An external SVG reference points to a symbol located inside an external SVG file (often called an SVG sprite sheet) hosted on a server.
Syntax
<!-- External reference to a separate file -->
<svg class="icon">
<use href="/assets/sprite.svg#icon-user"></use>
</svg>Key Characteristics
- Efficient Browser Caching: The external sprite file is treated as a static asset. Once fetched, the browser caches it via HTTP cache headers and reuses it across multiple page loads.
- Lean HTML Markup: The main HTML document remains lightweight because it does not carry the bulk of the SVG vector paths.
- CORS Restrictions: Browsers enforce the Same-Origin
Policy on external SVG files referenced via
<use>. The sprite file must be served from the same domain or include appropriate Cross-Origin Resource Sharing (Access-Control-Allow-Origin) headers. - Asynchronous Loading: On the initial visit, icons may experience a brief render delay while the external file downloads.
Direct Comparison
| Feature | Internal Reference (#id) |
External Reference
(file.svg#id) |
|---|---|---|
| Source Location | Same HTML document | External .svg file |
| HTTP Requests | 0 additional requests | 1 request for the sprite file |
| Caching Mechanism | Cached only with HTML | Cached independently as static asset |
| CORS Sensitive | No | Yes (requires same-origin or CORS headers) |
| HTML Document Size | Increases with icon count | Stays minimal |
| Offline/Local Testing | Works natively via
file:// |
Requires a local web server |
When to Use Which
- Use internal references when building single-page applications (SPAs) with small icon sets, dynamic server-rendered pages where total HTML size is negligible, or environments where setting up proper CORS headers for static assets is not possible.
- Use external references for multi-page websites and design systems with large icon libraries, where leveraging the browser cache across routes delivers the best overall performance.