Direct answer: To convert image to Base64 for HTML, turn the image file into a text string and paste it as a data URI — data:image/png;base64,.... The image then lives inside your HTML or CSS file, with no separate file and no extra download. It works best for small logos and icons under a few KB. Large photos should stay as files, because Base64 makes an image about 33% bigger.
What Is an Image to Base64 Conversion?
Converting an image to Base64 means turning the image's bytes into plain text characters — the letters A–Z and a–z, the digits 0–9, plus (+) and slash (/). The image can then travel inside text: an HTML file, a CSS file, a JSON payload or an API request. The result is called a data URI, and it looks like this:
data:image/png;base64,iVBORw0KGgo...
The part before the comma names the image format; everything after the comma is the image itself, encoded as text. Base64 is an encoding, not encryption — anyone who gets the string can decode the original image back, so never use it to hide a private image.
The prefix must match the real format: image/png for a PNG, image/jpeg for a JPG, image/gif, image/webp or image/svg+xml. A wrong prefix gives a broken image. You can even read the format from the first characters: every PNG's Base64 starts with iVBORw0KGgo, and every JPEG starts with /9j/. Those are the file signatures, encoded.
How to Convert Image to Base64 for HTML (3 Free Ways)
1. Use a free online tool — nothing is uploaded. Open a free in-browser converter such as image2base64.com or pixtools.app, drop your image on the page, and copy the result. The conversion runs in your browser with the FileReader API, so the file never leaves your device. Choose "Data URL" output for HTML or CSS, and "Base64 only" if an API wants just the string without the prefix.
2. Use JavaScript in the browser. The built-in FileReader.readAsDataURL() method does the whole job in a few lines:
const reader = new FileReader(); reader.onload = () => console.log(reader.result); // "data:image/png;base64,..." reader.readAsDataURL(file);
3. Use Python or Node.js. For a saved image file, one short script is enough:
import base64
with open("logo.png", "rb") as f:
uri = "data:image/png;base64," + base64.b64encode(f.read()).decode()
In Node.js the same step is readFileSync("logo.png").toString("base64"), with the data:image/png;base64, prefix added in front.
One honest note: our own free Base64 Encoder encodes and decodes text in your browser. Use it to check a short string or decode a data URI back to text. For image files, use one of the three methods above.
![]() |
| Our Base64 Encoder works on text — the same Base64 alphabet that images use. "Hello, World!" becomes SGVsbG8sIFdvcmxkIQ==. |
How to Use a Base64 Image in HTML and CSS
HTML image: paste the data URI as the src of an <img> tag:
<img src="data:image/png;base64,iVBORw0KGgo..." alt="Logo" width="32" height="32">
CSS background: the same string works inside url():
background-image: url("data:image/png;base64,...");
Favicon, with no file at all:
<link rel="icon" href="data:image/png;base64,...">
SVG is different. An SVG file is already text, so URL-encode it instead of Base64 — the result is shorter, and you can still read and edit it. See our Base64 vs URL encoding guide for the exact difference.
How Big Does Base64 Make Your Image?
Base64 turns every 3 bytes of the file into 4 characters of text, so the text is about 33% bigger than the file — plus about 22 characters for the data:image/png;base64, prefix. The formula is simple:
Base64 characters = 4 × ceil(file bytes ÷ 3)
| Image | File size | Base64 text |
|---|---|---|
| 16×16 icon PNG | 112 bytes | 152 characters |
| Small logo | 3 KB | about 4 KB |
| Blog photo | 200 KB | about 266 KB |
Worked example: a 3 KB logo is 3,072 bytes. Divide by 3 to get 1,024 groups, times 4 characters per group, and you get 4,096 characters — about 4 KB of text sitting inside your HTML. There is one more cost: the encoded image cannot be cached on its own. It downloads again with every page that includes it, while a separate image file is cached once and reused.
When to Inline an Image — and When Not To
| Inline as Base64 | Keep as a separate file |
|---|---|
| Small logos and icons (a few KB) | Large photos |
| HTML emails, where attachments get blocked | Images used on many pages |
| Single-file HTML pages | Images that change often |
| Favicons | Anything over about 10 KB |
A Blogger note: in a Blogger post, a data URI works in the HTML view of a post or in your theme — but keep it small. A very long string makes the editor slow and the post heavy, and it counts toward your post size.
Common Mistakes
1. Wrong MIME type. An image/png prefix on JPEG bytes shows a broken image. Check the first characters: iVBORw0KGgo means PNG, /9j/ means JPEG.
2. Double encoding. Encoding a string that is already Base64 gives garbage. Paste it into a decoder to check — our Base64 to Image Decoder guide shows the reverse step.
3. Encoding a big photo. A 200 KB photo becomes about 266 KB of text inside your HTML, and it loads again on every visit.
4. Using Base64 for SVG. URL-encode SVG instead. It stays readable and is usually shorter.
5. Pasting a data URI where it gets stripped. Some email clients and Markdown renderers block data: images, so test where the text will actually be shown.
Frequently Asked Questions
How do I convert image to Base64 for HTML?
Drop the image into a free in-browser converter, copy the data URI it gives you, and paste it as the src of an img tag. The conversion runs on your own device, so nothing is uploaded. You can also do it in code with JavaScript's FileReader.readAsDataURL() or Python's base64 module.
What is the data URI format for a PNG image?
data:image/png;base64, followed by the encoded text. The prefix names the format — image/png for PNG, image/jpeg for JPG — and everything after the comma is the image itself.
How much bigger does Base64 make an image?
About 33% bigger: every 3 bytes of the file become 4 characters of text. A 3 KB logo becomes about 4 KB of text, and a 200 KB photo becomes about 266 KB.
Can I use a Base64 image in CSS?
Yes. Use it as a background like this: background-image: url("data:image/png;base64,..."); It works the same way as in an img tag.
Should I convert a large photo to Base64?
No. A large photo becomes a very long string that bloats your HTML and cannot be cached separately from the page. Keep photos as files and inline only small icons and logos.
Is converting an image to Base64 safe — does it upload my image?
In-browser converters read the file on your own device and never upload it. But Base64 is an encoding, not encryption: anyone with the string can decode the original image, so never use it to hide a private image.
Try It Now
Shrink the image first with our Image to WebP Converter — a smaller file means a shorter Base64 string. Then check any Base64 string with our free Base64 Encoder, or turn a data URI back into a picture with our Base64 to Image Decoder guide.
