Key takeaways
- Base64 turns binary image bytes into ASCII text so they can live inside HTML, CSS, JSON and email — at the cost of about 33% size inflation.
- The small-asset rule: data URIs are for files under roughly 2 KB (icons, spinner frames, tiny gradients); anything bigger belongs in a requestable file.
- Inlining saves the extra HTTP request — which matters for a hero-page icon and is noise for everything the browser loads later anyway.
- Email HTML is the exception zone: many clients block remote images by default but render data URIs, which flips the usual size advice.
- Base64 is encoding, not compression — the string is bigger than the file, and the browser decodes it back to identical bytes.
What the Encoding Actually Does
Binary files contain bytes across the whole 0-255 range, including values that would break text protocols — HTML, CSS, JSON, email and URLs all assume readable characters. Base64 re-expresses every three bytes as four ASCII characters drawn from a safe alphabet, so the data can sit inside text anywhere text is allowed. The decoded result is byte-identical to the file: encoding, never compression — the output string is about a third longer than the original.
The complete form is a data URI: data:image/png;base64,iVBORw0KG... — a scheme, a MIME type telling the browser how to interpret the payload, and the characters. It works anywhere a URL works: an <img src>, a CSS background-image, an SVG <image href> inside a single self-contained file, an @font-face for fonts, a JSON blob that needs to carry a picture with no hosting.
Converting Both Directions
Image to Base64: the browser tool reads the file, encodes it and hands you the full data URI plus the bare string, ready to paste. The output is deterministic — the same file always produces the same characters — so you can diff it, and the MIME header tells you what the file was (a useful forensic aside: pasting an unknown string into the decoder shows whether the mystery asset was a PNG or a JPEG).
Base64 to image: paste the string (the header prefix optional) and the decoder rebuilds the exact file for download. This direction is where the format gotcha lives — the 33% inflation is reversed on decode, so a 200 KB file arrives as a 267 KB string, and a 'truncated' error almost always means whitespace or newlines got lost in the copy-paste. Whitespace-tolerant decoders handle the pretty-printed case; a raw base64 with embedded newlines is still valid input.
The Size Arithmetic That Decides Everything
Every use of a data URI is a bet that saving one HTTP request beats paying the inflation. Do the actual maths for the asset: a 1.2 KB favicon inlined costs 1.6 KB of string and zero requests — a clear win on the critical path. A 40 KB logo inlined costs 53 KB and is only a win if it loads on first paint and the alternative request would be the difference between hitting and missing a rendering deadline.
The failure mode to avoid is the page that inlines everything: a 300 KB Base64 photo inside the HTML makes the document itself the payload, blocks first paint while it parses, kills the HTML cache (one pixel changes the icon, every page carrying the string re-downloads), and inflates again on every reuse. External files are cached once and referenced forever — which is the entire economy data URIs opt out of.
The rule of thumb the numbers converge on: under ~2 KB inline freely; 2-10 KB inline only for above-the-fold assets; above 10 KB serve a file. SVG icons break the arithmetic entirely — raw SVG in markup or as a file beats Base64-ing a PNG of it in every dimension — so for the modern icon case the real answer is usually 'just use the SVG'.
Where Data URIs Genuinely Win
Single-file deliverables: an HTML report, a one-page tool, a component demo you email to a colleague — anything that must work with zero external assets has one valid image format, and it is Base64. Email HTML is the adjacent case: clients that block remote images by default will render an inlined logo, which is why marketing-email builders convert icons to data URIs and accept the size hit — the one place the 33% is a bargain for display rate.
Critical-path micro-icons: spinner frames, the star rating glyph, a chevron — sub-kilobyte assets that a request would cost more than the payload, since a request pays DNS, TCP and TLS geometry before the first byte. Build tools inline exactly these, which is where the practice's respect comes from.
And the developer-hygiene case: pasting a string into a CodePen, a JSON config, a test fixture — a self-contained image reference that survives the host disappearing. The temporary-URL graveyard (deleted Imgur accounts, dead drive links) is a reminder that 'a URL' sometimes means 'a dependency'.
Cautions Worth the Ink
Content Security Policy: inline data URIs need explicit permission in a strict CSP (img-src data:), so teams that adopt them sometimes fight their own hardening — decide it deliberately rather than discovering it in a console error.
Size limits: some email clients cap total document size hard (Outlook's 20-version history of limits is the famous one), and a heavy inlined image can flip a message to clipped or 'images blocked' — the opposite of the reason it was inlined. And a search for 'how to decode a base64 image' is how someone learns what the string was; do not treat a data URI in a file you are publishing as invisible. It is readable.
Frequently asked questions
The questions people ask most about base64 images: how to embed a picture in html or css with no file, answered directly.