← Back to Blog

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

April 13, 2026 3 min read By CodeTidy Team

The Surprising Truth About Image Loading Times

Have you ever wondered why your website's images take ages to load, despite being optimized and compressed? The culprit might be the number of HTTP requests required to load each image. One way to reduce these requests is by using Base64 Data URIs, which allow us to embed images, fonts, and files directly into our HTML and CSS.

Table of Contents

  • What are Base64 Data URIs?
  • How to Create a Base64 Data URI
  • Embedding Images with Base64 Data URIs
  • Embedding Fonts and Files with Base64 Data URIs
  • Performance Considerations: When to Inline and When to Externalize
  • Best Practices for Using Base64 Data URIs

What are Base64 Data URIs?

A Base64 Data URI is a way to encode binary data, such as images, fonts, and files, into a text format that can be used in HTML and CSS. This allows us to reduce the number of HTTP requests required to load a webpage, which can improve page load times. A Base64 Data URI typically consists of the following format:

data:[<mediatype>][;charset=<charset>][;base64],<encoded data>

For example:

data:image/png;base64,iVBORw0KGg... // truncated for brevity

How to Create a Base64 Data URI

There are several ways to create a Base64 Data URI, including online tools and command-line utilities. One popular method is to use the base64 command in the terminal:

base64 -w 0 image.png > image.base64

This will output the Base64-encoded image data, which can then be copied and pasted into your HTML or CSS.

Embedding Images with Base64 Data URIs

To embed an image using a Base64 Data URI, simply use the src attribute in your HTML:

<img src="data:image/png;base64,iVBORw0KGg... // truncated for brevity" alt="Embedded Image">

Alternatively, you can use the background-image property in CSS:

.element {
  background-image: url(data:image/png;base64,iVBORw0KGg... // truncated for brevity);
}

Embedding Fonts and Files with Base64 Data URIs

In addition to images, we can also embed fonts and files using Base64 Data URIs. For example, to embed a font, we can use the @font-face rule in CSS:

@font-face {
  font-family: 'Embedded Font';
  src: url(data:font/truetype;base64,...) format('truetype');
}

To embed a file, such as a PDF or Word document, we can use the object element in HTML:

<object data="data:application/pdf;base64,..." type="application/pdf" width="100%" height="500"></object>

Performance Considerations: When to Inline and When to Externalize

While Base64 Data URIs can improve page load times by reducing the number of HTTP requests, they can also increase the size of your HTML and CSS files. As a general rule, we recommend inlining files that are smaller than 2-5KB, as the overhead of the HTTP request is likely to outweigh the benefits of inlining. For larger files, it's often better to externalize them and use a separate HTTP request.

Best Practices for Using Base64 Data URIs

  • Use a consistent naming convention for your Base64 Data URIs to avoid confusion.
  • Keep your Base64 Data URIs organized by type (e.g., images, fonts, files).
  • Use a tool or utility to generate your Base64 Data URIs, rather than doing it manually.
  • Test your Base64 Data URIs in different browsers and devices to ensure compatibility.

Key Takeaways

  • Base64 Data URIs can improve page load times by reducing the number of HTTP requests.
  • Use the data attribute in HTML or the url function in CSS to embed Base64 Data URIs.
  • Inline files smaller than 2-5KB, and externalize larger files.
  • Use a consistent naming convention and keep your Base64 Data URIs organized.

FAQ

Q: What is the maximum size limit for Base64 Data URIs?

A: There is no official size limit for Base64 Data URIs, but we recommend keeping them under 2-5KB to avoid increasing the size of your HTML and CSS files.

Q: Can I use Base64 Data URIs for videos and audio files?

A: Yes, you can use Base64 Data URIs for videos and audio files, but be aware that this can significantly increase the size of your HTML and CSS files.

Q: Are Base64 Data URIs compatible with all browsers?

A: Most modern browsers support Base64 Data URIs, but there may be some issues with older browsers or devices. Be sure to test your Base64 Data URIs in different environments to ensure compatibility.

AI agent tools available. The CodeTidy MCP Server gives Claude, Cursor, and other AI agents access to 60+ developer tools. One command: npx @codetidy/mcp