Data URIs: Embed Files Directly in HTML/CSS
Data URIs allow you to embed files directly in web pages as Base64-encoded strings, eliminating the need for separate HTTP requests. They're commonly used for small images, icons, and fonts.
Data URI Syntax
A Data URI follows this format:
data:[<mediatype>][;base64],<data>
data:— The URI schememediatype— MIME type (e.g., image/png, text/html);base64— Indicates Base64 encodingdata— The actual Base64-encoded content
Examples
Image in HTML
<!-- Embed a small PNG image -->
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==" alt="1x1 pixel" />Background Image in CSS
.icon {
background-image: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCI+PHBhdGggZD0iTTEyIDJMMiA3djEwbDEwIDVsMTAtNVY3eiIvPjwvc3ZnPg==');
width: 24px;
height: 24px;
}
Inline SVG Data URI
<!-- SVG can also be URL-encoded instead of Base64 -->
<img src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='24'%3E%3Ccircle cx='12' cy='12' r='10'/%3E%3C/svg%3E" />Common MIME Types
| File Type | MIME Type |
|---|---|
| PNG Image | image/png |
| JPEG Image | image/jpeg |
| GIF Image | image/gif |
| WebP Image | image/webp |
| SVG Image | image/svg+xml |
| WOFF2 Font | font/woff2 |
| PDF Document | application/pdf |
| JSON | application/json |
When to Use Data URIs
✓ Good for
- • Small images (<10KB)
- • Icons and logos
- • Images used once
- • Reducing HTTP requests
- • Email HTML templates
- • Single-file HTML pages
✗ Not ideal for
- • Large images (>10KB)
- • Frequently changing images
- • Images used on multiple pages
- • When caching is important
- • Mobile-first sites (larger HTML)
Performance Considerations
Pros
- Fewer HTTP requests: No separate request for each image
- No CORS issues: Data is inline, no cross-origin concerns
- Atomic loading: Image loads with the HTML/CSS
Cons
- No caching: Data URI images can't be cached separately
- Larger file size: Base64 adds ~33% overhead
- Blocks rendering: Browser must decode before displaying
- Duplicated data: Same image used twice = twice the data
Creating Data URIs
JavaScript (Browser)
// Using FileReader
const reader = new FileReader();
reader.onload = () => {
const dataUri = reader.result; // Already includes data:... prefix
console.log(dataUri);
};
reader.readAsDataURL(file);
// Using Canvas (for images)
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
const dataUri = canvas.toDataURL('image/png');
Command Line
# macOS/Linux
echo "data:image/png;base64,$(base64 < image.png)"
# Or with openssl
echo "data:image/png;base64,$(openssl base64 -in image.png | tr -d '\n')"
Browser Support
Data URIs are supported in all modern browsers. The main limitations are:
- IE 8: Limited to 32KB
- IE 9+: Limited to 4GB
- Modern browsers: Practically unlimited
Try It Yourself
Drag and drop an image file into our encoder to get a Data URI you can use in your HTML or CSS.
Create Data URI →