On this page
A data URL puts an asset’s contents directly into a URL, so you can embed small images or other data in HTML and CSS instead of requesting a separate file. That convenience has costs: larger documents, different caching behavior, content security policy requirements, and no guarantee that embedded content is safe. This guide explains the syntax and helps you decide when a data URL in HTML or CSS makes sense.
Key Takeaways
- Use data URLs for small, stable assets when keeping them beside their consumer is useful; keep larger or frequently reused assets in separate files.
- Percent encoding and Base64 are different ways to represent data. Neither compresses it.
- An embedded asset is part of its containing HTML or CSS resource, not an independently cacheable file with its own URL.
- Content security policy must allow the relevant
data:resource in the appropriate directive. - A data URL describes how data is represented, not whether the data is trustworthy.
How data URLs work
A data URL has this general form:
data:[<media-type>][;base64],<data>
The comma separates the metadata from the payload. The media type describes the content, such as text/plain or image/svg+xml. If you omit it, the data URL uses a default media type and character encoding; specify the type explicitly when embedding an asset so it is interpreted as intended.
The payload can be represented using percent encoding or Base64. These are alternative representations, not compression methods.
Percent-encoded data
Percent encoding represents bytes using % followed by two hexadecimal digits. For example, a comma and a space can be represented as %2C and %20:
<p><a href="data:text/plain,Hello%2C%20world%21">Open a text data URL</a></p>
For non-ASCII text, encode the text as UTF-8 before percent-encoding its bytes. Characters that are meaningful in a URL, such as #, may need encoding too. In CSS, also account for the quoting and escaping rules of the surrounding url(...) value.
Base64-encoded data
The ;base64 marker indicates that the payload is Base64-encoded bytes:
<a href="data:text/plain;base64,SGVsbG8sIHdvcmxkIQ==">Open the same text</a>
Base64 is useful for binary data because it represents arbitrary bytes using a restricted character set. It is not a size-saving encoding: it represents every three input bytes with four Base64 characters, before any surrounding URL or document overhead. Percent encoding may be more convenient for text whose characters need little escaping; the better choice depends on the actual payload and context.
Advertisement
Useful cases in HTML and CSS
A data URL can be used wherever the relevant HTML attribute or CSS property accepts a URL. A common case is a small decorative image:
<img
alt="Blue dot"
src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1'%3E%3Ccircle cx='0.5' cy='0.5' r='0.5' fill='%23007acc'/%3E%3C/svg%3E"
>
The encoded payload is a small SVG. The # in its color value is written as %23 so it is treated as part of the payload rather than as a URL fragment delimiter.
The same idea works in CSS:
.status-dot {
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1'%3E%3Ccircle cx='0.5' cy='0.5' r='0.5' fill='%23007acc'/%3E%3C/svg%3E");
}
Typical candidates include small decorative icons and tiny, fixed assets used only by one page or stylesheet. A separate file is generally easier to maintain when an asset is large, shared across many pages, updated independently, or expected to benefit from its own caching and delivery policy.
Size costs and caching trade-offs
There is no universal size threshold at which a data URL becomes a bad choice. The result depends on the asset, the encoding, the document or stylesheet, and how often that resource is used. Without a reproducible comparison for your actual assets and delivery setup, avoid relying on a specific size-saving claim.
Consider these costs instead:
- The containing resource grows. The payload travels as part of the HTML or CSS that contains it. Base64 adds encoding characters; percent encoding can expand bytes that need escaping.
- Repeated copies repeat data. If the same payload is written into several documents or stylesheets, each containing resource carries its own copy.
- Caching is coupled to the container. A data URL does not give the asset a separate network URL to cache or update independently. If the containing stylesheet is cached and reused, its embedded data is reused along with it; changing the asset means changing that stylesheet. HTML-embedded data is similarly tied to the HTML response.
- Tooling becomes less convenient. Long data URLs can make source files harder to inspect, diff, review, and edit.
A standalone file is often a better choice for assets that are shared or change independently. A data URL can be reasonable for a tiny, stable asset when the simpler packaging is worth the trade-offs.
Content security policy and browser considerations
A page’s content security policy (CSP) can block data URLs. If a data URL is needed, allow the data: scheme in the directive that governs that resource type. For example, a policy could permit data URLs for images while keeping other resource directives more restrictive:
Content-Security-Policy: default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'
This example allows data: images through img-src; it does not allow data URLs for every resource type. Check the directive applicable to the resource you are embedding, and do not add data: broadly just to silence a policy violation. In particular, allowing a scheme for executable content can weaken the policy.
Data URLs are supported by modern browsers for common resource uses, but support and security behavior can vary by browser, context, and resource type. This guide does not provide a version-by-version compatibility matrix because live browser and documentation research was not available for this article. Check the browsers and embedding contexts your project supports before depending on a less common use.
Advertisement
Security limits: embedded does not mean trusted
A data URL is a way to carry data, not a security check. Anyone who can supply or modify the URL may control its media type and payload. Do not trust content just because it appears inline, uses Base64, or begins with data:.
The risk depends on how the content is consumed. An image used only as an image is different from content interpreted as HTML or another active format. Do not construct a data URL from untrusted input and assume that encoding it makes it safe. Base64 is reversible encoding, not sanitization or validation.
When an application accepts or generates data URLs, decide which media types and uses are permitted. Validate data according to the intended context, and use a restrictive CSP as an additional layer rather than as a substitute for safe handling. Do not allow data URLs for executable resources unless there is a specific, reviewed requirement.
Common Mistakes
- Treating Base64 as compression. It changes the representation; it does not make the original payload smaller.
- Forgetting the comma. It separates the URL metadata from the payload.
- Leaving reserved characters unescaped. Characters such as
#can change how a URL is parsed if they are meant to be part of the data. - Assuming the payload is independently cached. The data URL is embedded in its HTML or CSS container.
- Adding
data:to an overly broad CSP directive. Permit only the resource types that actually need it. - Equating encoding with safety. Percent encoding and Base64 do not validate, sanitize, or make untrusted content trustworthy.
Best Practices
- Use explicit media types so the intended format is clear.
- Choose percent encoding or Base64 based on the payload and context, not an assumption that one is always smaller.
- Keep separately cached, shared, or frequently updated assets in separate files.
- Test the final HTML or CSS in the browsers and contexts your project supports, including its CSP.
- Treat data from users or other untrusted sources according to the content type and the way it will be consumed.
Conclusion
Data URLs are a useful packaging option for some small, stable assets, but they trade a separate resource for a larger containing document and less independent caching. Choose them deliberately, check CSP and browser requirements, and never mistake an encoded payload for a trusted one.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.