Here is a little trick that enables you to apply retina images to your web pages in pure CSS, no JavaScipt hacks or "-webkit" prefixes required!

The best part is that only a single retina image is required as opposed to requiring a "2x" and an unscaled version to be present in your project.

How to make a retina image

To begin with a "retina" image is required, which is just a buzz-word for an image that has double the normal resolution. You can create an image like this in Photoshop by making it twice the normal size (a 16x16 pixel icon would be 32x32 instead) and giving it a density of 144, rather than the default 72.

When saved, the file traditionally has "@2x" at the end of the filename and is in PNG format (e.g. "home-icon@2x.png").

CSS Integration

To apply the retina graphic to the web page, we use a solution that takes the double scaled retina image and resizes it to fit the unscaled version.

.home-icon {
  width: 16px;
  height: 16px;
  background-image: url("images/home-icon@2x.png");
  background-position: center center;
  background-repeat: no-repeat;
  background-size: 16px 16px;
}

This CSS snippet gives the icon a 16 pixel square size and we know the "home-icon@2x.png" image is 32 pixels square, so the background-size property fits them to match.

On a regular screen, everything will look normal, however on a "Retina" or other high resolution display, the icon will be displayed in it's true quality without any blur or defects.

The reasons I chose this solution are:

  • Pure CSS
  • Works across browsers
  • Works on mobile
  • Only requires a single image (efficient)

For an example, check out the differences of the image in this JS Fiddle on and off a high resolution display such as an iPhone: http://jsfiddle.net/UxK4N/