Format

What Is Base64 Image Encoding? A Developer's Guide

Base64 is an encoding scheme that converts binary image data into ASCII text, allowing images to be embedded directly in HTML, CSS, JSON, and other text-based formats without a separate file.

What is Base64?

Base64 is not an image format — it is an encoding scheme that represents binary data as a sequence of printable ASCII characters. Any binary file (image, font, PDF) can be Base64-encoded; the result is a text string that can be embedded anywhere text is accepted.

For images, Base64 encoding produces a data URI — a self-contained URL that contains the image data directly. Instead of referencing an external file (src="logo.png"), a Base64 image embeds the entire image inline (src="data:image/png;base64,iVBORw0KGgo...").

Base64 was not designed for images specifically — it was designed to transport binary data through protocols that only handle text (email, originally). Its use in images is a practical workaround for embedding content in text-based formats.

How Base64 encoding works

Base64 converts binary data to text using a 64-character alphabet: A–Z, a–z, 0–9, +, and /. The algorithm works like this:

  1. Take 3 bytes (24 bits) of binary data at a time.
  2. Split those 24 bits into four 6-bit groups.
  3. Map each 6-bit group to one of 64 characters in the Base64 alphabet.
  4. If the input is not a multiple of 3 bytes, pad the output with = characters.

Because 3 bytes of binary become 4 Base64 characters, Base64 encoding always adds approximately 33% overhead. A 100 KB PNG encoded as Base64 produces a 133 KB string.

The data URI format wraps the Base64 string with a MIME type declaration:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEA...

Use cases for Base64 images

  • HTML email: Many email clients block external image requests for privacy. Embedding images as Base64 ensures they always display, regardless of client settings. This is the primary real-world use case.
  • Critical above-fold images: Inlining a small hero image or logo as Base64 in your HTML eliminates the render-blocking HTTP request. Useful for images that must display immediately on first paint without waiting for a network round-trip.
  • CSS backgrounds for small UI icons: A 16×16 or 32×32 icon embedded as a data URI in CSS avoids an HTTP request. Tools like webpack and Vite do this automatically for images under a configurable size threshold.
  • JSON and API payloads: Some APIs accept or return image data embedded in JSON as Base64 strings, avoiding the need for multipart form uploads.
  • SVG data URIs: SVG files can be Base64-encoded or URL-encoded for use as CSS background images.
  • Configuration files: Embedding a small logo or icon in a configuration file (app manifest, email template) where external file references are impractical.

Limitations

  • 33% larger: Base64 encoding adds overhead. A 100 KB PNG becomes a 133 KB string. This directly increases page weight and CSS file size.
  • Not cacheable: An external image URL is cached by the browser after the first request. A Base64-encoded inline image is re-parsed on every page load and cannot be cached separately from the HTML/CSS that contains it.
  • Bloats HTML and CSS: A single 50 KB hero image as Base64 adds 67 KB of text to your HTML. Inline styles in the HTML source become very long and hard to read or maintain.
  • No lazy loading: Inline images are not deferrable — the browser must parse the entire Base64 string even for below-fold content.
  • Poor for large images: The combination of size overhead and cache limitations means Base64 should only be used for small images (typically under 4–8 KB before encoding).

When to use Base64 (and when not to)

Use Base64 when:

  • Embedding images in HTML email templates
  • Inlining critical icons under 4 KB in CSS (automated by build tools)
  • Passing images through text-only APIs
  • Storing images in config files or databases that don't support binary

Do not use Base64 when:

  • Serving images on web pages (use external files — they cache properly)
  • The image is larger than 4–8 KB (size overhead + no caching = poor performance)
  • You need lazy loading or responsive images
  • Multiple pages use the same image (external URL = shared cache; Base64 = duplicated on each page)

Base64 vs external image URL

PropertyBase64 data URIExternal image URL
HTTP requestNone (inline)1 request per image
Browser cachingNo (embedded in HTML/CSS)Yes (shared across pages)
File size overhead+33%None
Lazy loadingNot possibleFull support
Email client supportAlways displaysOften blocked
Best forEmail, tiny icons, API payloadsAll web pages

Frequently asked questions

How do I convert an image to Base64?

Use the Image to Base64 tool on this site. Upload your image, and the tool will output the Base64 string and the complete data URI (data:image/png;base64,...). You can then paste this directly into HTML, CSS, or JSON.

Does Base64 compress images?

No. Base64 encoding increases file size by approximately 33% compared to the binary image. If you want to reduce image file size, use a compression tool first (e.g., compress to WebP), then encode to Base64 if you need to embed it. The compression happens in the original format choice, not Base64 encoding.

What is a data URI?

A data URI is a URL scheme that embeds file content directly in the URL string, formatted as: data:[<mediatype>][;base64],<data>. For a PNG image: data:image/png;base64,iVBORw0KGgo.... This is the full string you paste into an HTML src attribute or CSS background-image property to embed an image without a separate HTTP request.

Can I use Base64 images in CSS?

Yes. Use the data URI as the url() value: background-image: url("data:image/png;base64,..."). This embeds the image directly in your CSS file, eliminating the HTTP request. Best for small icons and decorative elements — large images will significantly bloat your CSS file and hurt performance.

Base64 tools