Sometimes you need an image inside a text file: a logo in a single-file HTML report, a small icon in a stylesheet, or a picture in a JSON request to an API. Base64 makes that possible by turning the bytes of an image into ordinary text characters. It is a handy technique with real costs, and this guide explains how it works, when it helps and when a normal image file is the better choice.
What Base64 actually is
Computer files are made of bytes, and many bytes do not correspond to printable characters. If you tried to paste the raw contents of a PNG into an HTML file, you would get broken symbols and invisible control characters that could corrupt the document. Base64 solves this by describing any binary data using only 64 safe characters:
- the capital letters A to Z,
- the lowercase letters a to z,
- the digits 0 to 9,
- and two symbols, plus (+) and slash (/).
An equals sign (=) is used as padding at the end when needed. These characters survive almost anywhere text can go: HTML, CSS, JSON, XML, email and configuration files.
How the encoding works
Base64 reads the data three bytes at a time. Three bytes are 24 bits, which it splits into four groups of 6 bits. Each 6-bit group is a number from 0 to 63, and each number maps to one of the 64 characters. For example, the three letters Man become the four characters TWFu. When the data does not divide evenly into groups of three, padding fills the gap: the single letter M encodes as TQ==.
The process is completely reversible. Anyone with the Base64 text can decode it back to the exact original bytes, with nothing lost and no key required.
The size cost: about a third larger
Because every 3 bytes become 4 characters, Base64 text is always about 33 percent larger than the file it represents (4 ÷ 3 ≈ 1.33). A data URI adds a short header on top of that.
| Original image | Approximate Base64 size | Reasonable to inline? |
|---|---|---|
| 1.5 KB icon | 2 KB | Yes, often |
| 6 KB logo | 8 KB | Possibly |
| 150 KB photo | 200 KB | Usually not |
| 3 MB photo | 4 MB | No |
When Base64 is wrapped into lines of 76 characters, as it is inside email messages, the line breaks push the overhead to roughly 37 percent. On websites, servers that compress text with gzip or Brotli can win back part of the overhead, but not all of it, and the image itself is not made any smaller.
What a data URI looks like
A data URI is Base64 text with a short header that tells the browser what kind of file it is. Its general form is data:[media type][;base64],[data]. For a PNG it starts like this:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUg...
You can use a data URI anywhere a URL to an image is expected. In HTML:
<img src="data:image/png;base64,iVBORw0KGgo..." alt="Company logo">
And in CSS:
.icon { background-image: url("data:image/png;base64,iVBORw0KGgo..."); }
The media type must match the real file: image/jpeg for JPG, image/png, image/gif, image/webp or image/svg+xml for SVG. If it is wrong, some browsers will not display the image.
Data URI or raw Base64?
Raw Base64 is the encoded characters without the data:...;base64, header. APIs, databases and JSON fields that ask for "Base64-encoded image data" usually want the raw form, with the file type sent in a separate field. Read the documentation of the system you are sending to: a header where none is expected is a common reason for "invalid image" errors.
When data URIs are a good idea
- Tiny icons and logos. For images of a few kilobytes, putting them inside the HTML or CSS saves the browser a separate request. A common rule of thumb is to keep data URIs under about 10 KB.
- Self-contained files. A single HTML report, an offline help page or an exported dashboard that must work without any other files is a natural fit, even if it gets a little heavier.
- CSS backgrounds and small patterns. Small decorative images can live inside the stylesheet, so they arrive together with the styles that use them.
- APIs and JSON. Many services accept images as Base64 inside a JSON body, because JSON cannot carry raw binary.
- Prototypes and test data. When you just need an image to appear in a demo, inlining avoids hosting a file somewhere.
When to avoid them
- Large photos. You pay the one-third size penalty, and the text bloats your HTML or CSS, making it slower to download and awkward to edit.
- Images used on many pages. A normal image file is downloaded once and cached by the browser, then reused on every page. A data URI inside an HTML page is downloaded again with that page every time the page itself is not cached, and it cannot be shared between pages.
- Images in stylesheets that block rendering. Browsers usually wait for CSS before showing the page. Large data URIs in a stylesheet make that wait longer, and any small change to the stylesheet means downloading every embedded image again.
- Email messages. Base64 is how email attachments are transported behind the scenes, but data URIs written into the HTML of an email are blocked or ignored by many popular email programs. Use attached or hosted images instead, and test with the email programs your readers use.
Modern web servers can send many files over a single connection, which has made the "fewer requests" benefit smaller than it used to be. For most websites, normal image files remain the default, and data URIs are an occasional tool for very small images.
Step by step: converting an image with Preslo
- If the image is large, shrink it first with the Image Resizer or the Image Compressor. Every kilobyte saved before encoding saves about a third more in the Base64 text.
- Open Image to Base64 and drop the image onto the box, or click to choose it. JPG, PNG, GIF, WebP, SVG, ICO, BMP and other formats your browser recognizes are accepted.
- Check the preview, the file type and the length of the encoded text.
- Pick the output you need: Copy data URI, Copy raw Base64, Copy CSS background-image or Copy HTML <img> tag. A message confirms when it is on your clipboard.
- For long results, use Download .txt to save the data URI as a text file instead.
The tool warns you when an image is over 5 MB. Above 20 MB the text is not shown in the box, because displaying millions of characters can freeze the page, but you can still copy or download it. Files over 100 MB are refused. The file is encoded in your browser and never uploaded, which is useful for unpublished designs or private images.
Security: encoding is not encryption
Base64 looks scrambled, which leads some people to treat it as a way to hide data. It is not. Anyone can decode it with a single command or a free website, so never use it to protect passwords, personal photos or confidential documents. If something must be kept secret, use real encryption.
A few other points are worth knowing if you build websites:
- Content Security Policy. If your site uses a Content Security Policy, data URI images only load when the
img-srcrule allowsdata:. A strict policy that leaves it out is a common reason inline images silently fail. - Untrusted input. Be careful about accepting data URIs from users. SVG files can contain scripts and links, so treat uploaded SVG data with the same caution as any other user-supplied content.
- Size limits. Applications and APIs often limit how large a field or request can be. A Base64 image that worked in testing may be rejected once a larger photo is used.
Troubleshooting
- The image does not appear. Check that the data URI is complete, with no line breaks or missing characters at the end, and that the media type matches the file.
- The API says the image is invalid. It probably expects raw Base64 without the
data:header, or the other way round. - The page became slow. One or more embedded images are too large. Replace them with normal image files.
- An SVG is bigger than expected. SVG is already text, so it can often be embedded as URL-encoded SVG code, which is usually smaller than its Base64 version.
Key takeaways
- Base64 writes binary data with 64 safe text characters, turning every 3 bytes into 4 characters.
- The result is about 33 percent larger than the original file, before any header.
- A data URI adds a header such as
data:image/png;base64,and works wherever an image URL is expected. - Use data URIs for tiny icons, self-contained files, CSS details and APIs; avoid them for large photos, images shared across pages and email.
- Shrink images before encoding them.
- Base64 is an encoding, not encryption: anyone can decode it.
