SiteKickr Web Development

Leverage the iPhone 4 Retina display with CSS

The iPhone for introduced the high resolution retina display. While native iPhone applications can be built to support this display simply by improving the resolution of their graphics, mobile web developers need to take a 2 phase approach, to ensure that non-Retina users aren't forgotten.

While there are JavaScript techniques that can be employed to handle HTML image tags (swapping them for their high-res counterpart when a high-res screen is detected), we are going to deal only with background images in this post.

The approach:

Now, here's where we introduce the high-res file:

Example:

Let's say we have a 512px by 512px high resolution background sprite. We will save two versions of the file:

CSS

#header { background: url(bg.png) 50px 50px no-repeat; }

@media only screen and (-webkit-min-device-pixel-ratio: 2) {
  #header { background-image: url(bg2x.png); background-size: 256px 256px; }
}

Mobile safari will automatically resize your background-image to fit appropriately, while maintaining the resolution of the image.