SVG files scale cleanly, stay small, and accept per-use customization—but only while they remain inline. Once an SVG is referenced as an external file, its internal attributes fall out of reach. A site logo stored as web-logo.svg might be embedded like this:

<img src="https://css-tricks.com/images/logo.svg" />

That works when the artwork never changes. In practice, the same mark often ships in several color variants; Slack, for one, publishes two versions of its logo.

Even the colors in the main logo are slightly different.

Being able to override the fill color would let a single file generate every variant. The same need shows up with icon sets, where duplicating whole files to recolor them quickly gets out of hand.

<img src="https://css-tricks.com/icons/heart-blue.svg" />
<img src="https://css-tricks.com/icons/heart-red.svg" />

Fetching SVG files into the DOM

svg-loader addresses this by retrieving SVG files over XHR and inserting them as inline elements. Because the markup then lives in the document, its fill and stroke properties can be overridden exactly as with a hand-inlined SVG. A typical usage points data-src at the file URL and supplies a fill attribute, which overrides whatever fill the original file declares.

The setup requires little from the page: the library must be included somehow—through a <script> tag or inside the JavaScript bundle—and the server hosting the SVG files has to return suitable CORS headers so the XHRs succeed. Files are cached locally afterward, which makes later loads faster. On that very first load, performance is comparable to plain <img> tags.

Dynamically added elements and attribute changes are picked up automatically, so the approach fits any web framework, including React.

The pattern is not unique. svg-inject does something similar, but svg-loader is easier to adopt: dropping in the library is the only step, with no additional code on the page.

Why not just inline the markup?

Inlining is the most direct route, and svg-loader effectively performs the same operation—so why route through a separate file?

  • Inline SVGs bloat source code. A path can run from a handful of lines to hundreds. Vainlining is fine for a couple of tiny icons; once markup grows large or numerous, it turns into long stretches of non-business-logic text that hurt readability. The reasoning mirrors why external stylesheets are preferred over <style> blocks, or images over data URIs—and why React projects tend to import SVG as its own component rather than embed it in JSX.
  • External files are easier to work with. Copy-paste gets tedious when you are still deciding which icon belongs in a view; with external references, the filename is all you need to know. Paired with unpkg, svg-loader makes the 5,000+ icons in Material Design Icons usable immediately.

Weighing the usual objections

Is one HTTP request per SVG wasteful?

Increasingly less so. HTTP2 has reduced the cost of individual requests, and for XHRs and other non-blocking resources the compression gains of bundling are close to irrelevant in real-world use. A test page loads 50 icons this way; open it in incognito mode, since the files are cached by default.

Why not use the <use> tag (SVG symbols)?

Symbols split an SVG's definition from its usage, so it is declared once and referenced wherever needed:

<svg>
  <use xlink:href="#heart-icon" />
</svg>

The blocker is hosting: no browser honors symbol files served from a third-party domain, ruling out references like this.

<svg>
  <use xlink:href="https://icons.com/symbols.svg#heart-icon" />
</svg>

Safari goes further and refuses symbol files from the same domain as well.

Can't a build tool handle inlining?

Common bundlers like webpack and Grunt do have loaders for inlining SVG files kept locally, but fetching from a URL and inlining is not obviously supported. Even if a plugin exists, configuring a bundler is no small task, and many developers hold off until a project reaches a certain complexity. Much of the web never touches webpack or React at all, and plain scripts reach a far broader audience.

What is wrong with the <object> tag?

<object> is a native, cross-browser way to embed external SVG files:

<object data="https://unpkg.com/[email protected]/svg/access-point-network.svg" width="32" height="32"></object>

Its limitation is customization: the SVG's attributes cannot be changed unless the file is on the same domain—the tag ignores CORS headers entirely. Even for same-domain files, JavaScript is needed to manipulate fill:

<object data="https://unpkg.com/[email protected]/svg/access-point-network.svg" width="32" height="32" onload="this.contentDocument.querySelector('svg').fill = 'red'"></object>

Trade-offs, summed up

Referencing external SVG files this way makes icons and other assets far more convenient to consume. Via unpkg, any GitHub-hosted icon becomes usable without extra code; there is no need to build a bundler pipeline for SVG processing or a component per icon, only a CDN to host the files. The result is a large set of benefits for very little cost.