How to Embed Images as Base64 in HTML, CSS, and Markdown
Encoding an image to a data URI and using it in an img tag, a stylesheet, an email template, or a Markdown file, with size guidance.
Primary Browser Tool
Image to Base64 Converter
Encode images as Base64 data URIs with ready-to-paste HTML, CSS, and Markdown snippets, or decode a Base64 string back into an image file.
Encode and Copy the Right Snippet
Drop an image on the Image → Base64 tab and choose the output style that matches where you will paste it. The tool wraps the encoded data in the surrounding syntax for you.
| Output style | What you get | Paste it into |
|---|---|---|
| Data URI | data:image/png;base64,… | Any place a URL is expected |
| Raw Base64 | Only the encoded characters | JSON fields, API payloads, config files |
| HTML <img> | A complete img element with alt text | HTML pages and email templates |
| CSS background | A background-image declaration | Stylesheets and style attributes |
| Markdown |  | README files and notes, where the renderer allows data URIs |
Shrink the Image First
Encoded text is a third larger than the file, and it travels inside your HTML or CSS, so every unnecessary kilobyte counts more than usual. Before encoding:
- Resize the image to the exact pixel size it will be displayed at.
- Compress it. For icons, an 8-bit PNG or an optimized SVG is often under 2 KB.
- Prefer SVG for simple shapes and logos; it is tiny and stays sharp at any size.
- Check the “As Base64” figure in the tool. If it is above about 100 KB, link to the file instead.
Where Inlining Helps and Where It Hurts
Inlining removes a network request, which is worthwhile for a handful of tiny images needed immediately, such as an icon in a button or a loading indicator. It is also the simplest way to make a single HTML file fully self-contained.
It works against you for larger or reused images. An inlined image cannot be cached on its own, so it is downloaded again with every page that includes it, and a large data URI in a stylesheet delays the rendering of the whole page. Email is a special case: several major mail clients do not display data URI images at all, so hosted images or CID attachments remain the reliable choice there.
Practical Examples
Search icon in a stylesheet
A 620-byte SVG is encoded and used as a background-image on the search field. The page saves a request and the stylesheet grows by under 1 KB.
Common Mistakes & Honest Limitations
Mistakes to Avoid
- Inlining a hero photo of several hundred kilobytes.
- Pasting the HTML snippet into a CSS file, or the reverse. Choose the output style that matches the destination.
Technical Limitations
- Some Markdown renderers and content security policies block data URIs. Test in the actual destination.