What Is Base64 Image Encoding?
Base64 is a binary-to-text encoding scheme that represents binary data using a set of 64 printable ASCII characters (A–Z, a–z, 0–9, +, and /). When you encode an image to Base64, you convert its raw binary bytes into a text string that can be safely embedded directly in HTML, CSS, JSON, XML, or any other text-based format.
The result is typically wrapped in a data URI that includes the MIME type: data:image/png;base64,iVBORw0KGgo.... This tells the browser exactly what type of image the data represents, so it can render it without fetching a separate file.
Why Convert Images to Base64?
There are several practical reasons why developers embed images as Base64 data URIs instead of linking to external files:
- Reduce HTTP requests: Every external image file requires a separate HTTP request. For small images like icons, logos, or UI sprites, the overhead of the HTTP request (DNS lookup, TCP handshake, TLS negotiation) can exceed the time spent transferring the actual image data. Embedding these as Base64 eliminates these requests entirely.
- Self-contained documents: HTML emails, single-file exports, and offline-capable pages benefit from having all assets embedded directly. A Base64-encoded image in an HTML email ensures the image displays even if the recipient’s email client blocks external image loading.
- CSS backgrounds: Small background patterns, gradients, and decorative images can be embedded directly in CSS using
background-image: url('data:...'), avoiding an extra file and HTTP request. - JSON and API payloads: When an API needs to accept or return images alongside structured data, Base64 encoding allows the image to be included as a simple string field in JSON.
- Bundling and build tools: Webpack, Vite, and other bundlers can automatically inline small images as Base64 data URIs during the build process, using a size threshold (commonly 4–8 KB) to decide which images to inline.
When Not to Use Base64 Images
Base64 encoding increases the data size by approximately 33% compared to the original binary file. A 30 KB PNG becomes roughly 40 KB of Base64 text. For large images, this overhead becomes significant:
- Large photographs (100 KB+) should be served as separate files, ideally in modern formats like WebP or AVIF with proper caching headers.
- Images that change frequently lose caching benefits when inlined. A separate image file with a long cache TTL is far more efficient.
- Above-the-fold hero images should load as fast as possible. Inlining them inflates the HTML document size, delaying the initial paint.
The rule of thumb: inline images smaller than 4–8 KB, serve larger images as separate files with CDN caching.
Supported Image Formats
This tool supports the five most common web image formats:
- PNG (Portable Network Graphics): Lossless compression, supports transparency. Ideal for screenshots, UI elements, icons, and diagrams.
- JPEG / JPG: Lossy compression optimized for photographs. Smaller file sizes but no transparency support.
- GIF (Graphics Interchange Format): Supports animation and simple transparency. Limited to 256 colors per frame.
- SVG (Scalable Vector Graphics): XML-based vector format. Infinitely scalable, very small file sizes for geometric shapes and icons. SVGs can also be embedded directly as inline HTML rather than Base64, which is often preferable.
- WebP: Modern format by Google offering both lossy and lossless compression with typically 25–35% smaller file sizes than equivalent JPEG or PNG. Supported by all modern browsers.
How Data URIs Work
A data URI follows the format: data:[mediatype][;base64],data. The components are:
data:— The URI scheme identifier.[mediatype]— The MIME type, such asimage/png,image/jpeg, orimage/svg+xml.;base64— Indicates the data is Base64-encoded (as opposed to URL-encoded text).,data— The actual Base64-encoded content.
Browsers parse data URIs natively. When you set an <img> element’s src to a data URI, the browser decodes the Base64 string directly into pixel data without making any network request.
Using Base64 Images in HTML
To embed a Base64-encoded image in HTML, set the src attribute of an <img> tag to the full data URI:
<img src="data:image/png;base64,iVBORw0KGgo..." alt="My image" />This works in all modern browsers. The image renders immediately without any additional HTTP request. Always include a meaningful alt attribute for accessibility.
Using Base64 Images in CSS
You can use a data URI as a CSS background-image:
.icon {
background-image: url('data:image/svg+xml;base64,PHN2ZyB4...');
background-size: contain;
width: 24px;
height: 24px;
}This pattern is common for small icons, loading spinners, and decorative elements. It avoids an extra HTTP request and keeps the image tightly coupled with the CSS that uses it.
Base64 Encoding: Technical Details
Base64 encodes every 3 bytes of binary data into 4 ASCII characters. Each character represents 6 bits of the original data (2^6 = 64 possible values). When the input length is not a multiple of 3, the output is padded with = characters. This 3:4 ratio is why Base64 output is approximately 33% larger than the input.
The encoding process uses the character set A-Z, a-z, 0-9, +, /. A URL-safe variant replaces + with - and / with _, though for data URIs the standard alphabet is used since the entire URI is treated as a single token.
Performance Considerations
When deciding whether to Base64-encode an image, consider these performance tradeoffs:
- Gzip compression: Base64 text compresses well with gzip/brotli. The 33% size increase is partially offset when the HTML or CSS file is served with compression (which it should be).
- Parse time: The browser must decode Base64 strings, which adds a small CPU cost. For a handful of small images this is negligible; for hundreds it can add up.
- Caching granularity: Inlined images cannot be cached independently. If the HTML page changes, the inlined image data must be re-downloaded too.
- HTTP/2 multiplexing: With HTTP/2, multiple small files can be fetched over a single connection with minimal overhead, reducing the benefit of inlining.
How This Tool Works
This tool runs entirely in your browser using the JavaScript FileReader API. When you drop or select an image file, the browser reads the file locally and converts it to a Base64 data URI using FileReader.readAsDataURL(). No data is sent to any server — the conversion happens instantly on your device.
The tool displays the original file size and the Base64 output size so you can assess the encoding overhead. It also provides ready-to-use code snippets for HTML <img> tags and CSS background-image properties.
Frequently Asked Questions
What is the maximum file size I can convert?
This tool accepts files up to 10 MB. However, for practical use, Base64 encoding is most beneficial for images under 8 KB. Larger images should be served as separate files for better performance.
Is my image uploaded to a server?
No. The entire conversion happens in your browser using the FileReader API. Your image never leaves your device. You can verify this by disconnecting from the internet after loading the page — the tool continues to work.
Why is the Base64 output larger than the original file?
Base64 encoding represents every 3 bytes as 4 ASCII characters, resulting in approximately 33% size increase. This is the inherent cost of representing binary data as text.
Can I convert Base64 back to an image?
Yes. Simply use the data URI as the src of an <img> element, or decode the Base64 string back to binary using atob() in JavaScript. Many online tools also offer Base64-to-image decoding.
Should I use Base64 for SVG images?
For SVGs, it is often better to embed them directly as inline SVG in HTML rather than Base64-encoding them. Inline SVGs can be styled with CSS and manipulated with JavaScript. However, Base64-encoded SVGs are useful in CSS background-image properties where inline SVG is not possible.
Do all browsers support data URIs?
Yes. All modern browsers (Chrome, Firefox, Safari, Edge) fully support data URIs. Internet Explorer supported them from version 8, with a 32 KB size limit that was removed in later versions. In practice, data URIs work everywhere today.