Improving Image Quality on the Retina Display

Improving Image Quality on the Retina Display

The new iPad was released last month, and, amongst other things, it sports an incredible Retina Display with a 2048 by 1536 pixel resolution. Pretty much as soon as it was announced, focus turned to native app developers and their work to scale apps up to the higher resolution.

That was arguably the wrong focus. Relatively little was thought about in terms of the web, and how that would look on the new Retina Display in the run up to the announcement. It’s left us with an internet of amazingly crisp text, but laughably terrible, low quality images. It’s time to optimize, so in this tutorial we’re going to strategize how you can design with the Retina Display in mind.


Use Fewer Images

This is arguably pretty obvious, but it may still be overlooked by some. With significant advancements in CSS, the use of images in a site’s structure can be fairly minimal, or even non-existent at all. This gives these sites a huge advantage when it comes down to Retina Display compatibility because text and CSS-generated graphics will scale up without reducing in quality. For the same reason, text looks measurably better on a Retina Display.


Apple has made a big deal of text quality on the new iPad’s Retina Display

A site I was working on needed not a single update to its theme to look great on the Retina Display; the lack of images meant there wasn’t anything to update. Of course, not every site will be totally sans images, but minimizing their use will reduce the effort required to optimize for the Retina Display.


See what Paul Boag has to say on images and rounded corners

Generally, there’s one step to this tactic: minimize image use where it’s not really needed.


How Apple Does It

Naturally, Apple’s website is optimized to look incredibly sharp and crisp on the Retina Display. In the days prior to the iPad’s announcement, Apple started updating their own website to offer up responsive images; items that changed depending on whether you were browsing on a new iPad or not.

Apple’s method is simple in principle. The website loads up as normal, but then they use JavaScript to replace relevant images if you’re browsing on a new iPad. If you visit on a new iPad, you’ll get the optimized images, whereas any other browser will pull up the defaults.

One such example of this is Apple’s hero text on the iPad’s product page, using the following code in the file itself.

<hgroup class="headline">
	<h1><img src="http://images.apple.com/ipad/home/images/overview_title.png" width="471" height="94" alt="Resolutionary" /></h1>
	<h2 class="first"><img src="http://images.apple.com/ipad/home/images/overview_intro_build.png" width="280" height="53" alt="The New iPad" /></h2>
	<h2 class="second"><img src="http://images.apple.com/ipad/home/images/overview_intro.png" width="490" height="53" alt="Introducing the new iPad. With the stunning Retina display. 5MP iSight camera. And ultrafast 4G LTE." /></h2>
	<h3><img src="http://images.apple.com/ipad/home/images/overview_price.png" width="135" height="24" alt="Starting at $499." /></h3>
</hgroup>

For simplicity’s sake, I won’t share their actual scripting, but essentially it first checks whether the browser is on a device with a Retina Display, then checks for the existence of a 2x image (we’ll get onto that in a minute) and, if both the previous conditions are true, displays the 2x image.

The 2x suffix convention means the “Retina” counterpart for the file image.png would be image_2x.png. This comes from iOS, which automatically displays an @2x image in a native app when it exists. Unfortunately, we web designers don’t get the luxury of automatic search-and-display, so we need to come up with methods of emulating that with our own scripts and queries.

This does come at a price however. Using this method, the end user loads both the non-Retina and Retina image, meaning the page ends up with an increased file size. Not only that, Apple’s scripting means more requests are sent to and from the browser, resulting in further page load time. Of course, if you continue to minimize image use, this problem will also be reduced.


The Tutorial: Using CSS Media Queries

While Apple uses JavaScript, a somewhat effective method, there is an alternative; using CSS media queries to serve up styling condition to the device’s pixel density.

Before we get onto that, it’s important to note that, ultimately, the iPad’s display is still 1024×768 px in “CSS pixels”. I’ll allow Dave Hyatt to explain.

Most Web site authors have traditionally thought of a CSS pixel as a device pixel. However as we enter this new high DPI world where the entire UI may be magnified, a CSS pixel can end up being multiple pixels on screen. For example if I set a zoom magnification of 2x, then 1 CSS pixel would actually be represented by a 2×2 square of device pixels.

Therefore, the method we’re using won’t target the iPad based on its resolution because it simply wouldn’t work. For all intents and purposes, the iPad’s resolution is still 1024×768 px according to the browser, and targeting this resolution would also target all previous iPad generations. Likewise, targeting the new 2048×1536 px resolution with media queries wouldn’t have any effect on the new iPad.

Instead, we can target using the min-device-pixel-ratio query. That query tells the browser to load up a specific style based on the ratio of device pixels to the aforementioned “CSS pixels”. In the Retina Display-equipped iPad’s case, this is two (1024:2048 and 768:1536 simplified down to 1:2).

Therefore, we can load in a “retina” stylesheet based on the device’s pixel ratio.

<link rel="stylesheet" type="text/css" href="retina.css" media="only screen and (-webkit-min-device-pixel-ratio: 2)" />

Obviously, we use the WebKit prefix since the iPad’s browser is Safari, a WebKit browser, but also because the media query seems to be only available through WebKit for now. Since styles cascade (as the name “CSS” will tell you), by simply ordering the above tag correctly, rules in that stylesheet will overwrite the ones for regular devices specified previously.

If you don’t happen to want to put everything in a whole separate stylesheet, you can use inline media queries to specify portions of your stylesheet for Retina Display-equipped devices only, like so:

@media(-webkit-min-device-pixel-ratio: 2) {
	#element {
		background-image('image-2x.png');
	}
}

Note: While using the “2x” naming scheme is a useful way of organizing your images, don’t feel obliged to do so. You can name your images whatever you want and, unlike with native apps, they don’t need to be even remotely similar.

Of course, using this media query isn’t in any way specific to the iPad. In fact, the same logic will be triggered when browsing your site on any device with a similar pixel density that employs the same CSS to device pixel ratio, including the iPhone 4 and iPhone 4S. If you do want to target only the iPad, you can simply combine the pixel ratio query with a width one, to specify only Retina Display-equipped devices of the iPad’s reporting resolution.


Conclusion

It’s not a case of your site looking good without optimizing, but looking better with. You only need to visit an image-rich, but non-optimized, site to understand how terrible non-Retina imagery looks on the new iPad. And, ultimately, you need to actually witness the results. If you don’t have access to a new iPad, I highly recommend taking a trip to an Apple Store to take a look.

While Apple has sold millions of units, in the grand scheme of browsers, it’s still a small concern. However, with potential Retina Display-equipped MacBook on the horizon for as early as this summer, putting in the work now could really pay off later.

Tags: retina
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://nicolazanon.tumblr.com/ Nicola

    Hi,

    one question: if you do this:

    @media(-webkit-min-device-pixel-ratio: 2) {
    #element {
    background-image(‘image-2x.png’);
    }
    }

    the browser will load the other image (image.png) as well, so your’re gonna end up with an extra download time. If you have many images in your site it will be painful to load on a mobile phone.

    Is there a way to prevent loading both images?

    Thanks!

    • http://www.brettjankord.com Brett Jankord

      You can check out https://github.com/bjankord/Hi-Res-Images. On first load, it use javascript to check for retina capable displays, and then swaps out all the images with a “hi-res” class. The user does download 2 images on first load, but the script then sets a cookie and on additional pages the user visits, it will only load the retina image. Another solution I’ve seen is http://retinajs.com/

      I’m working on a noscript solution that would get rid of the dual downloads on initial page visit, hoping to have that finished soon.

    • http://ktee8.com/ Ktee8

      If you wanna stick to noscript way, you may find a hint here.
      http://timkadlec.com/2012/04/media-query-asset-downloading-results/

      Like test five above, set the default image as @media(-webkit-device-pixel-ratio: 1),
      and it’s gonna be alright? I don’t know. Otherwise, if you set the default in regular css,
      the results could depend on devices like test four, I guess.

      If you need to use image tag instead of css background-image,
      you may need to rely on scripts like he mentioned. Let me add one more.
      http://retina-images.complexcompulsions.com/

      Anyway, thank you very much for this article. I’m begging to see the light!!

  • http://www.iqonicdesign.com Ari

    Great article, I was wondering how to do this in CSS! I have a new iPad, and it’s wise to use CSS instead of images for many UI elements, to cut down on load time.

  • http://www.aaronlumsden.com Aaron Lumsden

    Nice little article. This is something I’ve been thinking about since the release of the new iPad. For some strange reason it never even occured to me to use media queries. Doh! Thanks :-)

  • http://mdnw.net Brandon Jones

    Fantastic article Connor! (and very timely as I was just thinking about this issue)

    This might be an interesting case for moving as much of a site’s images over to responsive imaging via technique such as : http://viewportindustries.com/blog/automatic-responsive-images-in-wordpress/ – it’s certainly not going to work for older sites with a low res library of images… but moving forward it seems like having at least 2 images served up for different resolutions might be the way to go.

    • http://ktee8.com/ Ktee8

      I actually appliked it to my own site and it seems to be working OK.
      http://ktee8.com/2012/04/573/
      But it’s not for retina. I wish I could put all ideas together:)

  • http://themeforest.net/user/pogoking?ref=pogoking pogoking

    The technique presented here didn’t account for one thing – since the device doesn’t know that you are actually loading an optimized assets, they will be scaled too and will appear 4 times bigger than you intended them to. What you have to do here is add a background-size property. If the original image was 16x16px (an icon, for example), you’d have to use a 32x32px image for retina display and scale the background down to 16x16px again, like this:

    #element {
    background-image: url(image-2x.png);
    background-size: 16px auto;
    }

    • http://www.creativeindividual.co.uk Laura

      Thanks, I was wondering what I was doing wrong. background-size works like a charm! :)

  • Pingback: Improving Image Quality on the Retina Display | Shadowtek Hosting and Design Solutions

  • Pingback: Websites und Bilder für High-Resolution-Displays (Retina) optimieren | kulturbanause blog

  • Pingback: Webdesign für das Retina Display › Webdesign & Webentwicklung

  • Victor Engel

    I am visiting this site using a new iPad. Imagine my surprise when half way through reading, the font size suddenly changed! After some experimentation, it appears the font size is differenit for portrait orientation and landscape orientation. I originally came onto the site while in landscape orientation. It looks like the original font size used is what it uses for portrait orientation, and it corrects itself later. This is confirmed by reloading the page in one orientation or the other and/or rotating after the page is loaded,

    • Dave

      You can put in meta tags to stop that re scaling happening.

  • http://www.focality.com Corby Simpson

    This works well where Media Queries can be used, but for HTML elements and tags which may utilize dynamic content, is there not an issue?

    For example, if the traditional code is an tag and the src is pointing to an image, you can’t use a media query to replace or manipulate that image (that I know of?) I think that’s partly why Apple is using JavaScript image replacement… Not everything is CSS.

  • Pingback: Retina revolutie - Design & User Experience blog

  • Pingback: Moughts No. 18: Die Pixel verdichten sich… « MOUGHTS

  • Pingback: Retina revolution - Ontwerp

  • Pingback: (Nicht nur) in eigener Sache: WordPress.com unterstützt Retina-Displays | Retina Mac

  • Pingback: Retina Webdesign › Zure.org - Design, Inspiration, Typografie, Webdesign und WordPress › MacBook, Retina, Retina Webdesign, Webdesign

  • http://www.netvlies.nl Daan

    Good post! As a designer I started doing some testing with Retina images and come to a stunning conclusion. It IS possible to compress the hell out of Retina images and still get a sharper image on Retina screens. The Retina images are even smaller than normal resolution images, Sounds impossible, but true! You can read a blogpost about this phenomenon with lots of test images here:

    http://design.netvlies.nl/ontwerp/retina-revolution/

    Hopefully a small contribution to the discussion!

    Daan

  • Carson Reinke

    Could you not just use a x2 image and set a fixed width on the image? That way retina display has a high resolution image and non-retina displays are told to scale the image.

    • http://www.matthewsilvan.com Matthew Silvan

      Exactly what I would do, too. Since retina display users need to load the larger image anyway, one doesn’t need to fiddle with the CSS and HTML but simply use an image twice the size. The only setback is that we would be forcing large images on “older” systems, but it’s just a matter of time until most systems will sport the higher resolution.

  • Pingback: Retina revolution

  • Pingback: Retina revolution | Itsaat

  • Gladwyn Lewis

    Just a query, instead of increasing the image resolution to 2x, can’t we just use an image with double the DPI and save for the same size? wouldn’t that work fine?

  • Gemma W.

    I know this article is a bit old now but the Conditionizr jQuery plugin provides another way. http://conditionizr.com/ “Conditionizr automatically detects a retina device and tells the browser instantly, adding a ‘retina’ class. For devices that aren’t retina, it adds a ‘no-retina’ class. Hook your retina optimisations for CSS directly from the HTML tag. You can optionally load retina specific scripts and stylesheets too.”

  • Pingback: Web Contemporary | Easy Programs In Retina Display Clarified

  • http://www.facebook.com/johnthecreative John Merrifield

    One thing I don’t like about CSS media queries is that you are loading the images through the style sheets and not html. I sometimes do this also, but I’ve found that pinterest doesn’t see those images as pinnable. I guess you don’t care about that?

    • Eiki Martinson

      Well, you are right that content images really should be img tags in the html rather than backgrounds and not just for Pinterest’s benefit, although that is a good example of the benefits of semantic correctness. I consider the media query technique presented here as good for logos or navigational images, icons, etc. Fortunately such images are getting less and less necessary all the time.