How to Embed an Image in HTML With Base64 (Data URIs)

Normally a web page links to image files, and the browser downloads each one separately. Sometimes, though, you want an image inside the code itself – in an HTML email, a single-file report, a small icon in CSS or a quick prototype. That is where Base64 data URIs come in. Here is how to embed an image in HTML with Base64, with examples, and a clear rule of thumb for when it is a good idea.

Screenshot of the Image to Base64 Converter on Assen Tools
The Image to Base64 Converter in use. Open the Image to Base64 Converter – free, no sign-up.

What a data URI looks like

A data URI packs the image directly into the address:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...

It has three parts:

  • data: – tells the browser the data is inline
  • image/png – the file type (image/jpeg, image/svg+xml, image/webp and so on)
  • base64, followed by the image bytes encoded as text

Read what is Base64 encoding to understand how the encoding works.

Embedding in HTML

Use the data URI as the image source:

<img src="data:image/png;base64,iVBORw0KGgo..." alt="Company logo" width="120" height="40">

Always include alt text and dimensions, just as you would for a normal image.

Embedding in CSS

Small background icons can be placed straight into a stylesheet:

.icon-check {
  background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0i...");
}

For SVG icons, URL-encoded SVG is often even smaller than Base64.

Creating the code

With the free Image to Base64 converter:

  1. Upload your image.
  2. Copy the result as a ready-made data URI, an HTML <img> tag or a CSS rule.

It runs in your browser, so the image is not uploaded anywhere. Shrink the image first with the Image Compressor or Image Resizer – smaller images produce much shorter code.

When inline images help

  • Tiny icons and logos (a few kilobytes) – saves separate HTTP requests.
  • Single-file documents – HTML reports or offline pages that must work without extra files.
  • Prototypes and code snippets shared with colleagues.
  • Some email templates, although support varies by email client, so test carefully.

When they hurt performance

  • Base64 makes images about 33% larger.
  • They cannot be cached separately. A normal image is downloaded once and reused across pages; a data URI is downloaded again with every page or stylesheet that contains it.
  • Large data URIs bloat your HTML and CSS, delaying the moment the page can start displaying – bad for Core Web Vitals. See what are Core Web Vitals.

Rule of thumb: inline only very small images. For photos and anything larger than a few kilobytes, use normal image files, ideally compressed and in WebP format.

Decoding a data URI

Found a long Base64 image string in someone's code and want to see or save it? Paste the full data URI into your browser's address bar to view it, or decode the text with Base64 Encode and Decode.

Frequently asked questions

Do data URIs work in all browsers? Yes, all modern browsers support them.

Is there a size limit? Browsers handle large data URIs, but performance suffers long before any limit is reached.

Are inline images good for SEO? Search engines can see them, but normal image files with good file names and alt text are better for image search.

Can I use data URIs for favicons? Technically yes, but a normal favicon file is cached once by the browser and reused on every page, which is more efficient.

Summary

Now you know how to embed an image in HTML with Base64: convert the image to a data URI and use it in an img tag or CSS rule. Keep it for tiny icons, single-file documents and prototypes, and use normal cached image files for everything else. Create the code in one click with the free Image to Base64 converter.

Jonathan Nwachukwu, founder of Assen Tools

Written by

Jonathan Nwachukwu · Founder & developer

Jonathan is a freelance web developer and web development teacher who builds Laravel, WordPress and WooCommerce sites for clients. He built AssenTools to put the everyday file, photo and calculation jobs he kept doing for clients and students into one free, private toolbox, and he maintains every tool and guide on the site.