Base64 Images and Data URIs: When They Help and When They Hurt
Embedding an image directly in your HTML or CSS looks tidy, but it comes with a size and caching cost. Here is when it is worth it.
What Base64 and data URIs are
Base64 is an encoding that represents binary data using 64 printable characters. Every 3 bytes of data become 4 characters, so the encoded text is about one third larger than the original. A data URI wraps it with a media type: data:image/png;base64,iVBORw0KGgo…. You can paste that string anywhere a URL is accepted.
Base64 is not encryption. Anyone can decode it, so it offers no protection.
How to use one
<img alt="" width="16" height="16"
src="data:image/png;base64,iVBORw0KGgo...">
.icon { background-image: url("data:image/svg+xml;base64,PHN2ZyB4..."); }Create one with the image to Base64 converter: drop an image and copy it as a data URI, an HTML tag, a CSS rule or Markdown. The same tool decodes a Base64 string back into an image so you can inspect or download it.
When it helps
- Tiny images such as small icons or a placeholder, where a separate request would cost more than the extra bytes.
- Single-file deliverables such as a standalone HTML report or an email template that must be self-contained.
- APIs and JSON that need to carry an image as text.
- Prototypes and demos where convenience beats performance.
When it hurts
- Size overhead: about 33% larger than the binary file, before compression.
- No separate caching: the image is re-downloaded with every copy of the HTML or CSS that contains it, instead of once and cached.
- Render blocking: large strings inside CSS make the stylesheet bigger and can delay rendering. A large inline hero image can also hurt Largest Contentful Paint.
- Harder to maintain: you cannot easily swap or optimise the image, and diffs become unreadable.
- Email caveats: support for data URI images in email clients is inconsistent, so test carefully.
- Security policies: a strict Content Security Policy may block data URIs unless allowed.
Better alternatives
- Inline SVG for icons and logos: scalable, small and styleable.
- Normal image files served with long cache headers, and responsive markup for photos.
- Optimised formats: compress images first with the PNG compressor or convert to WebP so any embedded version is as small as possible.
Size maths you can check
Encoded length ≈ 4 × ⌈n ÷ 3⌉ characters for n bytes. A 3 KB icon (3,072 bytes) becomes 4,096 characters, which is exactly one third larger. A 60 KB image becomes about 80 KB of text. Gzip or Brotli compression of the HTML or CSS recovers some of that overhead for text, but not all, and base64 text of already-compressed images compresses poorly.
Using data URIs safely
- Only embed images you trust. SVG data URIs can contain scripts when opened directly in some contexts, so sanitise untrusted SVG.
- Set a sensible limit (for example a few KB) in your build tooling so large images are never inlined by accident.
- If you use a Content Security Policy, allow
img-src data:only if you need it.
A simple rule of thumb
If the image is under a few kilobytes and used once on a page, a data URI is reasonable. If it is larger, reused across pages or part of the visible hero, link to a real file. See image optimisation for Core Web Vitals for how image loading affects page speed.
Try it now: Image to Base64
Free, private and runs in your browser. Nothing is uploaded.
Open Image to Base64Frequently asked questions
Sources and further reading
External links open the original source. We cite primary or official documentation for factual claims and re-check them when we update an article. See our editorial policy.
Keep reading
- QR Code Best Practices: How to Make One That ScansMake QR codes that scan every time: static vs dynamic, error correction, quiet zone, contrast, size, print tips and staying safe from malicious codes.
- Responsive Images in HTML: srcset, sizes and the picture ElementLearn how srcset, sizes and the picture element let browsers load the right image for each screen, with copy-paste examples and common mistakes.
- Image Optimization for Core Web Vitals: Fix LCP and CLSHow images affect LCP and CLS, with fixes: right size and format, fetchpriority, no lazy-loading the hero, and set dimensions on every image.