Image to Base64: What It Is and How to Convert
Published Sep 8, 2026
By The ImageTools Pro team · Last reviewed Sep 8, 2026
Base64 lets you embed images directly inside HTML, CSS, or JSON — no separate file. Learn what Base64 is and how to convert any image to Base64 online.
Image to Base64: What It Is and How to Convert
Base64 encoding turns an image into a text string you can paste directly into HTML, CSS or a JSON field. No separate file, no upload step, no broken image path. The trade-off is size: the string is always about a third larger than the original bytes. Here is how it works, when it helps, and how to avoid the mistakes that make an embedded image fail.
What Base64 actually does
Base64 represents binary data using 64 printable characters. Every 3 bytes of the source become 4 characters of output, which is where the 33% overhead comes from. If the input length is not divisible by 3, the encoder adds padding characters at the end.
A complete data URI has two parts: a prefix that declares the MIME type, and the encoded payload. The prefix looks like data:image/png;base64, followed by the string. Without the prefix, a browser treats the text as an ordinary string, not as an image.
| Image type | MIME prefix |
|---|---|
| JPEG | data:image/jpeg;base64, |
| PNG | data:image/png;base64, |
| WebP | data:image/webp;base64, |
| GIF | data:image/gif;base64, |
| SVG | data:image/svg+xml;base64, |
The real cost, measured
Base64 is not compression. It is an encoding, and it makes the payload bigger. These figures use our own fixture files and the standard 4-characters-per-3-bytes rule.
| File | Original size | Base64 characters | With prefix | Overhead |
|---|---|---|---|---|
| Transparent logo | 129,117 bytes | 172,156 | 172,178 | 33% |
| Screenshot | 197,518 bytes | 263,360 | 263,382 | 33% |
| 12 MP photo | 1,360,339 bytes | 1,813,788 | 1,813,810 | 33% |
For ASCII text, one character is one byte in UTF-8, so those character counts are also byte counts. The 12 MP photo becomes a 1.8 MB string before it is pasted into anything. That is fine for a build step and painful for a hand-edited HTML file.
When Base64 helps
| Situation | Verdict | Why |
|---|---|---|
| Small logo inside a CSS file | Good | Removes one request and keeps the asset with the style |
| Single-file HTML demo | Good | One file really does contain everything |
| Favicon or tiny icon | Good | The overhead is a few hundred bytes |
| JSON field for a small image | Workable | The alphabet has no quotes or backslashes, so it needs no escaping |
| Large photo in a page | Bad | Adds 33% to an already heavy asset and blocks caching |
| The same image on many pages | Bad | The string is repeated in every document instead of being cached once |
| Email signature | Test it | Some email clients and security filters block data URIs |
The caching point is the one people miss. A normal image file is downloaded once and reused from the browser cache. An inline data URI travels with every HTML or CSS document that contains it, and it cannot be cached as a separate resource.
How to convert an image to Base64
- Open Image to Base64.
- Drop a JPG, PNG, WebP or GIF file.
- Click Convert to Base64.
- Copy the full result, including the data: prefix.
- Paste it into the target attribute or download it as a text file.
- Open the page and confirm the image renders before you ship it.
Pasting it correctly
- HTML: put the entire string in the src attribute of an img tag. Do not add a file path or a second prefix.
- CSS: use the string inside a quoted url value for background-image. The Base64 alphabet includes plus, slash and equals, all of which are safe inside quotes.
- JSON: paste the string as a normal JSON string value. Base64 contains no double quotes or backslashes, so it does not need escaping.
- No line breaks: a wrapped string is still valid in some parsers but breaks in others. Keep it on one line.
Common failures
- Missing prefix. The payload alone is not a data URI. The browser needs the MIME declaration.
- Truncated string. Base64 payloads have a length that is a multiple of four, including padding. One missing character can break the whole decode.
- Wrong MIME type. Declaring a PNG as image/jpeg may render in a forgiving browser and fail in a strict consumer.
- Copied from a terminal that wrapped the line. Remove the newlines before pasting.
- Double encoding. Encoding an already encoded string produces a much larger payload that no browser can display.
- Using it for a multi-megabyte photo. The page becomes heavier than the image ever was as a separate file.
A quick validation checklist
- Does the string start with data:image/ and the correct subtype?
- Is the payload free of spaces and line breaks?
- Is the total payload length a multiple of four?
- Does the image render in the target browser, email client or app?
- Did you keep the original file for future edits?
For the complete reference, including how the encoding maps to bytes and how to validate a data URI, read the Image to Base64 guide.
Convert your first image now: Image to Base64