How do I embed an image in HTML or CSS with Base64?
One file instead of two — when a data URI is the right choice, and how to use it without slowing your page down
By Metin Ersoy · Updated November 10, 2026
What a data URI is
Normally an HTML page points to a separate image file, and the browser fetches it with a second request. A data URI puts the image itself into the page as text. It starts with a short label that says what kind of file it is, followed by the file written in Base64:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA…
Everywhere a browser accepts an image address — an img src, a CSS url(), a favicon link — it also accepts a data URI.
1. Turn the image into Base64
Open Image to Base64, choose your picture (JPG, PNG, WebP, GIF, SVG, ICO, BMP or AVIF) and pick the format you need:
| You want to… | Choose |
|---|---|
| Put the image in an HTML page | HTML img tag |
| Use it as a background in a stylesheet | CSS rule |
| Paste it into JSON, Markdown or a form field | Data URI |
| Send it to an API that asks for the bare code | Base64 code only |
Click Convert, then Copy. For long results, downloading the .txt file is safer — it is easy to miss the end of a very long line when selecting by hand.
2. Paste it where you need it
In HTML, the img tag from the tool goes straight into the page. Add a meaningful alt text between the quotes so screen readers can describe the image.
In CSS, paste the rule into the selector you want, for example a button or a header. Remember to give the element a size, since a background image does not size the element by itself.
In JSON or an API request, paste the data URI (or the bare code, if the documentation asks for it) as a string value. Check the service's size limit first; many APIs reject requests over a few megabytes.
3. Keep embedded images small
Base64 text is about 33 % bigger than the original file, and it is downloaded again every time the page or stylesheet changes. A practical rule:
- Under 10 KB — icons, small logos, simple shapes: embedding is a good idea.
- 10–100 KB — acceptable in single-file documents, offline pages and email templates.
- Over 100 KB — keep it as a normal image file on a web page.
To make an image smaller before converting, use Compress PNG or Compress JPG, or lower its pixel size with Resize image. A 2000-pixel-wide photo shown 300 pixels wide is wasting most of its size.
Common mistakes
- Cutting the text short. A missing character at the end breaks the whole image. Copy with the button or download the .txt file.
- Wrong type in the label. A JPG labelled image/png may still display in some browsers but fail in others. The converter reads the real type from the file, so its label is always right.
- Line breaks inside the code. Some editors wrap long lines and insert breaks. In HTML and CSS the data URI must stay on one line.
- Huge images in CSS. Every page that uses the stylesheet downloads the image, even if it never shows it. Keep big pictures as files.
When not to use Base64
Use normal image files for photos, for anything over about 100 KB, for images you want to appear in search results and for images that are used on many pages (a file is cached once; embedded text is repeated). Base64 is a tool for small, self-contained pieces — not a replacement for image files.
Ready to try it? It is free and your file stays on your device.
Open Image to Base64