What Is a Data URI?
A data URI is a URL that contains the file's actual content, not a path to a file on a server. The format is:
data:[media type];base64,[Base64-encoded data]
For a PNG image, it looks like:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...
The browser reads this string, decodes the Base64 data, and renders the image โ exactly as if it had loaded a separate PNG file, but without making any HTTP request.
How to Use a Data URI in HTML
Swap the src attribute of an <img> tag with the data URI string:
<img src="data:image/png;base64,iVBORw0KGgo..." alt="Company logo">
You can also use it in CSS as a background image:
.logo {
background-image: url("data:image/png;base64,iVBORw0KGgo...");
background-size: contain;
}
And in an SVG <image> element, or as the source for a CSS content property on pseudo-elements.
How to Convert an Image to a Data URI
- Open the ToolsBox Image to Base64 converter.
- Drop your image file onto the upload area (PNG, JPG, SVG, WebP, GIF are all supported).
- The tool instantly generates the full data URI string.
- Copy the ready-to-paste
<img>tag or CSS snippet directly.
Everything happens in your browser โ the image is never uploaded to a server.
Convert your image to Base64 โ free
Get a ready-to-paste HTML img tag and CSS snippet in seconds.When to Use Data URIs
Data URIs are the right choice in specific, limited situations:
- HTML email templates. Many email clients block external images by default. Embedding small logos or decorative graphics as data URIs ensures they always display, regardless of the client's privacy settings.
- Small icons and logos. If your site uses a small icon (under 2โ3 KB) that appears on every page, embedding it avoids one HTTP request per page load.
- Self-contained single-file web apps. If you are distributing a single HTML file that needs to work offline or as a standalone document, data URIs let you include all assets in one file.
- Critical above-the-fold images. Inlining a very small hero image in the HTML can eliminate the render-blocking delay of waiting for the image request.
When NOT to Use Data URIs
The tradeoffs are significant and data URIs are frequently misused. Avoid them when:
- The image is large. Base64 encoding adds ~33% overhead. A 500 KB photo becomes ~665 KB in HTML. This inflates the page size and slows initial render.
- The image is reused across pages. A separate image file is cached by the browser on the first visit and served from cache on subsequent pages. A data URI is re-downloaded on every page because it is part of the HTML document (which typically is not cached as aggressively).
- You care about Core Web Vitals. Large inline images increase Time to First Byte and Largest Contentful Paint, hurting your Google ranking signals.
- You need to update the image easily. A data URI embedded in HTML requires editing the HTML to change the image. An external file can be replaced with the same filename.
Performance Comparison
| External Image File | Data URI (Inline) | |
|---|---|---|
| HTTP requests | 1 extra request per image | 0 extra requests |
| Browser caching | Cached separately | Not cached independently |
| File size overhead | None | +33% (Base64) |
| Good for large images | Yes | No |
| Good for HTML emails | Often blocked | Yes |
| Good for small icons | Yes (with caching) | Yes (if used once) |
SVG: A Better Alternative for Icons
For icons and logos, inline SVG is often a better choice than Base64 data URIs. SVG is plain text XML that you can paste directly into HTML without any encoding, it scales to any size without quality loss, and can be styled with CSS:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24">
<path d="M12 2L2 7l10 5 10-5-10-5z"/>
</svg>
For raster images (photos, screenshots, complex illustrations), data URIs or external files are the options โ SVG only works for vector graphics.
Frequently Asked Questions
What is a data URI?
A data URI embeds a file's content directly into a URL string. For images it looks like: data:image/png;base64,iVBOR... The browser decodes the Base64 data and renders the image without making a separate HTTP request.
When should I use data URIs for images?
For small icons in HTML emails, logos in self-contained files, or icons under 2โ3 KB used on every page. Avoid them for large photos โ they increase file size by 33% and cannot be cached separately.
Do data URIs work in all browsers?
Yes. Supported in all modern browsers including Chrome, Firefox, Safari, Edge and all mobile browsers. There was a 32 KB limit in old IE versions but this is irrelevant for modern projects.
How do I convert an image to a data URI?
Use the ToolsBox Image to Base64 converter. Drop your image in and it generates the full data URI string plus ready-to-paste HTML and CSS snippets in seconds, without uploading anything.
โ Back to Blog | Related tools: Image to Base64 ยท Base64 Encoder