Sometimes an image needs to travel inside text rather than as a separate file. A developer wants a tiny icon baked into a stylesheet so it cannot fail to load. An API expects a profile picture as a JSON string. A single-file HTML report must open offline with its logo intact. In each case the answer is a Base64 data URI: the image's bytes written out as letters and numbers.
This guide explains what Base64 encoding actually does, how the data: URL syntax works, and, most importantly, when inlining an image is a good idea and when it quietly slows a page down. It then shows how to produce the code in a browser, in a terminal and in a few programming languages.
How Base64 turns an image into text
An image file is binary: a sequence of bytes that can take any value from 0 to 255. Many text-based systems (HTML attributes, CSS, JSON, email bodies) cannot carry arbitrary bytes safely. Base64 solves this by taking the data three bytes at a time and writing each group as four characters from a 64-character alphabet: A-Z, a-z, 0-9, + and /, with = used as padding at the end.
Because every 3 bytes become 4 characters, the encoded text is about 33% larger than the original file:
encoded length = 4 × ceil(original bytes ÷ 3)
A 6 KB icon becomes roughly 8 KB of text. Compression by the web server (gzip or Brotli) recovers some of that for text-based responses, but not all of it.
Anatomy of a data URI
A data URI wraps the Base64 text with a header that tells the browser what it is:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...
data:marks the URL scheme.image/pngis the MIME type. It must match the real format (image/jpeg,image/webp,image/svg+xmland so on).;base64says the payload is Base64-encoded.- Everything after the comma is the encoded data.
You can use that string anywhere a normal image URL goes:
<img src="data:image/png;base64,iVBORw0KGgo..." width="24" height="24" alt="">
.icon { background-image: url("data:image/png;base64,iVBORw0KGgo..."); }
When inlining helps, and when it hurts
| Inline it | Keep a separate file |
|---|---|
| Tiny icons and UI sprites of a few kilobytes | Photos and anything over a few dozen kilobytes |
| Single-file HTML exports and offline reports | Images reused across many pages (they cache better as files) |
| API payloads that only accept JSON | Images that change independently of the page |
| Placeholder or blurred preview images | Content images you want indexed in image search |
The main costs of inlining are:
- No separate caching. An inlined image is downloaded again every time the HTML or CSS that contains it is downloaded.
- Heavier critical files. A large data URI inside a stylesheet delays the whole stylesheet, which can delay rendering the page.
- Harder maintenance. A 40,000-character string in a CSS file is impossible to review in a code diff.
A practical rule: inline only what is small and needed immediately. Our tool flags any result over 100 KB for this reason.
Encode an image with our tool
The Image to Base64 converter reads the file on your device and produces four ready-to-paste formats.
- Open Image to Base64 and drop an image onto the upload area. PNG, JPG, WebP, GIF, SVG, ICO and other image types up to 5 MB are accepted.
- Review the summary: Type, Dimensions, Original size, Base64 length and the Size overhead percentage, so you know exactly what inlining will cost.
- Copy the output you need:
- Data URI for an
srcattribute, CSSurl()or JSON field. - Raw Base64 without the
data:prefix, which most APIs and databases expect. - CSS background-image, a complete declaration.
- HTML
<img>tag, with width and height already filled in.
- Data URI for an
- Click Download .txt if the string is too long to copy comfortably, and Start over to encode another image.
Before encoding, shrink the image as far as it will go. Every kilobyte you save with the Image Compressor or by resizing is a kilobyte less of Base64 text.
Other ways to encode images
Terminal
On macOS and Linux:
base64 -i logo.png # macOS
base64 -w 0 logo.png # Linux, on one line
On Windows PowerShell:
[Convert]::ToBase64String([IO.File]::ReadAllBytes("logo.png"))
These print raw Base64 only; add the data:image/png;base64, prefix yourself.
In code
JavaScript in the browser can use FileReader.readAsDataURL(file). In Node.js, fs.readFileSync("logo.png").toString("base64") does it in one line. Python has base64.b64encode(open("logo.png", "rb").read()). These are the right choice when encoding is part of an application rather than a one-off job.
Build tools
Front-end bundlers can inline small assets automatically below a size threshold you configure. If you use one, let it decide rather than pasting data URIs by hand.
Tips and pitfalls
SVG often does not need Base64
SVG is already text. Percent-encoding it (data:image/svg+xml,%3Csvg...) is usually shorter than Base64. Clean the file first with the SVG Optimizer.
Email clients are inconsistent
Some email clients block or strip data URI images. For newsletters, hosted images or attachments referenced by content ID are more reliable.
Check the MIME type
If you label a JPEG as image/png, most browsers still display it, but some strict systems reject it. The tool reads the type from the file, so copying its output avoids this mistake.
Going the other way
To turn a Base64 string back into a file, use Base64 to Image. For non-image data such as text or tokens, the general Base64 Converter is the right tool.
Frequently asked questions
Why is my Base64 string bigger than the image?
Base64 writes every 3 bytes as 4 characters, so the text is about a third larger than the original file. That overhead is built into the encoding.
Is a Base64 image faster to load?
Only for very small images, where it saves a separate request. For larger images it is usually slower, because the data cannot be cached separately and makes the HTML or CSS heavier.
Can search engines index Base64 images?
Inline data URIs are not treated like normal image files, so they are unlikely to appear in image search. Use regular image files for content you want found.
Is Base64 a form of encryption?
No. Anyone can decode Base64 instantly. It only changes how data is represented, not who can read it.
What is the difference between the data URI and raw Base64?
The data URI includes the data: prefix and MIME type, so a browser can display it directly. Raw Base64 is just the encoded bytes, which APIs and databases usually expect.