Free tool
Image to Base64 converter
Drop, pick or paste an image to get its Base64 data URI and ready-to-paste HTML, CSS, Markdown or JSON. Shrink it to WebP or JPEG first if you like, or paste Base64 to get the image back.
Free · no sign-up · runs in your browser, nothing is uploaded
Add an image to get its Base64 data URI and code.
What a Base64 image data URI looks like
A data URI (now officially a data URL) carries the file itself instead of pointing to it. The format comes from RFC 2397:
data:[<media type>][;base64],<data> data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA… data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'%3E…
The media type tells the browser how to read the bytes, so it must match the real format. Every Base64 PNG starts with iVBORw0KGgo and every JPEG with /9j/, because those are the file signatures encoded. This tool reads the signature instead of trusting the file name.
| Format | Media type | Base64 starts with |
|---|---|---|
| PNG | image/png | iVBORw0KGgo |
| JPEG | image/jpeg | /9j/ |
| GIF | image/gif | R0lGOD |
| WebP | image/webp | UklGR |
| SVG | image/svg+xml | PHN2Zy or PD94bWwg |
| ICO | image/x-icon | AAABAA |
Using a Base64 image in HTML, CSS, Markdown and JSON
<!-- HTML -->
<img src="data:image/png;base64,iVBORw0KGgo…" width="32" height="32" alt="Logo">
/* CSS */
.logo { background-image: url("data:image/png;base64,iVBORw0KGgo…"); }
<!-- Favicon, no extra request -->
<link rel="icon" type="image/png" href="data:image/png;base64,iVBORw0KGgo…">
<!-- Markdown -->

// JSON (APIs that take files as text)
{ "name": "logo.png", "mime": "image/png", "data": "iVBORw0KGgo…" }Some Markdown renderers and many email clients strip or block data: images, so test where the text will be shown. Browsers also refuse to open a data: URL typed or linked as a top-level page, so use them inside a page, not as a link target.
When to inline an image, and when not to
| Inline as Base64 | Keep as a separate file |
|---|---|
| Small icons, logos and loading placeholders | Photos, screenshots and anything large |
| Single-file HTML exports, emails you build as one file, test fixtures | Images reused on many pages (a file is cached once; an inlined copy is downloaded with every page or stylesheet) |
| JSON APIs that only accept text | Images that change on their own schedule (any change re-downloads the whole CSS or HTML) |
An inlined image saves one request but costs about a third more bytes, and it can only be cached together with the file it sits in. If the image is big, shrink it first: choose WebP and a max width above and compare the sizes in the stats.
SVG: URL-encode instead of Base64
SVG is text, so it does not need Base64 at all. A URL-encoded SVG data URI is usually shorter, and you can still read and edit it. Only a few characters must be escaped: # (it would start a URL fragment, so fill="#f00" must become %23f00), %, and < > for safety. Double quotes are switched to single quotes so the result fits inside url("…").
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3E%3Ccircle cx='4' cy='4' r='4' fill='%23f97316'/%3E%3C/svg%3E");Turn on "URL-encoded SVG" above to get this form. To shrink the SVG itself first, run it through the SVG optimizer; to turn it into a PNG, use SVG to PNG.
Convert an image to Base64 in code
Browser JavaScript: FileReader.readAsDataURL() gives the full data URI for a file from an <input type="file">, and a canvas can re-encode an image.
const reader = new FileReader();
reader.onload = () => console.log(reader.result); // "data:image/png;base64,…"
reader.readAsDataURL(input.files[0]);
canvas.toDataURL('image/webp', 0.8); // a canvas as a WebP data URINode.js
import { readFileSync, writeFileSync } from 'node:fs';
const uri = 'data:image/png;base64,' + readFileSync('logo.png').toString('base64');
writeFileSync('out.png', Buffer.from(b64, 'base64')); // Base64 → imagePython
import base64
with open('logo.png', 'rb') as f:
uri = 'data:image/png;base64,' + base64.b64encode(f.read()).decode()
with open('out.png', 'wb') as f: # Base64 → image
f.write(base64.b64decode(b64))Terminal
base64 -w 0 logo.png # GNU/Linux, one line base64 -i logo.png # macOS base64 -d logo.b64 > logo.png # Base64 → image
For text and files of any other type, use the Base64 decoder and encoder. To change an image's format without Base64, try the image converter. More on DevHunt: image tools and design tools.
More free developer tools
FAQ
How do I convert an image to Base64?+
Drop the image on the box above, click to pick it, or paste it with Ctrl+V (Cmd+V on a Mac). You get the data URI at once, and the tabs give you the same image as an <img> tag, a CSS background, Markdown or JSON. The file is read in your browser and never uploaded.
How do I use a Base64 image in HTML?+
Put the data URI in the src attribute: <img src="data:image/png;base64,iVBORw0KGgo…" alt="…">. Pick the HTML <img> tab above to get the tag with the width and height filled in, which stops the layout from jumping while the page loads.
How do I use a Base64 image as a CSS background?+
Use the data URI inside url(): background-image: url("data:image/png;base64,…");. Quote it. For SVG icons, the URL-encoded form (turn on "URL-encoded SVG") is usually shorter than Base64 and still works in url().
Does converting an image to Base64 make it bigger?+
Yes. Base64 writes every 3 bytes as 4 characters, so the data URI is about 33% larger than the image file. To win some of it back, choose WebP or JPEG output and a max width before copying; the stats show the new size against the original.
How do I convert Base64 back to an image?+
Switch to "Base64 → Image" and paste the string, with or without the data:image/…;base64, prefix. The tool reads the first bytes to find the real format (PNG, JPEG, GIF, WebP, AVIF, SVG, ICO, BMP), shows a preview with the size in pixels, and downloads it with the right extension.
Is there a size limit for Base64 images?+
Browsers allow very long data URLs: MDN lists 512 MB for Chromium and Firefox and 2048 MB for Safari. The practical limit is much lower. A big inlined image makes the HTML or CSS file large, and it cannot be cached on its own, so keep inlining for small icons and placeholders.
Why does my Base64 image not show?+
The usual causes are a missing or wrong prefix (it must be data:image/png;base64, and so on), line breaks or spaces copied into the string, a URL-encoded string (%2B instead of +), or a cut-off string. Paste it into "Base64 → Image" here: it ignores whitespace, reads the real format from the bytes and says what is wrong.
Sources: RFC 2397 (data URL scheme) · MDN: data: URLs · MDN: FileReader.readAsDataURL() · MDN: HTMLCanvasElement.toBlob() · RFC 4648 (Base64) · WHATWG MIME Sniffing · Python base64. Checked 2026-10-29.