Pikkola data-uri, tool No. 98

A file that fits in an attribute.

Turn text, a file or an SVG into one data URI, in base64 or in the shorter percent-encoded form, and see exactly what the extra characters cost. Paste a URI back and the bytes come out, with the media type read from the URI itself.

2
encodings
0
uploads
3
snippet forms

The wrapping desk

Wrap it, or open it

Two halves: the left turns bytes into a URL, the right turns a URL back into bytes. Both sides use the same parser, so a URI this page made is one this page can read.

Encode

Drop a file here

Read in this tab. Nothing is uploaded.


    

Decode

Decoded text

    

What a data URI really costs

It removes a request and adds characters to CSS or HTML. Which one matters depends on the size of the file and on what else the page is loading.

01

One request that is not there

A stylesheet with an inlined icon makes no request for it: no DNS, no connection, no cache entry to go stale. On a page with five hundred small icons that is the difference between five hundred requests and one, which is why icon sets are the place people reach for it.

02

Base64 costs a third more

Three bytes become four characters, so a base64 URI is about 37 per cent longer than the file, before the media type. Gzip and Brotli compress that overhead well because the alphabet is small, but the browser still parses every character into bytes on the way in.

03

Percent-encoding can be smaller

An SVG is text, and a URL leaves letters, digits and most punctuation alone. Only the characters that would end the data, the quote and the angle brackets have to become %XX, so an SVG often grows a little or shrinks after its comments are removed. It cannot carry binary: a byte that is not valid UTF-8 does not survive the trip.

What is converted, and what is refused

The encoder is exact and the decoder is suspicious by design.

Part What this page does What it will not do
Encoding Base64 for any bytes, percent for text and SVG only Percent-encode a binary file and call the result lossless
Minify Removes the XML declaration, the doctype, comments, metadata blocks and the whitespace between elements Rewrite paths, drop attributes, or claim a smaller file than it produced
Media type Taken from the file when the browser knows it, and printed in the URI Guess a type from the bytes, or sniff it the way a browser does
Decoding Reads the media type, the charset and the base64 flag from the URI itself Trust a base64 body whose length is wrong, or repair a damaged one silently
Preview An <img> for an image type, drawn from the URI, with no request Run a script carried in a URI, or open one in a frame
Storage Nothing: a reload returns to the first sample Keep a file, a URI, a cookie, or upload anything

A data URI is not a safe place to carry a script: browsers refuse data: top-level navigation and most refuse data: in a script tag, and a page with a strict content policy has to allow data: explicitly. For an image or a font in a stylesheet it is a transport detail, not a security boundary.

Field guide

One request removed, a third more characters added

A data URI trades a network request for inline characters. Whether that is a good trade depends on the file, on how often it is used, and on which of the two encodings can carry it.

The shape of the URL

data: + a media type + , + the data, with ;base64 before the comma when the data is base64. The media type may carry a charset, as in data:text/plain;charset=utf-8,…. Everything after the first comma is the data, which is why a comma inside an SVG has to be percent-encoded and why a missing comma is the failure this page names first.

A fragment after the body is not part of the data: ...#icon addresses an element inside an SVG document, and browsers treat everything from # as belonging to the page. That is one of the two characters that must always be escaped in a hand-written URI, the other being the percent sign itself.

Base64, and the third it costs

Base64 maps three bytes onto four characters from a 64 character alphabet. The result is 4/3 of the input, plus padding, plus the media type, which on a small icon is most of the URL. It carries any bytes, including binary, and it survives being copied through almost anything, which is why it is the default here and the only option for images and fonts.

The cost is not only size. A base64 image has to be decoded before it can be painted, and it cannot be cached separately from the document that carries it. For a 1 KB icon in a stylesheet that is invisible; for a 200 KB hero photograph it delays the first paint of the whole page.

Percent-encoding, and why an SVG likes it

A URL may carry letters, digits and a set of punctuation unescaped, so an SVG full of element names loses very little. Only <, >, #, %, the quote and the space become %XX, and after comments and indentation are removed the URI is often shorter than the source file. The catch is that it only works for text: percent-decoding produces bytes, and any byte that is not valid UTF-8 in the original does not come back.

Both encodings have to be quoted correctly where they are used. In CSS, url() accepts an unquoted URI with no spaces, parentheses or quotes; in HTML, an attribute needs its own quotes not to collide with the ones inside the data. The snippets on this page escape the double quote, which is the character that breaks an HTML attribute first.

When a real file wins

An asset used once, small, and needed before the stylesheet is parsed: a URI is a win, because the request it removes is a round trip that would otherwise happen. An asset used on every page, or large enough that the base64 overhead passes a few kilobytes: a file with a long cache header is better, because the browser downloads it once and reuses it across pages, which an inline URI cannot do.

The rule of thumb most performance guides settle on is a threshold around 2 KB of encoded output. Beyond it, the extra parse and the lost cache entry cost more than the request saved, and a page made of twenty inlined fonts is slower than the same page with ten real files.

What a data URI must not carry

Not a script, and not a page. Browsers refuse data: for top level navigation, most refuse it as a script source, and a strict content policy has to name data: explicitly in img-src or font-src before an inline asset loads at all. This page's own policy allows data: for images and fonts only, which is the shape a policy should take if it needs them.

For user-supplied content, a data URI is still user-supplied content: a decoded SVG can contain a script, and an <img> will not run it but an <object> or an inline SVG can. Decode a stranger's URI here to inspect it, and do not put it back into a page unexamined.

When the question is the encoding alone, Pikkola Base64 keeps the bytes and drops the media type. When the markup inside the URI needs drawing first, Pikkola SVG Studio writes it.

Questions

Is my file uploaded to encode it?

No. The browser's own file reader puts the bytes in memory, the encoder runs here, and the URI is printed as text. Nothing is stored, so closing the tab forgets the file, and the only request the page makes after it loads is the cookieless page-view count.

Which is smaller, base64 or percent-encoded SVG?

For an SVG with comments and indentation, percent after minifying is usually smaller: this page shows both numbers and the change against the source. For a PNG or a font, percent-encoding cannot be used at all, because the bytes are not text. When the two are close, choose base64: it survives any tool that rewrites the CSS.

Why do some data URIs break when I paste them into CSS?

Usually a character that ended the value early. A base64 body has no quotes and no parentheses, so it is safe inside url() as long as it is not wrapped in single quotes by mistake; a percent-encoded SVG that still contains a quote or a hash will end the URL or start a fragment. The outputs here escape both, which is why the CSS tab works as pasted.

Does a data URI get cached?

Only with the document that carries it. There is no separate cache entry, so it is downloaded again with the CSS or the HTML, but it also means it cannot go stale on its own. For an asset used once, that is a win; for one used on every page, a real file with a long cache header is better.

Is there a size limit?

Browsers accept large ones, but the practical limits are the length of the CSS or HTML document and the cost of keeping the decoded copy in memory. A few kilobytes is the design point; a hundred kilobytes inline is usually a mistake. If a single URI is over about 2 KB of extra characters, a real file with a cache header is the better trade.