Developer Guide

Base64 Data URIs for Images, the +33% Overhead and When Inlining Wins

By Muhammad Waseem Updated 832 words

Base64 turns every 3 binary bytes into 4 text characters, so an inlined image is about one-third larger than the same file served as a separate resource. The reason anyone pays that tax is to remove an HTTP request, and on modern HTTP/2 that saving is often smaller than people assume, so inlining is a trade, not a free win.

Encode, decode and inspect the exact byte cost with the browser base64 encoder, nothing is uploaded, shrink the source with the image compressor before you inline it, and read the honest rules below for when a data URI actually helps.

The +33% overhead, precisely

Base64 encodes 3 bytes into 4 ASCII characters, so encoded size grows by a factor of 4/3, about 33%, plus a couple of padding characters. A 12 KB icon becomes roughly a 16 KB string. A 150 KB photo becomes about 200 KB of text. That growth is why you never base64 a large image and expect it to behave like the file.

A data URI embeds that string directly in HTML or CSS, e.g. <img src="data:image/png;base64,...">, so the browser gets the pixels without a second request, the single genuine benefit, and the reason it is limited to tiny assets.

The request saving is smaller on HTTP/2

On old HTTP/1.1 with a handful of parallel connections, killing one request for a small icon was a real win. On HTTP/2, and HTTP/3, many resources share one multiplexed connection, so the per-request cost that data URIs dodge is much lower, which means the 33% size penalty now looks worse, not better.

There is a subtler cache cost too. A data URI inside your CSS or HTML is re-sent whenever that document or stylesheet changes, because it is not a separately cacheable file. A standalone image, cached by URL and content-hashed, is re-fetched far less often. Inline a sprite you change rarely. Think twice before inlining anything a returning visitor already has cached.

What to inline, and what never to

Data URIs earn their overhead for very small, request-sensitive, above-the-fold assets. A critical icon, a tiny logo, a background texture needed before first paint. The threshold is genuinely small, tens of bytes to a few kilobytes, not hundreds.

AssetInline as data URI?Better choice
1, 2 KB icon used once, above foldYesData URI removes a round trip.
Small SVG logoOftenInline SVG markup beats base64 for vectors.
Repeated spriteMaybeOnly if it is cached and changes rarely.
Content photographsNoServe as files. +33% text is a lot of wasted bytes.
Large hero imageNoCompress and serve as a normal optimized image.

For vector shapes, inline the SVG markup itself rather than a base64 PNG, it stays human-editable, gzips well, and scales, while the color picker helps you pull exact hex values into it.

Building and checking a data URI

Compress the image first with the compressor, then paste or load it in the encoder to get the string, and confirm the decoded round-trip matches. Read the encoded length before you ship it. If a tiny icon base64s to tens of kilobytes, you have inlined something too big and should serve it as a file instead.

To see the damage in a real page, run the HTML through the HTML beautifier and search for data:image, a large document full of base64 blobs is a classic sign of over-inlining, and the readable output makes the offenders obvious.

All of this happens locally on your machine, so an unreleased asset or a private image never leaves your device while you test the encoding.

Frequently Asked Questions

How much bigger is a base64 image than the file?

About 33% larger, because every 3 bytes become 4 characters. A 12 KB image is roughly 16 KB as a data URI, and the penalty scales, which is why base64 suits tiny assets and wastes real bytes on large images.

Does inlining a base64 image speed up my page?

Sometimes, for very small critical assets, because it removes one request. On HTTP/2 the request saving is smaller and you pay the 33% size cost, and inlined data is not separately cacheable, so measure, and default to a normal optimized file for anything sizable.

Can a data URI be cached by the browser?

Not on its own, it lives inside the HTML or CSS that contains it, so it re-downloads whenever that document changes. A separate image URL with a content hash caches far better, which matters for assets a returning visitor already has.

Should I inline SVG as base64?

Usually no, inline the SVG markup directly. It stays editable, compresses well over the wire and scales perfectly, so you get the request saving without the base64 bloat that a raster PNG would carry.

When is a data URI the right tool?

For tiny, request-sensitive, above-the-fold assets like a critical icon or small logo needed at first paint. Keep the payload to a few kilobytes and prefer files for photos and anything large.

Is it safe to base64 an image online?

With this encoder yes, it runs in your browser and uploads nothing, so a private or unreleased asset is never sent to a server. Encoders that take your file remotely keep a copy you cannot recall.

Related guides

Browse all 57 guides

Recognised & listed on