Divya Manian

RSS Feed Youtube Channel Github

Using Data URIs in CSS

Chris Hills brought Data URIs to my attention in a comment on my post on web fonts. I had not thought about using Data URIs in CSS at all, but others have.

Data URIs allow any file to be embedded inline within CSS. Here is an example of a Data URI:

    span.button {

See how it looks

Data URIs are always of this format:


mediatype can be any of the Content Types. The Content Types most useful for CSS are:

  • font/opentype
  • application/x-font-ttf
  • image/png
  • image/gif
  • image/jpeg
  • image/svg+xml

Data URIs can be used in the CSS in many ways.

Embedding Images in CSS

Any CSS property that uses the src function can use Data URI scheme to embed data. This means, you can embed a CSS sprite like:

.button {
    background:url(data:image/png /*embed code */)

.button.active {
    background-position:0 -40px;

.button:hover {
    background-position: 0 -20px;   

See a demo of embedding images with data URI scheme

This only works on Firefox 2+, Opera 7.2+, Chrome, Safari, IE 8+. For earlier IE versions, you can use a MHTML workaround in your IE-specific stylesheet.

Embedding Web Fonts in CSS

Most browsers allow fonts to be embedded using Data URI scheme.

Robert Accettura wrote a great article investigating Data URLs in @font-face. His demo page renders the embedded fonts correctly in Opera 10, Firefox 3.5, and Safari 4.

IE 8 (the only IE that supports data uri scheme), for security reasons, allows only images to be embedded inline, so @font-face will not render embedded fonts.



  • Large file sizes. Embedded images work best with small images like icons and arrows. Rob Flaherty has done some excellent research into real-time performance of inline embedding.
  • Not easy to maintain. Any change to the image or font needs to be added correctly to the CSS.
  • Comments