Image to Base64 Encoding Complete Guide

Embed images directly in HTML, CSS, and JSON using Base64 encoding — no external files, no HTTP requests.

~33% size overhead No HTTP requests Works in all browsers

Base64 encoding converts binary image data into a text string of 64 printable ASCII characters. This allows images to be embedded directly in HTML, CSS, and JSON without external file references.

The technique is widely used for small icons, email templates, and offline-capable apps. Understanding when Base64 helps and when it hurts performance is essential for modern web development.

What is Base64 image encoding

Base64 is a binary-to-text encoding scheme that represents binary data using 64 characters: A–Z, a–z, 0–9, +, and /. Each group of 3 bytes (24 bits) of binary data maps to 4 Base64 characters. Images encoded in Base64 can be included in HTML as data URIs (data:image/png;base64,...) or in CSS as background-image values.

Free Tool Image to Base64 Encoder Convert any image to a Base64 data URI instantly

How Base64 encoding works

The encoder reads the image as raw bytes, splits them into 6-bit groups, and maps each group to one of 64 characters. The output ends with = or == padding to make the length a multiple of 4. A 1KB PNG file becomes approximately 1.37KB of Base64 text. The browser decodes the string back to binary before rendering.

Using Base64 in HTML and CSS

A Base64-encoded image can be used anywhere a URL is expected. In HTML as an img src, in CSS as background-image, or in SVG as an image href. The data URI format is:

<!-- HTML -->
<img src="data:image/png;base64,iVBORw0KGgo..." />

/* CSS */
.icon &#123;
  background-image: url('data:image/png;base64,iVBORw0KGgo...');
&#125;
Free Tool Base64 Encoder / Decoder Encode and decode Base64 strings for text and data

Size overhead and performance

Base64 adds approximately 33% size overhead compared to the original binary file. A 10KB PNG becomes ~13.3KB of Base64. However, embedding eliminates the HTTP request — for small files (< 5KB), this trade-off is favorable. For larger images, the overhead outweighs the request savings. Additionally, Base64 strings are not separately cacheable by the browser.

When to use Base64 images

Use Base64 for: small icons and favicons (< 5KB) embedded in CSS to avoid multiple requests; email templates where external images may be blocked; offline-capable Progressive Web Apps; SVG files with embedded bitmap textures; JSON payloads that need to include image data. Avoid for: large images > 10KB, frequently changing images, images that benefit from CDN caching.

Limitations and alternatives

Limitations: no browser caching, 33% size increase, long strings in source code. Alternatives: CSS sprites combine multiple small images into one file; HTTP/2 multiplexing reduces the cost of multiple requests; inline SVGs avoid the overhead while staying text-based. For email, consider using hosted images with a reliable CDN when Base64 strings are too long for email clients.

Free Tool Favicon Generator Generate favicons in all required sizes from any image

FAQ

What is Base64 image encoding? +
Base64 encoding converts binary image data into a printable text string using 64 ASCII characters. It allows images to be embedded directly in HTML, CSS, or JSON without separate file references.
How much larger is a Base64 image than the original? +
Base64 adds approximately 33% overhead. A 10KB PNG becomes about 13.3KB of Base64 text. This is the trade-off for eliminating an HTTP request.
How do I use a Base64 image in HTML? +
Use it as a data URI in the src attribute: . The same format works in CSS as background-image: url('data:image/png;base64,...').
Does Base64 work with all image formats? +
Yes. The MIME type in the data URI specifies the format: data:image/png, data:image/jpeg, data:image/gif, data:image/webp, data:image/svg+xml. All browsers support the common formats.
Should I use Base64 for large images? +
No. For images larger than 5–10KB, the 33% size overhead outweighs the HTTP request savings. Use Base64 only for small icons, thumbnails, and placeholders.
Are Base64 images cached by browsers? +
Not separately. The Base64 string is part of the HTML or CSS file, so it is cached as part of that file. The image itself cannot be independently cached or updated without changing the parent file.
Can I use Base64 images in emails? +
Yes. Some email clients block external images but always render inline Base64 images. However, Base64 significantly increases email size, which can trigger spam filters. Test with your email provider.