Image to Base64: Embed Images Directly in Code (Free Tool)
Image to Base64: Embed Images Directly in Code (Free Tool)
Converting an image to Base64 turns a picture file into a long line of plain text. That text can then live inside an HTML file, a stylesheet, a JSON payload or an email template, with no separate image file and no extra download. It is a neat trick, and it is also one of the most misused tricks in web development. This guide explains what image to Base64 conversion actually does, how to do it in seconds, and the size limits that decide whether inlining an image helps your page or quietly makes it slower.
Key takeaways
- Base64 turns every 3 bytes of an image into 4 text characters, so the encoded string is about 33% larger than the original file.
- A data URI wraps that string so a browser can use it anywhere a URL goes:
data:image/png;base64,.... - Inline only tiny images. A widely used rule of thumb is under about 4 KB; above 10 KB, serve the image as a normal file.
- Inlined images cannot be cached separately. Change one word in your page and the browser re-downloads every embedded image with it.
- For SVG, a URL-encoded data URI is usually shorter than Base64, because SVG is already text.
- Always shrink the image before encoding it. A smaller file means a shorter string.
What Is Base64 Encoding?
Base64 is a way of representing binary data as plain text. It uses 64 safe characters: the upper-case letters A to Z, the lower-case letters a to z, the digits 0 to 9, plus + and /. The algorithm, defined in RFC 4648, works in groups: every 3 bytes of the original file (24 bits) are split into four 6-bit groups, and each group becomes one character. If the file length is not divisible by three, the output is padded with = characters at the end.
The arithmetic is fixed, so the result is predictable. The encoded text is always 4 x ceil(n / 3) characters long, where n is the number of bytes in the file. For any file of a reasonable size, that works out to a size increase of almost exactly one third. A 3 KB icon becomes about 4 KB of text. A 300 KB photo becomes about 400 KB of text. Nothing about the image itself changes: the pixels, the colours and the quality are identical. Only the packaging changes, from binary to text.
Why would anyone want a picture as text? Because there are places where only text is allowed. An HTML file can hold text. A CSS stylesheet can hold text. A JSON object, an XML file, an email body and an environment variable can all hold text. None of them can hold a raw binary image file. Base64 is the bridge: it lets an image travel through text-only channels without being corrupted.
It is worth saying plainly what Base64 does not do. It does not compress anything. It does not make an image smaller, faster or better quality. It makes the image about a third bigger, in exchange for letting it live somewhere a file cannot go. That trade is worth making for a small icon. It is a bad trade for a photograph.

What Is a Data URI?
A Base64 string on its own is just text. To make a browser treat it as an image, you wrap it in a data URI, a small addressing scheme defined in RFC 2397. The format is:
data:[mediatype][;base64],<data>
For a PNG image, that looks like this:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...
The prefix tells the browser three things: this is an image, it is a PNG, and the data that follows is Base64-encoded. With that prefix in place, the string can be used anywhere a URL is expected. In HTML, it goes straight into an img tag:
<img src="data:image/png;base64,iVBORw0KGgo..." alt="Company logo" width="64" height="64">
In CSS, it becomes a background image:
background-image: url("data:image/png;base64,iVBORw0KGgo...");
The browser decodes the string and renders the image directly from the document. There is no network request, no separate file to fetch, and no chance of a broken image link, because the image is inside the page itself. That is the whole appeal, and it is real. It is also where people stop thinking, which is where the problems start.
How to Convert an Image to Base64
The fastest way is a browser-based converter. The file is read on your own device, encoded locally, and nothing is uploaded to a server, which matters if the image is an unpublished design, a client logo or anything confidential. The steps are the same in every good converter:
- Drop the image in. Drag a PNG, JPG, WebP, GIF, SVG, AVIF, BMP, ICO or TIFF file onto the converter, or click to browse for it.
- Pick an output format. Most converters offer the raw Base64 string, the full data URI, a ready-to-paste HTML
imgtag, a CSSbackground-imagerule, and sometimes a Markdown image or a JSON field. - Copy the result. Copy the string or download it as a
.txtfile, then paste it where it needs to go.
If you would rather do it in code, the browser already has the tool built in. The FileReader.readAsDataURL() API reads a file and returns the complete data URI:
const reader = new FileReader();
reader.onload = () => {
console.log(reader.result); // data:image/png;base64,...
};
reader.readAsDataURL(file);
In Python, the same job is two lines:
import base64
data_uri = "data:image/png;base64," + base64.b64encode(open("logo.png", "rb").read()).decode("ascii")
From the command line, the base64 tool does it, with one trap worth knowing: GNU coreutils wraps its output at 76 characters by default. An unescaped newline inside a quoted CSS string makes the browser drop the whole declaration, so always pass -w 0 to keep the output on a single line:
base64 -w 0 logo.png

Where Base64 Images Actually Help
There are real situations where embedding an image as text is the right call. They share a pattern: the image is small, it is used once, and a separate file would be awkward.
Small icons and UI elements. A 16-pixel icon, a spinner, a decorative divider or a tiny logo mark can be inlined into a stylesheet so the page renders it with zero extra requests. This is the classic use case, and it is the one the technique was designed for.
Favicons. A favicon is requested on almost every page load. Inlining a tiny favicon as a data URI in a link tag removes that request entirely.
Single-file HTML. If you are sending someone a single HTML file, an email template, or a document that must work offline with no supporting files, embedded images are the only way the pictures travel with the file.
Email templates. Many email clients block remote images by default, so a logo embedded as a data URI is one of the few ways to guarantee it displays. (Some clients still strip data URIs, so test before sending.)
JSON and APIs. When an API returns data as JSON, an image has to be text to fit in the response. Base64 is the standard way to include a small image, a signature or a thumbnail inside a JSON payload.
CSS sprites and critical-path images. A small image that a page needs before it can paint, such as a logo in the header, can be inlined so it arrives with the stylesheet instead of waiting for a second round trip.
When NOT to Inline an Image
The rule of thumb used by experienced developers is simple: inline only an icon of a few KB that your own code owns and uses once. Everything else should stay a file. Here is why, in detail.
The 33% overhead is only the start. Every 3 bytes become 4 characters, so the encoded image is a third larger before anything else happens. Compression helps on the wire: in a 2026 test by a DEV Community author, 50,000 random bytes became 66,668 bytes as Base64, and gzip shrank that back to 50,508 bytes, only about 1% over the original. But that recovery only happens on a compressed response. An uncompressed stylesheet, an email body or an API response pays the full third. And even in the best case, the gzipped Base64 stayed slightly above the raw file size, so inlining at most ties on bytes. It never wins.
The size cut-offs are concrete. Vite, the build tool used by a large share of modern web projects, inlines assets under 4 KB by default and leaves everything larger as a file. That 4 KB line is a sensible personal rule too. A second guide puts the hard limit at 10 KB: above that, never inline without measuring first. A 100 KB hero image encoded as Base64 adds 33 KB to a stylesheet, blocks the parser while it is read, and breaks every HTTP cache layer you have.
Caching is the real loss. A separate image file is cached once and reused everywhere. The same logo serves every page, survives a deploy if its URL is unchanged, and can be given a long cache lifetime. An inlined image inherits the cache lifetime of the document that contains it. Change one word in your HTML and the browser re-downloads every byte of every inlined image in it. If the same logo is inlined in fourteen components, every page load re-downloads the same inflated string fourteen times over.
HTTP/2 and HTTP/3 removed the original reason. Inlining was invented when browsers opened a limited number of connections per host and each new request queued behind the last. Over HTTP/2 and HTTP/3, many requests share one connection and are multiplexed, so the queueing penalty has largely gone. What remains is the per-request overhead of headers and the browser’s own bookkeeping, which for a small file is small.
Photos almost never qualify. A photograph is large, it changes independently of the page, and it is often reused across pages. It fails all three tests: small, code-owned, used once. Serve photos as normal files, ideally as WebP or AVIF, and let the browser cache them.

Base64 vs Regular Image Files: A Quick Comparison
| Factor | Base64 data URI | Separate image file |
|---|---|---|
| File size | About 33% larger | Original size |
| Extra requests | None | One per image |
| Caching | Tied to the document | Cached once, reused everywhere |
| Best for | Icons under ~4 KB, favicons, single-file HTML, email | Photos, logos reused across pages, anything over 10 KB |
| Maintenance | A 1,600-character string nobody can review in a diff | Swap the file, keep the URL |
Shrink the Image Before You Encode It
Every byte you remove from an image before encoding removes roughly 1.33 characters from the resulting string. That makes preparation the highest-leverage step in the whole process, and it is the step most people skip.
- Compress first. Run the image through a compressor and pick a quality level where the image still looks identical. A photo saved at quality 80 instead of 100 is often half the size with no visible difference. Fileoraa’s free compress image to KB tool lets you target an exact size, which is ideal when you are trying to stay under the 4 KB inlining threshold.
- Pick the right format. A logo with flat colours compresses far better as PNG or WebP than as JPG. A photo is smaller as JPG or WebP than as PNG. Converting a PNG screenshot to WebP before encoding can cut the string by half. Fileoraa’s PNG to WebP converter and JPG to PNG converter both run in the browser, so the file never leaves your device.
- Resize to the size it will display. An icon that renders at 32 pixels does not need to be a 512-pixel source file. Resize it first and the string shrinks dramatically.
- Consider URL-encoding for SVG. SVG is already text, so a data URI does not need Base64 at all:
data:image/svg+xml,...with the special characters percent-encoded is usually shorter than the Base64 version, and it compresses better with gzip.
Image to Base64: Frequently Asked Questions
What is image to Base64 conversion?
Image to Base64 conversion turns a picture file into a long string of plain text characters. The text can be pasted into HTML, CSS, JSON or an email, where a binary file cannot go. A data URI prefix (data:image/png;base64,) tells the browser to decode the text back into the image.
Does Base64 make an image bigger?
Yes. Every 3 bytes of the original file become 4 characters of text, so the encoded string is about 33% larger than the file. Gzip compression on the server can shrink most of that overhead back on the wire, but the browser still has to download, decompress and parse the full string.
What size image should I convert to Base64?
Keep it small. A widely used rule of thumb is to inline only images under about 4 KB, such as icons and favicons. Above 10 KB, serve the image as a normal file unless you have measured a real benefit. Photos should almost never be inlined.
Can I use a Base64 image in CSS?
Yes. Paste the data URI into a background-image: url("data:image/png;base64,..."); rule. Make sure the string is on a single line: command-line tools that wrap output at 76 characters will break the declaration, so use base64 -w 0 on Linux.
Is it safe to convert images to Base64 online?
It is safe when the converter runs entirely in your browser, because the file never leaves your device. Check that the tool says your images are encoded locally and never uploaded. Avoid converters that send your files to a server, especially for client work or unpublished designs.
Does Base64 work for all image formats?
Yes. PNG, JPG, WebP, GIF (including animated GIFs), SVG, AVIF, BMP, ICO and TIFF can all be Base64-encoded. The MIME type in the data URI prefix must match the format, or the browser may refuse to render the image. For SVG, a URL-encoded data URI is usually shorter than Base64.
Try It: Shrink First, Then Encode
Converting an image to Base64 is easy. Knowing when to do it is the skill. Keep the image tiny, keep it to a single use, and shrink it before you encode it. If your image is a photo, a logo reused across pages, or anything over a few kilobytes, a normal image file will serve you better every time.
Before you encode your next image, run it through Fileoraa’s free compress image to KB tool to hit your exact size target, or convert it to a smaller format with the PNG to WebP converter. Both run entirely in your browser, with no sign-up, no upload and no watermark. Your Base64 string will be shorter, your page will be faster, and your future self will thank you for the smaller diff.
