What a data URI actually is
A data URI inlines a resource into the URL itself. The shape is data:[mediatype][;base64],data, so an embedded PNG looks like data:image/png;base64,iVBORw0KGgo…. There is no second request: the browser reads the bytes from the string it already has.
Base64 in one paragraph
Base64 maps every three bytes of binary data onto four ASCII characters drawn from A–Z, a–z, 0–9, + and /, with = padding at the end. The result is plain text, safe to paste into HTML, CSS, JSON or an email body. It is an encoding, not encryption: anyone holding the string can decode the image in seconds.
The size trade-off
Because 8 bits become 6 bits per character, Base64 output is about 33% larger than the original file, plus a short prefix. Inline images also cannot be compressed across the document the way a separate file can, and they are not cached independently.
- Good fit — favicons, small logos, single-use icons in a CSS file, decorative SVGs, and images inside an HTML email where external images are often blocked.
- Poor fit — photographs, hero images, anything reused across pages, and anything large enough to matter for first paint.
A rough rule: a few kilobytes inline is fine, hundreds of kilobytes belongs in a separate file with a long cache lifetime.
Compression and gzip
Text compresses well, but there is little entropy left in Base64 text, so gzip typically recovers only a small part of the expansion. SVG is the exception: a Base64-encoded SVG is compressed bytes, whereas the raw SVG source is text that gzips extremely well. For SVG, prefer data:image/svg+xml,<svg…> with URL-encoded markup, or a plain file.
Where a data URI is accepted
In img src, in CSS as url(data:…), in SVG href, in fetch() as a same-origin URL, and in most Markdown or HTML tooling. Some contexts reject them outright for security reasons: many email clients block data URIs, some CMS sanitizers strip them, and browsers refuse data: URLs for top-level navigation and for most script types.
Why a decoded image sometimes shows nothing
Base64 records bytes, not meaning. Pasted raw Base64 with no data:image/png;base64, prefix leaves the browser unable to guess the media type, so the image fails to render. The other frequent causes are truncated strings, whitespace inserted by a chat app, and characters replaced by an HTML entity such as <. If the preview stays empty, check the prefix and the character set before suspecting the file.
Recommended workflow
Resize and compress the image first, then encode. Encoding a 12 MP photo and pasting it into a stylesheet produces a page that is slower and harder to maintain than a normal image reference. For small assets, decode the size, keep the data URI in the stylesheet, and leave everything else as a separate file.
Data URIs in CSS: practical thresholds
Build tools usually inline assets below a fixed size, commonly between 4 KB and 10 KB. That works because the browser fetches one stylesheet instead of one stylesheet plus a dozen tiny images, and the saving is real on high-latency connections. Above the threshold the trade-off reverses: the stylesheet becomes render-blocking, it is downloaded again whenever any byte changes, and it cannot be cached separately from the page’s layout rules. If an icon appears in several stylesheets, inlining duplicates it in each one.
Content Security Policy and sanitizers
A page with a strict Content-Security-Policy must allow data: in the relevant directive, for example img-src 'self' data:, or the image silently fails. Allowing data: more broadly is risky: an SVG data URI can contain script, and older browsers have treated navigations to data URLs as same-origin documents. Many rich-text editors and CMS sanitizers strip data URIs outright because they cannot tell an image from a payload, so test the final rendered output rather than the editor preview.
When a separate file wins
If the image is larger than a few kilobytes, appears on more than one page, or changes on its own schedule, a normal file is easier to cache, easier to replace and easier to inspect. The same applies to anything a designer may need to update without a code deploy. Data URIs are a delivery optimisation for small, stable assets — not a way to avoid managing images.
Encode or decode in either direction with our image ⇄ Base64 converter. It reads the file locally with FileReader, shows the decoded type and size, and switches between a full data URI and bare Base64 — nothing is uploaded.