You probably know that you can use data URIs in just about any browser now, but how do you convert an image to a data URI? It turns out, most browsers can do it for you.
What is a data URI?
Data URIs are all the rage these days. They let you reduce HTTP requests by putting small images right in your HTML or CSS. For a full overview of what they are and how to use them, check out this post on CSS Tricks.
Here’s what they look like in case you need a refresher:
Now I’m going to show you how to generate these URLs using a tool that every web developer has: your browser.
Chrome & Opera
This trick works in Chrome and newer versions of Opera, since they use the same rendering engine.
1. Load the page that contains your image (loading the image directly won’t work) and right-click on it. Select Inspect Element.
2. You’ll see the console appear. Now click on the value of the
3. The console will switch to the Resources tab and highlight the image. Right click on it and select Copy Image As Data URL.
4. Now just paste it into your HTML or CSS!
Firefox is the easiest because it works for images on a webpage as well as images loaded directly in the browser. There’s also one less step compared to Chrome/Opera.
1. Load the image or the page containing your image and right click on it. Select Inspect Element.
2. The image will appear highlighted in the DOM. Right click on it and select Copy Image Data-URL.
3. Paste it into your HTML or CSS.
Safari is similar to Chrome, but the UI is a bit different. It’s the same number of steps, and it won’t work if you load the image directly in the browser—it has to be on a page.
1. Load the page that contains your image and right-click on it. Select Inspect Element.
2. The console will appear with the image highlighted. Click on the value of its
3. The image will be shown in the console. Right click on it and select Copy Image.
4. Paste it into your HTML or CSS.
Alas, I haven’t found a way to copy an image as a data URI using IE9, 10, or 11, but you can use a free online tool instead.
A free online tool
Another easy way to generate data URIs is to use a free service. DataURL.net lets you drag and drop an image and gives you the resulting code instantly. So if you don’t like the in-browser method (or if your browser doesn’t support it) you’re not out of luck.