What Base64 Image Encoding Actually Does
Base64 encoding converts binary image data into an ASCII string. The result can be embedded directly into HTML, CSS, JavaScript, or JSON without a separate file fetch. A Base64-encoded image is typically used as a data URI:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB...">
The browser decodes the string back into an image at render time. This is useful for small assets — icons, logos, tiny SVG graphics — where the HTTP request overhead outweighs the size increase.
The Five Output Modes
| Mode | Output | Use Case |
|---|---|---|
| 🔤 Raw Base64 | Just the encoded string | JSON payloads, databases, custom wrappers |
| 🔗 Data URI | data:image/png;base64,... | Direct src or url() value |
| 📄 HTML Tag | <img src="data:..."> | Paste directly into HTML |
| 🎨 CSS Rule | .cls { background-image: url(data:...); } | Inline backgrounds, icons |
| ⚙️ JS Snippet | const img = "data:..."; | Dynamic image loading |
Size Reality
When to Inline (and When Not To)
| Use Inline Base64 | Use a Real File |
|---|---|
| Small icons (< 5 KB) | Photographs and large images |
| Tiny SVG logos | Images used in many pages (cache benefit) |
| Email templates (no external fetch) | Images that change frequently |
| Single-page apps with critical inline assets | Anything over ~10 KB |
| CSS gradients / patterns | Hero images, product photos |
SVG Special Case
SVG is text, so it can be inlined as data:image/svg+xml;base64,... or directly as data:image/svg+xml;utf8,<svg>...</svg>. The Base64 version is safer for complex SVGs with quotes and special characters. For simple SVGs, the utf8 form is smaller and more readable.
What Base64 Encoding Cannot Do
- It cannot hide image content. Anyone can decode Base64 in seconds. It's encoding, not encryption.
- It cannot compress images. It makes them larger, not smaller. Use a real image optimizer first.
- It cannot improve performance for large images. It usually makes things worse by preventing caching.
- It cannot replace a CDN. For production assets, use a CDN with proper cache headers.
Frequently Asked Questions
Is Base64 image encoding secure?
Does Base64 make my images smaller?
Why is my Base64 image not loading?
base64, prefix, (4) the image format is not supported by the browser. Check the Data URI mode output for the correct format.Can I use Base64 images in email?
What's the maximum file size?
Does Base64 work with SVG?
data:image/svg+xml;base64,.... For simple SVGs, the utf8 form data:image/svg+xml;utf8,<svg>...</svg> is smaller and more readable.Why does the output have line breaks?
Can I decode Base64 back to an image?
Is my image uploaded to your server?
Should I inline all my images?
What is a data URI?
data:[<mime>][;base64],<data>. Browsers can render data URIs anywhere a normal URL is accepted.