SVG as a data URI vs inline SVG

Published 2026-11-10 · for icons, favicons, and CSS backgrounds

An SVG can reach the page through three doors, and once it arrives, its behavior differs depending on which one you used. The same file works as an external .svg asset, as markup pasted straight into HTML, or as a data URI string — and developers get burned when they assume the three are interchangeable. Styling hooks, caching, script execution, and even duplicate-id bugs all hinge on the delivery route.

The route also decides what the SVG is to the browser. External files and data URIs placed in <img> or CSS are treated as images. Inline markup becomes part of the document itself. Most of the trade-offs below fall out of that single distinction.

If you just need to encode or decode a file right now, the SVG to Base64 tool and the Base64 to SVG decoder are one paste away. The rest of this page is about choosing the right form in the first place.

The three ways to ship an SVG

External .svg file

Referenced as <img src="icon.svg"> or background-image: url(/icons/icon.svg). The browser makes a separate HTTP request and keeps a separate cache entry, so the same icon can be reused across pages without inflating any single document. Like every image reference, it costs one round trip before first render.

Inline markup

Paste the <svg> element directly into the HTML. There is no request at all, and the markup joins the DOM like any other element. That gives the page full styling control — and full responsibility: because inline SVG is document content, a <script> inside it executes like any other script on the page, which is why SVG from an untrusted source should never be pasted in raw.

Data URI

The whole file, URL-encoded or Base64-encoded into one string with the image/svg+xml MIME type. Both encodings are valid, and browsers accept either:

<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0i..." alt="">
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http..." alt="">

A data URI works anywhere a URL works — <img src>, CSS url(), favicon <link> tags, and <object> embeds.

What stops working inside a data URI (or an img)

When an SVG is loaded as an image — through <img>, a CSS background, or a data URI used in either place — browsers treat it as static image data, not as a document. The consequences are consistent across modern browsers:

  • Scripts never run. A <script> element inside the SVG is ignored entirely.
  • External references are not fetched. Stylesheets, web fonts, and other images referenced from inside the SVG do not load. An icon that depends on a hosted font renders with a fallback instead.
  • The page's CSS does not reach inside. Class names from your stylesheet do nothing, variables defined on :root are invisible, and currentColor has no page color to inherit — it falls back to the image context's own default.

What does survive: internal <style> blocks, presentation attributes, and the declarative core of SVG — paths, shapes, gradients, masks. If your artwork relies only on those, the data URI form renders exactly like the file it came from.

One more consequence is about caching rather than rendering: a data URI has no cache entry of its own. It travels inside the HTML or CSS file and is re-read every time that file is loaded. For a small icon that is irrelevant; for something large and reused across pages it adds up — the same dynamic covered in when not to inline Base64 images.

What inline SVG gives you

Inline markup is a first-class DOM citizen, and everything the image context forbids becomes available:

  • Page styling. A rule like .btn:hover .icon path { fill: #fff; } works. currentColor picks up the surrounding text color, so one icon can follow the theme without variants.
  • CSS variables. Define --icon-stroke once on the page and reference it from the SVG's attributes.
  • Reuse with <use>. Define an icon once with an id and instantiate it elsewhere with <use href="#icon-name">, instead of duplicating markup.
  • Accessibility hooks. role="img", <title>, <desc>, and aria-labelledby sit in the normal document flow where assistive technology expects them.

The costs are the mirror image. Everything lives in one document, so ids are global: paste the same icon twice with a gradient defined as id="g1" in both copies, and internal url(#g1) references can resolve to the wrong copy, leaving one icon filled with the other's gradient. Slightly wrong colors from duplicated ids are a classic inline-SVG bug. Inline also means trusting the markup you pasted — untrusted SVG can carry scripts that will run, so sanitize anything you did not draw yourself.

What a data URI gives you

The data URI is the opposite bargain: isolation in exchange for opacity.

  • Encapsulation. Each data URI is its own little document. No id collisions, no style leakage in either direction, no surprises from a page-level CSS reset.
  • Universal placement. It works in <img>, CSS url(), favicon links, and any tooling that only accepts URL strings — places where pasting raw <svg> markup is impossible.
  • Self-contained deliverables. A page that embeds its icons as data URIs works as a single file with no side requests — handy for demos, email templates, and offline docs.
  • A hard trust boundary. In image context the browser will not execute the SVG's scripts at all. An SVG data URI cannot do what inline markup can; it still deserves sanitizing if it came from an untrusted source, but it cannot run as document content.

Against that: the string is opaque. Debugging a rendering bug means decoding the Base64 back to readable markup — the Base64 to SVG decoder takes a paste — and the payload grows by about a third, with no separate cache entry, as covered above.

Percent-encoding or Base64?

SVG is text, so Base64 is not the only option. Both of these are valid image/svg+xml data URIs for the same tiny circle:

data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxNiAxNiI+PGNpcmNsZSBjeD0iOCIgY3k9IjgiIHI9IjYiLz48L3N2Zz4=

data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2016%2016%22%3E%3Ccircle%20cx%3D%228%22%20cy%3D%228%22%20r%3D%226%22%2F%3E%3C%2Fsvg%3E

How the two trade off:

  • Base64 is quoting-safe. Its alphabet contains no quotes, angle brackets, or ampersands, so the string drops into a double-quoted HTML attribute or a CSS url("…") without a second escaping pass. It always adds roughly a third to the size and turns the markup unreadable.
  • Percent-encoding stays readable. You can still see the SVG structure inside the string, which helps debugging and code review. Its length depends on how much of the markup needs escaping — with light cleanup such as removing line breaks and switching to single-quoted attributes, it often lands near the source size and sometimes beats the Base64 form. In exchange it demands care with context-sensitive characters like %, #, and quotes; a missed escape is a classic cause of an SVG data URI that renders blank.

Neither form is more correct, and every modern browser handles both. Pick percent-encoded when you want the string inspectable and diffable; pick Base64 when the string passes through strict quoting contexts, templates, or tooling that expects it. Encode with SVG to Base64; decode with Base64 to SVG when a string needs inspecting.

Choosing by use case

The shortest correct answer is usually one of four moves:

SVG delivery route decision matrix
SituationBetter fitWhy
Icon must react to hover, focus, or the site themeInline <svg>Page CSS, currentColor, and variables reach inside
Icon lives in CSS background-imageData URIurl() takes a URL, not markup; a data URI needs no extra request
Same artwork pasted into the page more than onceData URI, or <use>Separate documents avoid duplicate-id clashes
Large illustration reused across many pagesExternal .svg fileOwn cache entry; keeps every HTML document lean
FaviconData URIOne string in a <link> tag, no extra request

The favicon case

Favicons deserve their own note because most trade-offs barely apply. The artwork is tiny, it appears on every page, and there is no styling to preserve. A single line like <link rel="icon" href="data:image/svg+xml;base64,..."/> removes a network request and keeps the icon next to the markup that references it. SVG favicons work in most modern browsers, though Safari has historically been the holdout — keep a PNG fallback alongside. The Base64 favicon page has a ready-to-paste snippet.

Summary

  • Use inline SVG when the artwork must respond to page styling, you need <use> reuse within one document, or DOM accessibility labels matter. Watch for duplicate ids, and only inline markup you trust.
  • Use a data URI when the SVG is a CSS background, a favicon, a repeated paste, or part of a self-contained file. Expect a larger payload and no separate cache entry.
  • Use an external file when the SVG is large or shared across pages. Both inline copies and data URIs stop making sense at some size — the thresholds are laid out in when not to inline Base64.

One cross-cutting rule ties the routes to security: inline SVG runs scripts, SVG-as-image never does, and SVG loaded as a document through <object> or <iframe> runs them again. If your site ships a Content-Security-Policy, data URIs and CSP explains which directives need data: — and which must never get it.