Base64 Data URIs Explained: Embedding Images and Fonts in CSS/HTML

A data URI lets you embed a file's actual bytes directly inside an HTML or CSS document, as text, instead of pointing to a separate file with a normal URL. Base64 is what makes that possible for binary files like images and fonts.

The anatomy of a data URI

A data URI has the form data:[mime-type];base64,[encoded-data]. For example, a tiny red-dot PNG becomes:

HTML
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB..." />

The browser decodes the Base64 portion back into raw image bytes and renders it exactly as if it had been loaded from a normal file — no network request required.

Using data URIs in CSS

The same technique works anywhere CSS accepts a URL:

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

@font-face {
  font-family: "MyIconFont";
  src: url("data:font/woff2;base64,PASTE_BASE64_HERE") format("woff2");
}

This is especially common for small SVG icons — see the SVG to Base64 tool to generate one, or the image to Base64 tool for raster images like PNG and JPEG.

The tradeoff: fewer requests, bigger payload

Inlining an asset removes a separate HTTP request, which can help for small, frequently-reused images (like a logo repeated across many pages) — especially over HTTP/1.1 where each request has overhead. But Base64 encoding adds about a 33% size overhead, and an inlined asset can no longer be cached independently of the document it lives in. For large images, or assets shared across many pages that benefit from long-term browser caching, a normal linked file is usually the better choice.

Try it

Use the Image to Base64 tool to generate a data URI from any image file, or the general Base64 converter for any other text or file content.

Base64 Data URIs — FAQ

What is a Base64 data URI?+

A data URI embeds a file directly inside HTML or CSS as text, in the form data:[mime-type];base64,[encoded-data], instead of linking to a separate file with a URL.

When should I use a data URI instead of a normal image file?+

For small, frequently-reused assets (icons, tiny logos, inline fonts) where saving an extra HTTP request outweighs the ~33% size increase from Base64 encoding. For large or rarely-changed images, a normal linked file with caching is usually better.

Do data URIs work in CSS as well as HTML?+

Yes — anywhere a URL is accepted, including background-image, list-style-image, @font-face src, and the src attribute of <img>, <video> or <script>.

Can browsers cache data URIs?+

Not independently — a data URI is part of the HTML/CSS document itself, so it is cached (or not) along with that document, not as its own cacheable resource the way a linked file is.