Short answer: a data URI is the image's raw bytes, base64-encoded, pasted straight into your HTML or CSS as data:image/png;base64,iVBORw0KGgo.... You can make one with an online converter or in the browser with FileReader.readAsDataURL. It saves an HTTP request but inflates the image by about 33% — so it is a win for tiny icons and a loss for photos.
What a data URI actually is
The format is data:[mime type];base64,[encoded bytes]. The mime type tells the browser what the bytes are — image/png, image/jpeg, image/webp, or image/svg+xml — and everything after the comma is the base64 payload. The browser decodes it exactly as if it had downloaded a file, except there is no download: the bytes are already in the document.
Why base64 costs exactly 33%
Base64 exists to carry binary data through channels that only accept text, and its price is fixed by arithmetic, not implementation. It encodes every 3 bytes of input as 4 characters from a 64-character alphabet — so the output is always 4/3 the input, a 33.3% overhead, before you count the data: prefix. A 3 KB icon becomes 4 KB of text: a fair trade for removing a request. A 300 KB photo becomes 400 KB of text welded into your HTML: the same trade, at a scale where it stops being fair. There is no smarter base64 that avoids this — the ratio is the format. Every decision in this post is really the question of whether avoiding one request is worth a guaranteed one-third size increase on that image, and the answer flips somewhere around a few tens of kilobytes.
Make one in the browser
No server needed. FileReader.readAsDataURL reads a file and hands you the complete data URI, prefix included:
function imageToDataUri(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => resolve(reader.result);
reader.onerror = () => reject(reader.error);
reader.readAsDataURL(file);
});
}
// Wire it to a file input
const input = document.querySelector("#upload");
input.addEventListener("change", async (e) => {
const uri = await imageToDataUri(e.target.files[0]);
document.querySelector("#preview").src = uri; // "data:image/png;base64,..."
});The same string drops into CSS as a background — background-image: url("data:image/png;base64,...") — which is where data URIs earn their keep most often: tiny repeating textures, custom bullets, and icons that belong to one component and nowhere else. In HTML it goes straight into an img src. Both work identically, because to the browser the data URI simply is the image.
When it helps
- Tiny icons and logos: saving one HTTP request outweighs the 33% size penalty when the file is a few kilobytes.
- Email signatures: many email clients block external images until the reader allows them — an embedded data URI renders immediately.
- Single-file HTML: demos, reports, and saved pages that must work with no external assets.
- Blur-up placeholders: embed a tiny low-res preview that shows instantly while the full image loads.
The caching problem, in detail
The trap list below names it briefly; here is why it deserves the emphasis. A linked image is cached on its own schedule: change your page a hundred times and the browser fetches the logo once. A data-URI image has no separate existence — it is part of the HTML or CSS file, so it is downloaded again every time that file changes or its cache expires, and it inflates the file that every single page view must load before anything renders. Worse, the image cannot be shared across pages: the same embedded logo in ten pages is ten downloads. This is why the size threshold matters more than the technique — under a few kilobytes, none of this costs anything real; at photo sizes, you have built a system where your heaviest assets ride inside your most-refetched file. Modern HTTP/2 and HTTP/3 also weakened the original argument: multiplexed connections made extra requests cheap, which moved the break-even point down, not up.
Prefer plain SVG for icons
If the icon is a vector, skip base64 entirely and inline the SVG as plain text. It is smaller than the base64 version, you can style it with CSS (fill, stroke, currentColor), and there is nothing to decode. Base64 only wins when the source is already a raster image you cannot replace. And if the SVG itself is a bloated editor export, shrink it first — How to Reduce SVG File Size covers the SVGO pipeline that routinely cuts 30–70% before the icon goes anywhere near your markup.
Common mistakes and how to fix them
- Embedding the hero photo. Cause: applying a small-icon technique at photo scale. Fix: photos stay as files, always — the 33% tax plus the caching loss makes embedded photos the worst of both worlds.
- The wrong mime type in the prefix. Cause: copying a data URI and swapping the image without updating data:image/png to match. Fix: the mime type must describe the actual bytes; browsers are forgiving until the day they are not.
- Forgetting the comma or the ;base64 marker. Cause: hand-trimming the string. Fix: a data URI is data: + mime + ;base64, + payload — generate it with a tool or FileReader rather than editing it by hand.
- Base64-encoding an SVG instead of inlining it. Cause: treating all images alike. Fix: SVG is already text — inline it raw (URL-encoding only the few characters CSS requires) and keep CSS styling access that base64 throws away.
The traps
- The 33% bloat is real: base64 turns every 3 bytes into 4 characters. A 300 KB photo becomes 400 KB of text inside your HTML.
- No separate caching: the image is re-downloaded every time the HTML or CSS file changes, even if the image did not.
- Large data URIs delay rendering: the browser must parse all that text before it can lay out the page.
- Hard to edit: the image is buried in code, invisible to asset pipelines and image optimizers.
Frequently asked questions
What is the maximum size for a data URI image? There is no single spec limit — browsers accept data URIs of megabytes — but practical limits arrive much earlier: Internet Explorer historically capped them at 32 KB, and long before any hard limit, the render-blocking and caching costs make large ones a bad idea. Treat a few tens of kilobytes as the working ceiling and you will never meet the real one.
Do data URIs work in CSS and email? In CSS, yes, everywhere modern. In email, mostly — Gmail and most major clients render embedded images, which is why signatures use them — but Outlook has historically been patchy, and some clients strip or block them. Test in the clients your audience actually uses before relying on it for anything important.
Is a data URI slower to decode than a file? The decode itself is fast; the cost is upstream. The browser cannot start on the image until the containing HTML or CSS has downloaded and parsed past it, and the 33% larger payload travels over the network as text. For small images that sequencing is exactly the win — no separate request — and for large ones it is exactly the loss.
Can I convert a data URI back to an image file? Yes — everything after the comma is ordinary base64. Decode it, save the bytes with the extension the mime type names, and you have the original file back, byte for byte. Nothing is lost in either direction; base64 is a transport encoding, not a compression.
Try it
Drop an image into the Image to Base64 converter and copy the data URI — everything runs in your browser, nothing is uploaded.