What Is a Data URI?
A data URI embeds file content directly in a document using the format data:[mediatype][;base64],<data>. Defined in RFC 2397, data URIs eliminate HTTP requests by inlining file data as text. They support any MIME type including images, fonts, PDFs, and SVGs.
When a browser encounters a data URI, it decodes the embedded content and renders it as if it were loaded from an external file. The data URI scheme was published in 1998 and is now supported by every modern browser. Unlike a standard URL that references a resource on a remote server, a data URI contains the resource itself. This makes data URIs self-contained: the document carrying a data URI has no external dependency for that resource.
Data URIs are commonly used to inline small images, icons, and fonts in HTML and CSS. They reduce the total number of HTTP requests a page makes, which can improve load time for pages with many small assets. For a broader overview of the encoding that powers data URIs, see the What is Base64 guide.
What Is the Data URI Syntax?
A data URI consists of 4 components: the data: scheme, an optional MIME type, an optional ;base64 encoding flag, and the encoded content after a comma. Each component serves a specific purpose in telling the browser how to interpret the embedded data.
| Component | Description | Example |
|---|---|---|
data: | URI scheme (required prefix) | data: |
| mediatype | MIME type of the content | image/png, text/html |
;base64 | Encoding flag (required for binary data) | ;base64 |
,<data> | The encoded content | ,iVBORw0KGgo... |
A complete data URI for a PNG image looks like this:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA...
If the MIME type is omitted, it defaults to text/plain;charset=US-ASCII. If the ;base64 flag is omitted, the data portion uses percent-encoding (URL encoding) instead. Text content like HTML or SVG can use either encoding method. Binary content like PNG, JPEG, and WOFF fonts requires Base64 encoding because percent-encoding would produce significantly longer strings.
How Do You Use Data URIs in HTML?
Data URIs can replace any URL in HTML attributes that accept a URI. The 3 most common HTML uses are <img> tags, <a> download links, and <iframe> elements. Each embeds content directly in the markup without referencing an external file.
Image tag: Replace the src attribute with a data URI to embed an image inline.
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUh..." alt="Inline icon" width="32" height="32">
Anchor tag: Create a downloadable file link without a server-side file.
<a href="data:text/plain;base64,SGVsbG8gV29ybGQ=" download="hello.txt">Download</a>
Iframe: Embed an HTML document directly inside a page.
<iframe src="data:text/html;base64,PCFET0NUWVBFIGh0bWw+..."></iframe>
The Base64 Embed Code Generator produces ready-to-use HTML snippets with the correct data URI syntax for any uploaded image. You can also encode images to Base64 first, then construct the data URI manually.
How Do You Use Data URIs in CSS?
CSS accepts data URIs anywhere the url() function is used. The 3 primary CSS use cases are background-image, @font-face declarations, and cursor properties. Embedding in CSS eliminates HTTP requests for small assets that would otherwise require separate files.
Background image: Embed a small icon or pattern directly in a stylesheet.
.icon-check {
background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0i...');
background-size: 16px 16px;
width: 16px;
height: 16px;
}
Font face: Embed a WOFF2 font file to avoid a separate font request.
@font-face {
font-family: 'CustomFont';
src: url('data:font/woff2;base64,d09GMgABAAAAA...') format('woff2');
}
Custom cursor: Set a custom cursor image without an external file.
.custom-cursor {
cursor: url('data:image/png;base64,iVBORw0KGgo...'), auto;
}
For generating CSS-specific Base64 code, use the Base64 CSS Background Generator. This tool outputs the complete CSS rule with the data URI already formatted. To calculate the size impact before embedding, use the Base64 Size Calculator.
What Are the Browser Size Limits for Data URIs?
Browser support for data URIs is universal in modern browsers, but maximum size limits vary. Chrome and Edge impose a 2 MB limit on data URIs used in image elements. Firefox and Safari have no documented hard limit. Internet Explorer 11 restricts data URIs to 4 KB in certain contexts.
| Browser | Maximum Data URI Size |
|---|---|
| Chrome | 2 MB (images), no limit (other) |
| Firefox | No hard limit |
| Safari | No hard limit |
| Edge | 2 MB (images) |
| IE 11 | 4 KB |
These limits apply to the total length of the data URI string, including the data: prefix, MIME type, and encoded content. A 1 MB binary file produces approximately 1.33 MB of Base64 text, so the effective binary file limit in Chrome is roughly 1.5 MB. In practice, data URIs are most beneficial for files under 10 KB where the saved HTTP request outweighs the 33% size overhead.
To check the encoded size of your file before creating a data URI, use the Base64 Size Calculator. For files that exceed browser limits, serve them as external binary files instead. See Base64 vs Binary for guidance on choosing between inline and external delivery.
When Should You Use Data URIs Instead of External Files?
Data URIs reduce HTTP requests at the cost of increased document size and lost cacheability. The decision depends on file size, reuse frequency, and delivery context. Files under 10 KB that appear on a single page are the strongest candidates for data URI embedding.
Use data URIs for:
- Images under 10 KB (icons, small logos, UI elements)
- Single-use decorative images that appear on one page
- HTML email templates where external image loading is blocked
- Reducing HTTP/1.1 request overhead on pages with many small assets
- Self-contained single-file HTML documents
- CSS sprites replaced by individual inline icons
Avoid data URIs for:
- Images over 10 KB where the 33% size overhead exceeds the HTTP request cost
- Assets reused across multiple pages (external files cache once, data URIs re-download with each page)
- Frequently updated resources where cache invalidation matters
- Assets that benefit from CDN distribution
- Lazy-loaded images that should not increase initial page weight
- Responsive images that need
srcsetfor different viewport sizes
For a detailed comparison of performance tradeoffs, read the Base64 vs Binary comparison. To convert images for embedding, use the Base64 Image Encoder.
What MIME Types Work with Data URIs?
Data URIs support any valid MIME type registered with IANA. The MIME type tells the browser how to interpret and render the encoded content. The table below lists the MIME types most frequently used in data URIs for web development.
| MIME Type | File Format | Common Use |
|---|---|---|
image/png | PNG | Icons, screenshots, graphics with transparency |
image/jpeg | JPEG | Photographs, thumbnails |
image/svg+xml | SVG | Vector icons, logos, illustrations |
image/webp | WebP | Modern image format with smaller file sizes |
image/gif | GIF | Simple animations, small graphics |
font/woff2 | WOFF2 | Web fonts embedded in CSS |
font/woff | WOFF | Web fonts (legacy format) |
application/pdf | Embedded documents in iframes | |
text/html | HTML | Iframe content, email templates |
text/css | CSS | Inline stylesheets |
text/plain | Plain text | Default MIME type when none specified |
application/json | JSON | Configuration data, API payloads |
Using the correct MIME type is critical. If the MIME type does not match the actual content, browsers may refuse to render the data URI or display it incorrectly. For example, encoding a JPEG file but specifying image/png as the MIME type can cause rendering failures in strict parsers. The Base64 Image Encoder automatically detects and includes the correct MIME type. For more details on MIME type handling, see Base64 MIME Types.
Frequently Asked Questions
What is the difference between a data URI and a URL?
A URL (Uniform Resource Locator) references an external resource located on a server. The browser sends an HTTP request to fetch the resource. A data URI embeds the resource content directly inline using the data: scheme defined in RFC 2397. No HTTP request is needed because the data is already present in the document. URLs are cacheable independently by the browser; data URIs are cached only as part of the HTML or CSS file that contains them.
Do data URIs work in all browsers?
Data URIs are supported in all modern browsers: Chrome, Firefox, Safari, Edge, and Opera. Internet Explorer added data URI support in version 8 with a 32 KB size limit, which was increased in later versions. IE 11 supports data URIs up to 4 KB in certain security contexts. All browsers released after 2015 support data URIs with no practical size constraints for typical web use. For current size limits by browser, see the browser limits table above.
Can data URIs contain non-Base64 data?
Yes. When the ;base64 flag is omitted, the content portion uses percent-encoding (URL encoding) instead of Base64. For example, data:text/html,%3Ch1%3EHello%3C%2Fh1%3E embeds an HTML heading using percent-encoded characters. This approach avoids the 33% size overhead of Base64 and works well for text-based content like HTML, CSS, SVG, and plain text. Binary formats like PNG, JPEG, and font files require Base64 encoding because percent-encoding binary data produces significantly longer strings.
Do data URIs affect page performance?
Data URIs have both positive and negative performance effects. On the positive side, each data URI eliminates 1 HTTP request, saving 50-200 ms of round-trip latency per asset. On the negative side, Base64 encoding increases the data size by 33%, the embedded content cannot be cached separately from the page, and large data URIs increase the initial HTML payload that blocks rendering. For a 5 KB icon, the 1.65 KB Base64 overhead is smaller than the cost of an HTTP request. For a 100 KB photo, the 33 KB overhead far exceeds the request cost. The Base64 Size Calculator helps determine the tradeoff for specific files.
How do I create a data URI from an image?
The fastest method is to use the Base64 Image Encoder on this site: upload an image, and the tool outputs the complete data URI with the correct MIME type prefix. Programmatically, the JavaScript FileReader API provides a readAsDataURL() method that reads a file and returns a complete data URI. On the command line, encode the file with base64 (Linux/macOS) or certutil -encode (Windows), then prepend data:[MIME type];base64, to the output. The Embed Code Generator produces ready-to-paste HTML and CSS snippets from any image.