Embedding images as Base64 data URIs: when it helps and when it hurts
A data URI lets you put an image directly inside your HTML or CSS instead of linking to a separate file. The image becomes a long Base64 string that the browser decodes in place. It is a genuinely useful technique, and also one that is easy to overuse.
What a data URI looks like
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...">
The pieces are the scheme (data:), the MIME type (image/png), the encoding (base64), and then the encoded bytes. The same form works in CSS: background: url(data:image/svg+xml;base64,...). The browser needs no extra network request; the image is already in the document.
The trade-off
Base64 is not compression. Encoding inflates the data by roughly 33 percent, because it represents three bytes of binary in four text characters. So an inlined image is always larger than the original file. What you save is the round trip: no separate HTTP request, no extra connection setup.
That trade mattered more under HTTP/1.1, where each request was expensive and browsers limited parallel connections, so bundling tiny images into the page was a real win. Under HTTP/2 and HTTP/3 many requests share one connection cheaply, which has narrowed the advantage. Inlining still helps for assets you need present the instant the HTML parses, but "save a request" is a weaker argument than it used to be.
SVG is the special case
For SVG specifically, you usually should not Base64 it at all. SVG is text, so you can inline it as a URL-encoded data URI instead:
background: url('data:image/svg+xml,%3Csvg xmlns=...%3E');
That skips the 33 percent Base64 penalty entirely, often making the inline SVG smaller than a linked file, and it stays readable and gzip-friendly. Reserve Base64 for genuinely binary formats like PNG and JPEG.
When to inline
- Good fits: tiny icons, a logo in an email, an SVG used once, or an image you need to be present the instant the HTML loads with no second request.
- Poor fits: large photos, anything reused across many pages, or assets that benefit from browser caching.
An inlined image cannot be cached on its own; it is re-downloaded with every copy of the page that contains it, and a CDN cannot cache it as a separate object. Worse, a large data URI sits in the HTML, which is usually render-blocking, so a heavy inlined hero image can delay first paint in a way a linked image would not. Build tools lean on this balance: most bundlers inline assets under a threshold (often 4 or 8 KB) and link anything larger automatically.
A practical rule: inline assets under a few kilobytes, link to anything larger, and prefer URL-encoded SVG over Base64. For email specifically, inlining and hosting both have quirks and some clients strip data URIs, so test in real clients before relying on either.
You can convert any image to a ready-to-paste data URI, and decode one back, with the Image to Base64 tool. It runs in your browser, so the image stays on your device. If you are working with raw Base64 text rather than images, the Base64 is not encryption post is worth a read.