Base64 Images: When to Use Data URLs (and When Not To)
What a base64 image actually is
A base64 image is not a new image format. The picture is still a PNG, JPG or WebP — base64 is only a way of writing its bytes using 64 safe text characters, so they can live inside a file that is otherwise plain text.
Written out in full it is called a data URL, and it always follows the same shape:
Three parts, separated by punctuation:
- data: — tells the browser this is inline content, not an address to fetch.
- image/png;base64 — the MIME type, then the encoding used.
- iVBORw0... — the encoded bytes themselves, which is the long tail of the string.
Because it is just text, you can drop it into an <img src>, a CSS background-image, or a JSON response. The browser decodes it back into a picture on the fly, with no separate file involved.
Why the file gets about 33% bigger
Base64 encodes every 3 bytes of binary data into 4 text characters. Three bytes is 24 bits, and each base64 character carries 6 bits, so 24 bits needs exactly 4 characters. That gives a fixed overhead:
- Encoded size ≈ original size × 1.333
- A 30 KB icon becomes roughly 40 KB of text
- A 1 MB photo becomes roughly 1.33 MB of text
That overhead never shrinks, no matter which image format you start from. It is the price of representing binary data using only printable characters.
When a data URL genuinely helps
- Tiny icons and UI graphics — a 1 KB icon pasted inline saves a round trip that costs more in latency than the image itself.
- Single-file deliverables — an HTML email, a self-contained demo page, or a document that must work offline with no accompanying assets folder.
- Guaranteeing a critical graphic paints immediately — inlining a small above-the-fold element means it cannot be delayed by a failed request.
- APIs and configs that only accept strings — some build tools and JSON payloads cannot reference a separate file.
A widely used rule of thumb: inline only if the encoded result stays under roughly 2 KB. Beyond that, the size penalty starts costing more than the request you saved.
When you should not use one
- Large images — the 33% overhead scales linearly. Inlining a 500 KB photo adds about 165 KB for no benefit.
- Anything that needs independent caching — an external image file can be cached on its own. Inlined, it is re-downloaded every time the host document changes.
- Images you want found in image search — search engines index image files at their own address. A data URL has no address to index.
- Content-heavy pages — a wall of base64 text bloats the HTML and slows down parsing on weak connections.
One more consideration: under HTTP/2 and HTTP/3 many small requests are multiplexed cheaply, so the old "one request saved" argument is much weaker than it used to be.
How to convert an image to base64
The Base64 Converter does both directions and runs entirely in your browser, so the file never leaves your device. Upload an image to get the data URL, or paste a string back in to decode it and download the original picture.
If you need it in code instead, every common runtime has a built-in way:
- JavaScript —
FileReader.readAsDataURL()for files,btoa()for strings - Python —
base64.b64encode(open('a.png','rb').read()) - Command line —
base64 -i image.png
FAQ about base64 images
data:image/png;base64, prefix automatically before parsing. If the original is a JPG but the prefix says PNG, most browsers still identify the real type correctly.