Copied!
Back
Encoding Tool

Image to Base64 Encoder

Convert images to Base64 encoded strings and data URIs for embedding directly in HTML, CSS, and email templates. Drag and drop or select any image file — PNG, JPG, SVG, GIF, WebP, AVIF. Get ready-to-use HTML <img> tags and CSS background-image code. Reduce HTTP requests by inlining small images. All encoding happens in your browser using the FileReader API — no images are uploaded to a server.

Last updated: May 2026 · Reviewed by FreeDevTool web performance engineering team
base64-image.tool
🖼

Click to select an image or drag and drop

PNG, JPG, SVG, GIF, WebP · Max 10 MB

Base64 image encoding — when data URIs help, when they hurt

A Base64-encoded image is just a regular image file translated into 64 ASCII characters and embedded inline in your HTML or CSS. The browser decodes it on the fly and renders it just like a fetched file. The trade is intuitive: you trade a network request for a larger document. Whether that trade pays off is more nuanced than the usual "use it for tiny images" rule of thumb suggests. This guide walks through how the encoding actually works (RFC 4648), what it costs in bytes and parser time, the specific cases where inline data URIs win, and the cases where they silently slow your page down.

The mechanics — how Base64 turns binary into text

Base64 (RFC 4648) is a binary-to-text encoding. It takes 3 input bytes (24 bits) and emits 4 output characters (24 bits worth, at 6 bits each). The 64-character alphabet is A–Z a–z 0–9 + /, plus = as padding. A picture of the math:

The 33% size overhead is unavoidable: 4 output bytes / 3 input bytes = 1.333. A 30 KB PNG becomes a 40 KB Base64 string. After gzip compression Base64 closes some of the gap (around 70–80% of the source size compresses well), but the inflated payload is what the browser parser has to walk.

Data URIs — the wrapper that makes Base64 usable in HTML/CSS

A data URI (RFC 2397) is the specific URL scheme that lets you put inline content where a URL is normally expected. The structure:

data:[<mediatype>][;base64],<data>

Real examples:

Most browsers cap data URIs at ~32 KB in CSS and ~2 MB in HTML in practice. Older Edge and IE truncated at 4 KB; if you support old Microsoft browsers, keep inline assets under that.

When Base64 inlining wins — and when it loses

ScenarioInline as Base64?Why
One-pixel tracking GIFYesSub-100-byte payload; saves a request.
Tiny UI icon (< 2 KB) used on every pageYesCritical render path; HTTP request overhead exceeds size cost.
HTML email templateYesExternal images blocked by default in most clients.
SVG icon setUse inline SVG, not Base64Skip 33% encoding overhead; SVG is already text.
Hero image (50–500 KB)NoBlocks parsing, blows out HTML, defeats CDN caching.
Photo galleryNoSame as above; lazy-load with loading="lazy" instead.
Brand logo on every pageNoExternal file caches once; inline copies repeat per HTML response.
Image inside generated PDF / single-file HTML reportYesSelf-contained document is the goal.

The hidden costs of inlining

  1. Caching is killed. An external image cached by the browser is reused across pages. An inlined image is downloaded fresh in every HTML response.
  2. HTML parser pays the cost. The browser walks every byte of the data URI as part of parsing. Inlining a 50 KB image into your HTML adds 50 KB to LCP-blocking parser time.
  3. HTTP/2 and HTTP/3 multiplex. The original "save a request" argument was strongest in the HTTP/1.1 era when each connection was sequential. With HTTP/2 server push and HTTP/3 multiplexing, the cost of a separate image request is much smaller.
  4. CDN compression skips inlined assets. Your CDN compresses HTML once, then transparently re-uses that for every visitor. An external PNG/JPG goes through the CDN's image optimization pipeline (WebP/AVIF transcoding, resizing). Inline images get neither.
  5. Content-Security-Policy headaches. A strict CSP with img-src 'self' blocks data URIs unless you add data: — which weakens the policy.

Generating data URIs in 6 languages

The picker handles the browser case. For build pipelines, here is the same conversion in code:

Node.js:
const fs = require('fs');
const buf = fs.readFileSync('icon.png');
const dataUri = `data:image/png;base64,${buf.toString('base64')}`;
Python:
import base64, mimetypes
mime = mimetypes.guess_type('icon.png')[0]
data = base64.b64encode(open('icon.png','rb').read()).decode()
print(f"data:{mime};base64,{data}")
Go:
import ("encoding/base64"; "net/http"; "os")
b, _ := os.ReadFile("icon.png")
mime := http.DetectContentType(b)
fmt.Printf("data:%s;base64,%s", mime, base64.StdEncoding.EncodeToString(b))
PHP:
$data = file_get_contents('icon.png');
$mime = mime_content_type('icon.png');
$uri  = "data:$mime;base64," . base64_encode($data);
Ruby:
require 'base64'
require 'mime/types'
data = Base64.strict_encode64(File.binread('icon.png'))
mime = MIME::Types.type_for('icon.png').first.to_s
"data:#{mime};base64,#{data}"
Bash:
echo "data:image/png;base64,$(base64 -w 0 icon.png)"

SVG — the special case where Base64 is wrong

SVG is already a text format. Base64-encoding it adds 33% with zero benefit. Two better options:

For a 1 KB SVG icon, the URL-encoded form is ~1 KB; the Base64 form is ~1.33 KB; the inline element is ~1 KB and lets you change fill on hover. Inline wins when you can.

Common Base64-image mistakes

Frequently Asked Questions

What is Base64 encoding for images?
Base64 image encoding converts binary image data into an ASCII text string that can be embedded directly in HTML, CSS, JSON, or email templates. The result is a data URI like data:image/png;base64,iVBOR.... This eliminates the need for a separate HTTP request to load the image file — the image data is inline in the document itself.
Does Base64 encoding increase file size?
Yes. Base64 increases data size by approximately 33% because it encodes 3 bytes into 4 ASCII characters. A 10 KB image becomes ~13.3 KB. However, this overhead can be offset by eliminating an HTTP request — each request has ~200-500ms overhead on slow connections. Use Base64 for small images (under 10 KB) where the request savings outweigh the size increase.
When should I use Base64 images vs regular files?
Use Base64 for: small icons/logos under 10 KB, HTML email templates (external images are often blocked), single-file HTML documents, CSS sprites for tiny UI elements, and JSON payloads that include images. Use regular files for: any image over 10 KB, photographs, hero images, and anywhere caching and CDN delivery are beneficial.
How do I embed a Base64 image in HTML and CSS?
HTML: <img src="data:image/png;base64,iVBOR..." alt="description">. CSS: background-image: url('data:image/png;base64,iVBOR...'). The data URI includes the MIME type prefix so the browser knows how to render the image. Always add meaningful alt text for accessibility and SEO.
What image formats work with Base64?
Any image format: PNG, JPEG, GIF, WebP, SVG, BMP, ICO, AVIF. The MIME type in the data URI identifies the format (image/png, image/jpeg, image/svg+xml). Note: SVG files are often more efficient as inline <svg> elements rather than Base64, since SVG is already text-based.

Browse all 50 free developer tools

All tools run in your browser, no signup required, nothing sent to a server.