When not to inline images as Base64
Published 2026-11-10 · the caching and performance trade-offs
The case for inlining is one sentence long: it removes an HTTP request. That sentence is true, and on a cold connection a request is expensive — DNS, TCP, TLS, then the file itself. Remove enough of them and a page gets measurably faster.
The trouble is that the sentence describes a narrow situation and gets applied to a broad one. Inlining is a trade, not an optimization. You give up independent caching, independent invalidation, and a place in the browser's priority queue, and you receive one fewer round trip. Whether that is a good deal depends entirely on which side of the trade your page sits on.
The one-fewer-request argument, and where it holds
HTTP/1.1 made request count matter enormously. Browsers opened a handful of connections per origin, so a page with thirty images queued them in waves, and each wave cost a round trip. Under those conditions, folding small assets into the document was a genuine win, and the technique earned its reputation honestly.
HTTP/2 and HTTP/3 changed the arithmetic. Multiplexing means many resources share one connection, so the marginal cost of an additional small request dropped sharply. Header compression removed most of the per-request overhead. The one-fewer-request argument did not become false, but it stopped being decisive for anything but the first few critical assets.
What still holds, and holds well:
- An asset needed on first paint, used once. A logo in the header, a small icon in the initial viewport. There is no second page view to amortize a request against, and the request would sit behind the document anyway.
- An asset smaller than the request that would fetch it. A request carries headers in both directions; a sub-kilobyte icon can genuinely cost more to fetch than to embed.
- A document that must stand alone. A single-file HTML export, an offline report, an artifact someone downloads and opens later. Here the request does not exist to remove — there is no server.
Everything else is a weaker case than it looks. See embedding images in a single HTML file for the scenario where the trade is clearly correct.
Caching is the cost you pay on every visit
A file with a URL gets its own cache entry. It can carry a long max-age, be marked
immutable, and be revalidated with a conditional request that returns a bodyless 304
when nothing changed. After the first visit it is effectively free, for as long as the header
allows.
Inline bytes inherit the document's caching, and nothing else. Three consequences follow, and they compound:
Every HTML fetch carries the image again
If the document is cached, the image is cached with it. If the document is revalidated or refetched — a new deploy, a cache miss, a user who cleared storage, a shared machine — the image bytes come along in full. There is no way to ask for the image alone.
One image edit invalidates everything
Change a pixel in an inlined icon and the HTML's content hash changes. Every visitor now re-downloads the entire document — markup, copy, and every other inlined asset — to pick up a change that should have cost one small request. With separate files you rename the one file and the rest of the cache survives.
Expiry is all-or-nothing
A logo that changes once a year and a page that changes weekly cannot have different cache lifetimes if the logo lives inside the page. You end up choosing between a short lifetime for the document (hurting the page) and a long one (making the logo stale). Separate files let you set each policy on its own terms.
Repetition turns a small overhead into a large one
This is the failure mode that shows up most often in real code, because it happens by accident. A component gets an inlined placeholder, the component is used in a list, and suddenly the same string is in the document twenty times.
The arithmetic is unforgiving. Inline an 8 KB avatar once and you added about 11 KB of text. Inline it twenty times and you added roughly 220 KB — to display twenty small circles. As a file, that is one download and nineteen cache hits, and the browser will decode it once.
The same applies across pages. An icon inlined into every page of a site is re-sent on every navigation that misses the document cache, while a shared sprite or icon file is fetched once and reused everywhere. If an asset appears more than once in your codebase, it wants to be a file.
Rendering and priority costs
The preload scanner has nothing to work with
Browsers speculatively scan the raw HTML byte stream for resource URLs before the parser
finishes building the DOM. That is how a normal <img> deep in the body starts
downloading while the parser is still on the <head>. A data URI offers no URL
to scan, so the image cannot be discovered early — it has to arrive as document bytes and then be
decoded.
Hero images and LCP get worse
Largest Contentful Paint is usually an image. Inlining it means the browser cannot begin fetching it until the HTML containing it has arrived, and the HTML is now larger by a third of the image's size. You have serialized two things that used to be parallel. For a hero photograph this is a straightforward regression; for a 2 KB icon it is noise.
Lazy loading stops applying
loading="lazy" works by deferring a fetch. There is nothing to defer when the bytes
are already in the document, so an inlined image is downloaded whether or not it ever scrolls
into view. On a long page with many below-the-fold images, that is the entire benefit of lazy
loading, discarded.
The document gets heavier to parse
Long attribute values are not free. They increase the byte stream the parser walks, they bloat the DOM node's attribute string, and they can push a document past the point where browsers handle it comfortably. A page with a dozen inlined images is a page whose HTML is mostly Base64.
Tooling you lose without noticing
Once an image is a string inside HTML, every tool that operates on images stops seeing it.
- Image CDNs and optimizers. Format negotiation, responsive variants, and on-the-fly resizing all key off a URL. Inline bytes bypass the pipeline entirely and ship at whatever size you encoded.
- Build-time optimization. Most bundlers can emit modern formats and multiple widths for a referenced asset. An inlined string is opaque to them.
- Analytics and monitoring. You cannot count requests for something that is not requested. Image-level metrics, cache hit ratios, and per-asset error rates all disappear.
- Debugging. The network panel shows nothing. A corrupted image produces a broken glyph with no request to inspect and no status code to read.
- Accessibility and metadata. EXIF stripping, alt-text linting, and content-hash-based integrity checks all assume a file.
Content Security Policy will sometimes forbid it outright
If your page sends a Content-Security-Policy with an img-src directive
that does not include data:, every inlined image is blocked. The browser reports a
violation and renders nothing — and the failure looks like a broken asset rather than a policy
decision, which sends people debugging the wrong thing.
Adding data: to img-src widens the policy for every image on the page,
not just the one you wanted to inline. Some teams accept that; others deliberately keep
data: out to make inline payloads impossible. The
data URI and CSP guide covers what the source expression
actually permits and how the directives interact.
Scenario by scenario
| Scenario | Better choice | Why |
|---|---|---|
| Favicon or 16 px UI icon, used site-wide | Inline only if it is one page; otherwise a file | Tiny either way, but a file caches across every page view |
| Logo in the header, one per page | Either; a file is the safer default | Small and needed early, but reused on every navigation |
| Hero photograph or banner | File | LCP candidate; inlining serializes the fetch behind the HTML |
| Avatar repeated in a comment list | File | One download and N cache hits instead of N copies of the string |
| Generated chart unique to the request | Inline | No cache value to lose, no reuse to amortize |
| Single-file HTML report or offline artifact | Inline | Self-containment is the requirement; there is no server to fetch from |
| Icon set, ten or more glyphs | File (sprite or font) | Ten inlined strings outweigh one sprite by a wide margin |
| Product images in a gallery | File | Large, numerous, below the fold, and benefit from lazy loading |
| Placeholder blur or tiny LQIP | Inline | Sub-kilobyte, needed on first paint, unique per image |
Image behind a strict CSP without data: | File | Inlining is blocked outright; the policy is the constraint |
Quick rules
If you want one heuristic rather than a table: inline when the image is unique, small, and needed on first paint. Everything else is a file.
Three questions, in order, settle almost every case:
- Does it appear more than once? Anywhere — same page, another page, another template. If yes, it is a file. This single question catches most of the real damage.
- Is it large or is it the LCP element? If yes, it is a file. Inlining a hero image is a measurable regression, not a micro-optimization.
- Does it change on a different schedule than the page? If yes, it is a file. Coupling an image's cache lifetime to a document's is the cost people forget until a deploy goes out to change one icon.
If all three answers are no, inlining is fine and probably slightly better than a request. Budget it: measure the encoded size before you commit, using the Base64 size calculator, and keep the total inlined weight for a page small enough that the document still arrives quickly.
When to inline, when not to
Inline:
- A unique, small asset needed on first paint.
- A self-contained deliverable with no server behind it.
- Content generated per request, with no reuse and no cache value.
- A placeholder or blur-up that must appear before anything else.
Keep as a file:
- Anything used twice, anywhere.
- Hero images, galleries, and anything below the fold.
- Assets that change on their own schedule.
- Anything you need to measure, optimize, or debug per asset.
- Pages under a CSP that does not permit
data:.
Related reading: the favicon guide for the small-unique-asset case done properly, online vs. local converters for the privacy side of encoding, and how much bigger Base64 actually makes a file for the byte arithmetic behind the numbers above.