SVG to Data URI: Inline SVG Without a Request

A data URI carries the whole image inside the URL. That removes one HTTP request and one cache entry; the cost is that the bytes now live in your stylesheet or HTML. This page builds both encodings, prints the real sizes, and hands you the snippet each context expects.

Base64 or percentCSS / HTML / JS / JSONDecode any URINo upload

1. SVG in

file or paste
Drop an .svg here
or
No file loaded
Shape
Minify

2. Two encodings

—
Base64 · 0 B
Percent-encoded · 0 B

3. The snippet each context needs

quotes and escapes applied
CSS
HTML
JavaScript

4. Decode a data URI

base64 or percent, pasted from anywhere

A data URI is the file itself, spelled into the URL. data:image/svg+xml,<svg…> is a complete SVG document that a browser will paint wherever a URL is accepted, with no second request. The encoding is the boring part and the escaping is the part that breaks builds, so this page does both and shows the byte cost of each.

A data URI is the file, not a link

Three pieces: the mediatype, an optional ;base64 marker, and the payload. Without the marker the payload is the document itself, percent-encoded where necessary; with it, the payload is Base64 of the same bytes. Both are legal, both render identically, and the browser does not care which you used.

FormLooks likeReadableCost
Percent-encodeddata:image/svg+xml,%3Csvg%20…Yes, after decoding the escapesOnly reserved characters expand
Base64data:image/svg+xml;base64,PHN2Zy…NoAlways +33%

The size counter under the two outputs settles the argument for your specific file rather than in general: small markup with few reserved characters is usually shorter percent-encoded, while a file with dense path data and non-ASCII titles is shorter in Base64.

Base64 or percent-encoded: measuring the difference

Percent-encoding escapes only the characters that would otherwise confuse a parser — here < > # % " ' ( ) and whitespace, plus everything outside ASCII. A star icon that is 300 bytes of markup gains perhaps 60 escaped characters, which is far less than the 100 characters Base64 would add. The same icon with a d attribute of four thousand characters, however, contains almost nothing worth escaping, so percent-encoding barely grows it while Base64 adds a third.

Non-ASCII flips the comparison hard: one CJK character is three UTF-8 bytes, and percent-encoding writes all three as %E4%B8%AD. A title block in Chinese can triple the URI, at which point Base64 is the smaller of the two.

The same SVG as a Base64 URI and a percent-encoded URI, with the snippet used in CSS, HTML and JavaScript
Measured on the sample icon: the same bytes, three contexts, two encodings · swipe to see it all

The escaping rules per context

This is where most broken inline SVG comes from, and none of it is about encoding:

ContextWhat breaksWhat to do
CSS url()A raw # starts a fragment, so fill="#5850EC" truncates the value and nothing rendersEscape # as %23, and quote the URI
CSS valueParentheses and quotes end the token earlyEscape them, or use Base64
HTML attributeDouble quotes inside a double-quoted attribute close itSingle-quote the attribute or encode the quotes
JS stringQuotes, backslashes and line breaksWrap in the opposite quote, escape the rest
JSON valueAnything that is not a valid string bodyJSON-escape the string, never paste raw markup

The four boxes in the tool above apply exactly these rules to the URI you built, which is why they are not interchangeable: the CSS box has %23 where the HTML box has a plain #, because only CSS treats it as a fragment marker.

Where an inline SVG actually helps

It stops helping when the same illustration is used on twenty pages: the bytes ship twenty times, and no CDN or cache can deduplicate them. For anything reused, a normal .svg file behind a cache header is smaller in total even though it costs a request.

Decoding a data URI you found

Paste any data URI into the decoder and it will tell you whether the payload is Base64 or plain, how many bytes came out, and whether the result parses as XML. This is the quickest way to answer "what is this value in our stylesheet" without opening a terminal. If you need the raw Base64 string rather than the URI wrapper, the SVG to Base64 page is the direct route, and if you want the same one-file property for a QR code, the QR code generator writes a single path SVG.

Check the # first. Nine out of ten “my inline SVG does not show up” cases are an unescaped hash inside a CSS url().

Frequently asked questions

Base64 or percent-encoded?

Whichever is smaller for your file — the two size counters above answer it. Percent for small markup with few reserved characters, Base64 for dense path data or non-ASCII text.

Why does my inline SVG vanish in CSS?

An unescaped # in fill or stroke is read as a fragment identifier. Escape it as %23 and quote the url().

Is an inline data URI cached?

No. It travels with the stylesheet or document, so it is fetched every time that file is, and it cannot be served from a CDN on its own.

Does gzip help a Base64 URI?

Barely: Base64 is close to random bytes. Percent-encoded markup compresses much better, which is another reason it often wins after transfer.

Is my SVG uploaded?

No. Encoding and decoding run in the tab with TextEncoder and encodeURIComponent. No upload endpoint, no external script.

Related tools