Base64 Images and Data URIs: When Inlining Helps and When It Hurts
A data URI lets you put an image directly inside your HTML or CSS instead of linking to a separate file. It looks like this: data:image/png;base64,iVBORw0KGgo…. Developers have used the technique since the late 2000s to save HTTP requests. It can genuinely help in the right place, and it can quietly slow a site down in the wrong one.
What Base64 is
Images are binary data: sequences of bytes that can contain any value. HTML and CSS are text. Base64 is a way of writing binary data using only 64 safe text characters: A–Z, a–z, 0–9, + and / (with = for padding).
It works by taking every 3 bytes (24 bits) of input and writing them as 4 characters of 6 bits each. That fixed 3-to-4 ratio is why Base64 is always about 33% larger than the original file. A 6 KB PNG becomes about 8 KB of Base64 text.
A data URI wraps that text with a media type:
data:[media type][;base64],[data]So data:image/webp;base64,UklGR… is a WebP image, inline.
A real example: this site’s 192 × 192 PNG app icon is 3,297 bytes. As a data URI it becomes 4,418 characters, 22 of them for the data:image/png;base64, prefix and the rest exactly a third bigger than the file. Gzip on the server claws some of that back, but the inline copy can’t be cached separately from the page that contains it.
Where data URIs help
Very small images. For a 1–2 KB icon, the overhead of a separate HTTP request (headers, round trips, connection handling) can outweigh the 33% size penalty. Inlining saves that request.
Self-contained documents. A single HTML file with all images inline can be emailed, archived or opened offline without missing pictures. Many export tools produce reports this way.
Critical above-the-fold placeholders. A tiny blurred preview (often under 1 KB) inlined in the HTML can show instantly while the real image loads, a technique used by many image-heavy sites.
Avoiding cross-origin issues in some canvas and testing scenarios, where an inline image is treated as same-origin.
Where they hurt
Larger images. A 100 KB photo becomes 133 KB, and that text is now part of the HTML or CSS file that must be downloaded and parsed before the page can render.
Caching. A separate image file can be cached once and reused across many pages. An image inlined in every HTML page is downloaded again with every page. If the image is in your CSS, any change to the CSS invalidates the whole file, image included.
Render blocking. CSS blocks rendering. Stuffing large images into a stylesheet delays the first paint of the entire page.
HTTP/2 and HTTP/3. Modern protocols multiplex many requests over one connection, which makes extra requests much cheaper than they were in the HTTP/1.1 era, when data URIs were most popular. The main argument for inlining has weakened.
A practical comparison
| Approach | Extra request | Size overhead | Cacheable separately | Best for |
|---|---|---|---|---|
| Normal image file | Yes | None | Yes | Most images |
| Base64 data URI | No | +33% | No | Tiny icons, single-file documents |
| URL-encoded SVG data URI | No | Small (often smaller than Base64) | No | Small SVG icons in CSS |
Inline <svg> in HTML | No | None | No | Icons you want to style with CSS |
| SVG sprite file | One for all icons | None | Yes | Icon sets on large sites |
A good rule of thumb: inline images under about 5–10 KB at most, and only if they’re used in one place.
SVG doesn’t need Base64
SVG is already text. You can put it in a data URI with URL encoding instead of Base64:
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E…%3C/svg%3E");This is usually smaller than Base64 and stays partly readable. Even better, for icons in HTML, paste the <svg> element inline, where it can inherit color with currentColor. Optimise the SVG first; our SVG optimization guide explains how.
Data URIs in email
Email is where people most often try Base64 images, and where they most often fail. Support varies a lot between clients: several popular clients, including Gmail on the web, don’t display data URI images in the HTML body, and some spam filters treat large Base64 blocks with suspicion. For newsletters, host images on a server and link to them, or use attachments referenced with Content-ID (CID) headers. Always test in real clients.
Security notes
Data URIs are just another way to deliver content, so they deserve the same caution:
- Browsers block top-level navigation to
data:URLs (for example from a link) because they were abused for phishing. - Content Security Policy (CSP) controls whether data URIs are allowed for images with the
img-src data:directive. - Never inline untrusted SVG, which can contain scripts if rendered as a document.
Base64 in JSON and APIs
Base64 also appears outside HTML and CSS. APIs sometimes return images as Base64 strings inside JSON, and some upload forms send them that way. That works for small thumbnails, but it has the same 33% overhead, and large Base64 strings make JSON slow to parse and hard to cache. For anything beyond small images, APIs usually return a URL to the image instead, or use multipart uploads that send the binary file directly.
| Situation | Better approach |
|---|---|
| Profile thumbnail in an API response | Small Base64 is acceptable; a URL is still cleaner |
| Full-size photo upload | Multipart form upload of the binary file |
| Images stored in a database | Store files in object storage; keep URLs in the database |
| Email templates | Hosted images or CID attachments |
Decoding Base64 back to an image
Sometimes you have a Base64 string, from an API response, a database or a stylesheet, and need the actual image. Paste it into a decoder that detects the file type from its first bytes: PNG data starts with iVBOR, JPEG with /9j/, GIF with R0lG and WebP with UklG. The image to Base64 converter above works in both directions, entirely in your browser.