How to Use Data URIs
A data URI embeds a file directly in the markup as data:mediatype;base64,payload, removing one network request. Base64 adds roughly a third to the size, and embedded content cannot be cached separately from the document that contains it. The trade is worth it for small, rarely-changing assets and harmful for large ones.
Data URI Encoder
Turn an SVG or snippet into an embeddable data: URI.
The appeal is obvious: one fewer request, no separate file to deploy, and no chance of a broken image path. The cost is less obvious, which is why data URIs are so often overused.
Two costs matter. Base64 inflates the payload by about 33%, and an embedded asset is re-downloaded every time its containing file changes, because it has no cache identity of its own.
Step by step
-
Confirm the asset is small and stable
Under a few kilobytes and rarely changing is the sweet spot: an icon, a small background, a single font subset. Anything large or frequently updated should stay a separate, cacheable file.
-
Encode it with the correct media type
The media type must match the content, exactly as with a Content-Type header. data:image/png;base64 for a PNG, data:font/woff2;base64 for a font. A wrong type here fails the same way a wrong header does.
-
Skip Base64 for SVG
SVG is text, so it can be embedded URL-encoded rather than Base64-encoded, which avoids the 33% penalty entirely and often produces a smaller string than the original file. Encode the angle brackets, hash and percent signs.
-
Weigh it against caching
An embedded asset is downloaded again with every change to the stylesheet or page holding it. If the same icon appears across many pages, a separate file cached once beats the same bytes embedded many times.
-
Check the Content-Security-Policy
A strict policy may block data: URIs. Loading images this way needs data: in img-src, and fonts need it in font-src. If an embedded asset silently fails to appear, look at the console before suspecting the encoding.
Example
The same tiny SVG both ways. URL-encoding is shorter than Base64 and stays readable.
Base64
url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciLz4=")
URL-encoded, preferred for SVG
url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E")
Frequently asked questions
What is a data URI?
How much larger does Base64 make a file?
Should SVG be Base64-encoded?
Do data URIs hurt caching?
Why does my data URI not load?
Tools used in this guide
All tools →- Data URI Encoder Turn an SVG or snippet into an embeddable data: URI. In your browser
- Base64 Encoder and Decoder Encode text to Base64 or decode it back, including URL-safe. In your browser
- URL Encoder and Decoder Percent-encode text for URLs, or decode it back. In your browser
- MIME Type Lookup Find the MIME type for any file extension, and back. In your browser
Related guides
All guides →- How to Fix Base64 Decoding Errors Padding, the URL-safe alphabet, stray whitespace and double encoding — the four reasons a decode refuses.
- How to URL Encode Text Percent-encode a value safely, and know when to encode the component rather than the whole URL.
- Minification vs Compression Two different savings that stack — and why compression does most of the work.
Last reviewed .