Find answers to common questions about Data URLs, their usage, and best practices. If you don't see your question answered here, feel free to contact us.
A Data URL is a special type of URL that allows you to embed small files directly into HTML, CSS, or JavaScript code as a string of text. Instead of pointing to a file location, a Data URL contains the actual content of the file, typically encoded in Base64 format.
Data URLs begin with the data:
scheme identifier, followed by a MIME type, an optional Base64 indicator, and the encoded data itself.
There is no functional difference between a Data URL and a Data URI. The terms are used interchangeably in web development.
Technically, "URI" (Uniform Resource Identifier) is a broader term that encompasses both "URL" (Uniform Resource Locator) and "URN" (Uniform Resource Name). In practice, when referring to the data:
scheme, both "Data URL" and "Data URI" refer to the same thing.
The general syntax of a Data URL is:
data:[<media type>][;base64],<data>
Where:
data:
is the scheme identifier<media type>
is an optional MIME type (e.g., image/png
, text/plain
). If omitted, it defaults to text/plain;charset=US-ASCII
;base64
is an optional indicator that the data is Base64 encoded<data>
is the actual content, separated from the preceding parts by a commaData URLs are best used in the following scenarios:
For more detailed guidance, check our Best Practices page.
Data URLs should be avoided in these scenarios:
Modern browsers don't have a specific size limit for Data URLs, but there are practical limitations:
As a best practice, it's recommended to limit Data URLs to resources under 5KB in size.
The choice between Base64 and URL encoding depends on the type of content:
For SVG files specifically, URL encoding typically results in a smaller string than Base64 encoding, making it the preferred choice for SVG Data URLs.
// URL-encoded SVG
data:image/svg+xml,%3Csvg xmlns=%22http://www.w3.org/2000/svg%22 width=%2224%22 height=%2224%22%3E...%3C/svg%3E
// Base64-encoded SVG
data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCI+...
Data URLs are supported in all modern browsers, including:
However, there are some browser-specific limitations to be aware of:
Data URLs have both positive and negative performance impacts:
For optimal performance, use Data URLs for small, frequently used resources that don't change often, and use traditional URL references with proper caching for larger files.