7 days of unlimited WordPress themes, plugins & graphics - for free!* Unlimited asset downloads! Start 7-Day Free Trial
  1. Web Design
  2. CSS

The What, Why and How of Data URIs in Web Design

Scroll to top
Read Time: 10 mins

Let's look at improving your site's performance by reducing the number of HTTP requests it makes. We're going to take advantage of a technique known as the data URI scheme; encoding both images and fonts into pure data strings that can be directly integrated into your markup and stylesheets.

Thumbnail: The Broccolidryiconsaniconsetitisfullof…icons by Visual Idiot (we'll be using these icons in a minute..)

As Internet connections became quicker and computers more powerful, website performance used to be an often overlooked best practice. Today, ensuring that your website is performing well is absolutely vital for keeping both your users and the search engines happy; especially when the site is being accessed using mobile data and under-powered phones and other devices.

The What, Why and How of Data URIs

We've already looked at CSS Sprite Sheets: Best Practices, Tools and Helpful Applications before on Webdesigntuts+, demonstrating how you can improve performance by reducing HTTP requests and reducing overall image file size. But did you know that you can represent an image (actually any binary data) in your HTML or CSS using only an ASCII text string?

This technique is known as the Data URI scheme, and is in fact not new. Although Data URIs have been floating around for more than ten years, they have only been gaining popularity fairly recently with widespread browser compatibility and a focus on improving site performance.

What Are the Benefits of Using Data URIs?

The primary benefit of using data URIs is performance enhancement. Any resources that are used in your site are fetched by your browser using an HTTP request. This includes everything from stylesheets, to JavaScript files, to images. Regardless of the speed of the internet connection, almost all browsers limit the maximum amount of concurrent HTTP requests to two at a time, effectively resulting in a bottleneck of data. The more HTTP requests that need to be made, the poorer the overall performance of the site will be.

Combining image data into the HTML or CSS of the site instantly removes the need for the browser to fetch additional resources. Although raw Base64 encoded data is generally considered to be around 33% larger than its optimized counterpart (for example an image), after gzipping and CSS file optimization the difference in file size is generally negligible.

What do Data URIs Look Like?

A Data URI is essentially binary data encoded into a Base64 format, along with some additional information for the browser including a MIME type, a Charset and the encoding format (Base64).

The data URI (in its simplest form) looks like this:

To use a data URI as an inline HTML image, the format looks like this:

To use a data URI as a repeating background image in CSS, the format is as follows:

Alternatively, combine the background elements with CSS shorthand:

While we've used images to illustrate the usage of Data URIs, almost any media can be used, including multimedia files, fonts (as we'll see shortly) and much more.

How to Use Data URIs

OK, let's get practical with a couple of examples. Let's take the following image (a repeating tile that I downloaded from Subtle Patterns), convert it into a Data URI and use CSS to create a repeating background for a site or interface.

Repeating Background Tile

Although Data URIs may look pretty funky, the process to create one is very simple.

First, head over to the Web Semantics Data URI Converter and upload the image by selecting the file location of the image and clicking the 'Convert Image' tool:

Web Semantics Data URI conversion toolWeb Semantics Data URI conversion toolWeb Semantics Data URI conversion tool

Once processed, if you scroll down the page, you'll see what this image looks like base64 encoded:

Web Semantics Data URI conversion toolWeb Semantics Data URI conversion toolWeb Semantics Data URI conversion tool

Since we need a repeating background for our project, we'll need to modify the CSS given to us by the tool.

For a repeating background, copy and paste the following CSS:

Let's take an even easier example, converting the following image to a Data URI and slotting it into our markup as an inline image:

Repeating Background Tile

Just follow the steps above to convert the image into a base64 encoded image and copy the HTML markup generated by the tool (from the 'As An Image' panel) and paste it directly into your HTML markup in the same way that you would for inserting a regular inline image.

The final code for this example looks like this:

When to Use Data URIs (and When to Avoid Them)

There aren't really any hard and fast rules about when to use data URIs to replace site assets, but there are a number of drawbacks and peculiarities to consider when choosing whether or not to use Data URIs.

  • If you plan to support
  • Data URIs are not cached by the browser, so they will need to be downloaded on every page, even if they've already been rendered by the browser.
  • Data URIs are most suited to small resources, but they can be used for larger resources (e.g. photographs) without incurring any major performance penalties (after gzipping they are around 2-3% larger than the original file). Be aware, however, that the site user will not see anything until the entire asset is downloaded, which could be undesirable for users on mobile or with slow Internet connection.
  • Data URIs make your CSS and HTML documents look very messy. For large sites and widespread Data URI utilization, stylesheets and markup become more difficult to read, navigate and maintain.
  • Every time that you make a change to an asset (for example a spritesheet), you'll need to re-encode the image file, adding more steps to your workflow.

Base64 Encoded Icon Fonts

Do you remember me saying that any binary data can be used in the data URI scheme? Now that we've looked at base64 encoded images, let's now take a look at turning an icon font into a data URI and implementing it in a project.

In fact, data URIs are a perfect match for icon fonts, especially if you're only planning to use a few symbols with a small filesize.

We're going to be using the online web app IcoMoon to generate a custom font set, output the data URI and generate the final CSS code.

First, head over to the IcoMoon site and click the 'Start Using The App' link:

IcoMoon Custom Font AppIcoMoon Custom Font AppIcoMoon Custom Font App

Next, either browse the font sets to select your custom icon fonts, or alternatively use the search field to the top left of the page to find fonts that match the tags 'Comment', 'Heart', 'Eye' and 'Search'.

I've used the Broccolidry font set from Visual Idiot in this example.

Select Your Custom Font SetSelect Your Custom Font SetSelect Your Custom Font Set

Next, move to the download settings page by clicking the 'Font' button at the bottom of the screen:

Move the Font Download Settings PageMove the Font Download Settings PageMove the Font Download Settings Page

On the download settings page, change the Baseline to 10%, click the settings icon to bring up the advanced options and select the 'Base64 Encode & Embed in CSS' checkbox. Click the 'Save' icon to download your font.

Download SettingsDownload SettingsDownload Settings

Once you've downloaded and extracted the custom font, you can open up the 'index.html' file to view your font and their unicode pairings:

Index.html allows you to view your font pairingsIndex.html allows you to view your font pairingsIndex.html allows you to view your font pairings

Using Your Custom Icon Font

Now that we've created our custom icon font set, let's see how we can use it in a project.

First, open up the stylesheet provided in the IcoMoon download and locate the following CSS. Since the name of the game is about reducing HTTP requests, we're going to be copying and pasting all of the provided CSS into our own main style sheet.

These few lines of CSS will now have your custom Icon Font ready to work on all modern browsers. If you want to support IE9 compatibility mode and old versions of IE, you'll need to add in an additional rule (note that this will fetch additional resources, but only when the User Agent requests them).

Curious about these IE fixes? Paul Irish has a great blog post on a bulletproof @font-face syntax.

Now that our rules have been created, there are two ways that we can include the custom font in our design, both of which use Unicode entitites to reference the relevant icon. The great thing about the IcoMoon app is that all of the CSS has already been generated for us, ready to be copied into our main stylesheet, depending on what approach we decide to use (or both).

Using Your Icon Font: Data Icon Approach

The first technique for using your icon font is the data-icon approach, which keeps our markup nice and clean and is screen-reader friendly.

Let's say that we want to include a comment icon (in this case with the unicode value of ") inside a span element on our page.


The relevant CSS (taken directly from the downloaded IcoMoon stylesheet) is as follows (comments are mine).

While this approach is nice and streamlined and avoids additional class bloat in your markup, it may not be the best choice for maintainable code. Unless your memory of Unicode characters is both encyclopedic and photographic, this approach does not contain any information within the HTML that tips you off to what icon will be displayed.

Using Your Icon Font: Additional Class Approach

As an alternative approach, you may choose to use additional CSS classes in your HTML to display the icon. This has the benefit of inputting semantic, descriptive information directly into your markup, enhancing the readability of your code.

To use this approach, add an element in your HTML with a class name as provided by the IcoMoon stylesheet:

Our CSS will look like this (comments are mine).

Now, with a little additional styling, you can easily implement great looking icon fonts into your next project -- without bogging your asset tree down with additional resources.


Thanks for taking the time to explore some of the ways that you can get the most out of Data URIs for your next project.

It's important to remember that Data URIs are not always the best solution. While the use of CSS3, SVG and Icon Fonts have some very compelling benefits over the use of images (especially in the realm of responsive design), the benefits of encoding your images in base64 are more nuanced. When considering whether or not to use data URIs in your next project, weigh up loading times and reduced HTTP requests with code maintainability — just consider Data URIs as another tool in your toolbox!

Did you enjoy this tutorial and are now itching to get started using these techniques in a more practical way? Keep your eye out for new premium content soon where we will be using data URIs extensively to create a light-weight user interface design. Watch this space!

How have you used icon fonts and data URIs in your projects? Subscribe and leave a comment!

Additional Resources

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.