What is a Data URI and how do I use it?
What is a Data URI?
A Data URI is a URI that starts with the “data” scheme and allows you to easily embed all kinds of objects within your web page.
Much like Blob URIs, Data URIs have been around for awhile and are supported on all of the major browsers. A Data URI is a formatted string with the following pattern:
The “media type” parameter is optional, but is always a good idea to include it. Usually you’ll see it start with the mime-type of the encoded data. The default mime-type, if it isn’t specified, is “text/plain”.
The “base64” parameter is also optional. If present, it indicates the data that follows is base64 encoded data.
Finally, the “data” parameter is the exactly that. The data you want to render.
Example of a Data URI
A data URI could be as simple as this:
or, more likely, you’ll see something like this:
If you render the example above as an image inside of a webpage, you’d see this:
If you’d like to easily play around with Data URIs and are using FireFox or Chrome, you can enter it in your URL bar to render it. Try it now: data:,Hello world!.
How do I create a base64 encoded string?
The easiest way is to use an online converter. You can google for “base64 encoder” and choose from one of the many websites. Alternatively, if you want to generate the strings via the command-line, you could use OpenSSL to do the conversion for you. You just need to type the following:
openssl base64 -A -in <input file you want to convert to base64>
This will output a base64 string, so you’ll need to embed it into a Data URI in order for it to work.
When should I use a Data URI?
I haven’t used Data URIs much myself, but typically I’d consider using them if I have lots of small images (less than 32×32 pixels) or images that are frequently used. A good example could be to swap out the bullet within a bulleted list.
A recent scenario where it came in handy was when I had to upload a common HTML page to both Google Drive and One Drive and render an image in both. It seemed that no matter which method I used to render a simple square, the only one that worked was by using an embedded data URI.
When should I NOT use a Data URI?
If size is important to you, then you won’t want to use Base64-encoded Data URIs. The reason is that base64 encoding anything will result in a larger output. Typically, it’s about 30% larger. If you use a lot of Data URIs it could also potentially slow the initial page load while the browser decodes any base64 data and generates the appropriate artifact.
Can I use Data URIs for other stuff?
Absolutely! If you can include it on a webpage, you can include it as a Data URI! However, some browsers, such as IE do have limitations on what can be included as a Data URI. For instance, you cannot include HTML as a Data URI in IE.
What are your thoughts? How do you use Data URIs? Are there any further advantages or disadvantages you can think of?
I hope you found this article informative and useful. Please share it if you learned something. If you have any questions or feedback on what you’d like to see in the future, please leave those below!