Encoding 2026-04-16

Fix Image to Base64 & Data URI Conversion Issues

Resolve image-to-Base64 problems: file size bloat, MIME type mismatches, data URI formatting errors, and browser compatibility issues with embedded images.

image to base64 not working data uri broken base64 image too large embedded image not showing

Converting images to Base64 data URIs is useful for embedding in CSS, HTML, or JSON, but it comes with pitfalls: 33% size increase, MIME type mismatches, and browser rendering issues.

Common errors covered

  1. 1 Wrong MIME type in data URI prevents display
  2. 2 Base64 increases file size by 33%
  3. 3 Line breaks in Base64 string corrupt the data URI
1

Wrong MIME type in data URI prevents display

Error message
Image does not display: broken image icon Resource interpreted as Image but transferred with wrong MIME type
Root cause

A data URI must specify the correct MIME type: data:image/png;base64,... for PNG, data:image/jpeg;base64,... for JPEG. A MIME type mismatch causes the browser to reject the image.

Step-by-step fix

  1. 1 Check the data URI prefix - it must match the actual image format.
  2. 2 Use our Image to Base64 tool which auto-detects MIME type.
  3. 3 Common MIME types: image/png, image/jpeg, image/gif, image/svg+xml, image/webp.
  4. 4 If unsure, check the file header bytes (magic numbers) to identify the format.
Wrong
<!-- PNG image with wrong MIME type -->
<img src="data:image/jpeg;base64,iVBORw0KGgo..." />
<!-- PNG data with JPEG MIME = broken -->
Correct
<!-- Correct MIME type for PNG -->
<img src="data:image/png;base64,iVBORw0KGgo..." />

2

Base64 increases file size by 33%

Error message
Page load time increased significantly after embedding images CSS file too large after Base64 embedding
Root cause

Base64 encoding converts 3 bytes of binary data into 4 ASCII characters, a 33% size increase. Plus, Base64 images cannot be cached separately.

Step-by-step fix

  1. 1 Use our Image to Base64 tool to check the encoded size.
  2. 2 Only embed images smaller than 10KB - larger images should use regular URLs.
  3. 3 For icons and small graphics, consider SVG (text-based, smaller, scalable).
  4. 4 Optimize images before encoding: compress PNG/JPEG first.
Wrong
/* Embedding a 500KB image = 667KB in CSS! */
.hero { background: url(data:image/jpeg;base64,...); }
Correct
/* Use regular URL for large images */
.hero { background: url('/images/hero.webp'); }
/* Only embed tiny images (<10KB) */

3

Line breaks in Base64 string corrupt the data URI

Error message
Image displays partially or not at all Base64 decoding error on some browsers
Root cause

Some encoders insert line breaks every 76 characters (MIME standard). While valid for email, data URIs require the Base64 string on a single line with no whitespace.

Step-by-step fix

  1. 1 Our Image to Base64 tool outputs single-line Base64 (data URI safe).
  2. 2 Remove all newlines, carriage returns, and spaces from the Base64 string.
  3. 3 In JavaScript: base64String.replace(/\s/g, '').
  4. 4 Verify that the string contains only valid Base64 characters.
Wrong
data:image/png;base64,iVBORw0KGgo
AAAANSUhEUgAAAAEAAAAB
<!-- Line breaks break the data URI -->
Correct
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB
<!-- Single continuous line -->

Prevention Tips

  • Only use Base64 data URIs for images under 10KB - use regular URLs for larger files.
  • Always verify the MIME type matches the actual image format.
  • Use our Image to Base64 tool which handles MIME detection and single-line output automatically.
  • Consider SVG for icons - it is text-based, smaller, scalable, and can be inlined directly in HTML without Base64.

Frequently Asked Questions

When should I use Base64 data URIs vs regular image URLs?

Base64 data URIs for: tiny images under 10KB, reducing HTTP requests, embedding in CSS/JSON/email. Regular URLs for: anything over 10KB, images that benefit from browser caching.

Can I use Base64 images in emails?

Some email clients support data URIs, but many (including Gmail) strip them. For email, use CID (Content-ID) attachments or hosted image URLs.

What image format is best for Base64 embedding?

SVG for icons/logos (smallest, scalable). WebP for photos (best compression). PNG for graphics with transparency. Avoid JPEG for very small images.

Related Error Guides

Related Tools

Still stuck? Try our free tools

All tools run in your browser, no signup required.