Which responsive images solution should you use?

Avatar of Chris Coyier
Chris Coyier on (Updated on )

📣 Freelancers, Developers, and Part-Time Agency Owners: Kickstart Your Own Digital Agency with UACADEMY Launch by UGURUS 📣

There are a bunch of techniques going around for dealing with responsive images lately. That is, solutions to help us serve the right image for the occasion (e.g. size of screen and bandwidth available). They all do things a bit differently. To keep track, Christopher Schmitt and I have created this spreadsheet of techniques.

The spreadsheet has the data, but let’s digest it through thinking about it through the lens of practical questions.

To choose which technique is right for you and your project these questions may help as a guide. Many of the questions may apply to your project, so you’ll have to sort out which techniques fit what scenarios and find the overlap.

Do I have legacy content?

Which really means… do I have legacy content that is impractical to update? For instance, I have thousands of pages of content on CSS-Tricks and a writing staff of one.

Yeahhhh… I’m not going to go back and update every <img /> on the site, so I need a technique that will allow me to leave those alone.

The only technique I know of that works with absolutely no markup changes is Adaptive Images. It works by routing requests for images through a PHP file which intelligently serves (and creates if need be) images of the appropriate size for the screen width.

Another question to ask yourself is if you care about legacy content. Perhaps the vast majority of traffic to your site is for newer content in which you can make markup changes and thus take advantage of other techniques. If that’s the case, read on to discover those other techniques.

If you have a small project, a brand new project, or a project with legacy content that you are able go back and update, you are also able to choose a technique which does require special markup, so also read on.

Do I care about special markup?

This is really a sub-question of the above. Many of the techniques require you to use special HTML. For example, HiSRC has you put higher resolution images as data-attributes:

<img src="200x100.png" data-1x="400x200.png" data-2x="800x400.png" />

I’d say this is a clean, valid, semantic technique, but it also means that you need these attributes on every <img /> on your site, which may not be possible on sites with loads of legacy content.

If you know that special markup (or specialized CSS) is not an option for you, really the only option is Adaptive Images. Even Sencha.IO requires prefixing the src attribute which may not be possible with legacy content.

Do I care about semantics?

Some responsive images techniques involve markup which isn’t strictly semantic. Ultimately, there is only one way an image can be semantic. That is if the src of it points to a real image and it has alt text describing that image. Brad Frost sums it up nicely:

In other words, if the technique requires at any point the src of the image to be missing or link to a transparent GIF (or the like), that’s not semantic.

So why do some responsive images techniques do this? Because having an image with a src that points to that image of a horse means that that image will start downloading as soon as that image gets parsed by the browser. There is no practical way to prevent this. Even if you super quickly swap out that src with a more appropriate version, now you’re downloading two images instead of one which is a performance hit instead of a performance gain. You may decide that’s acceptable (e.g. “desktop” environments typically have more bandwidth). Usually if this technique is employed, the image in the src is the smallest possible image.

If semantics is important to you, you should look at Adaptive Images (covered above) or HiSRC, a plugin by Christopher Schmitt which you can use with a semantic src attribute.

A few of the techniques use

Do I care about validity?

Validity as in “Does it validate?” according to the W3C Markup Validation Service. Validation is a tool to help you find problems and write better markup. But just because something doesn’t validate doesn’t make it bad or wrong. If invalid code works wonderfully in all browsers, you nor anyone else should care.

If you do care about validity (perhaps a client irrationally requires it from you and is holding your paycheck randsom) then there are a few techniques that you can’t use. The picturefill technique, for instance, uses the <picture></picture> element to work its magic. This may ultimately be standarized, but it isn’t yet, so it’s technically invalid syntax. It’s also required that <img /> elements have a src attribute, so techniques that remove that to get around the double-image-request problem are invalid.

I’d recommend these techniques if validity is a requirement for you: Adaptive Images, HiSRC, or Responsive Enhance. All of which use simple, valid <img /> syntax that include a src.

Do I care about art direction?

Some responsive images techniques serve different resolution versions of the exact same image. While that makes things easier (i.e. less work) it may not acceptable. Here’s a visual example:

On the left, the “mobile” and default src. In the middle, a slightly larger image that could be used on (ahem) “tablets”. On the right, the largest of the images.(credit)

These images are hand-crafted by a designer, cropped to retain meaning and impact. If you took the image on the right and just scaled it down, the people in the image would be very small and the feel of the image my be lost.

If this idea of art direction is important to your project, you’ll need a technique that will allow you to specify exactly which src to serve under which circumstances. This is picture perfect (GET IT?) for picturefill which allows you to be very specific about sources and what circumstances get what.

<picture>
  <source src="small.jpg" />
  <source src="medium.jpg" media="(min-width: 400px)" />
  <source src="large.jpg" media="(min-width: 800px)" />
</picture>

JavaScript takes it from there.

Do I care about JavaScript?

Most of these responsive image techniques utilize JavaScript to work their magic. Some only a tiny bit to set a cookie, but JavaScript none the less. Some of them have you put an <img /> in a

What about JavaScript *library* dependency?

HiSRC and rwdImages are both jQuery dependent. If your project is using a different library, these probably aren’t for you. But hey, you could port it and open source that! If you aren’t using a library, well, you probably should be but let’s not get into that.

Do I care about Server Side Components?

Some of these techniques aren’t solely JavaScript dependent. Adaptive Images works it’s magic primarily through .htaccess and PHP. Well, .htaccess presupposes an Apache server. And, while of course we all know and love PHP (ahem), many websites run on technologies like Ruby or Python.

Responsive Images (the original Filament Group technique) also uses .htaccess. So if you’re using something like Nginx as web server, this is either out or you’ll have to port over the .htaccess component to Nginx’s similar-but-different syntax.

Do I care about bandwidth testing?

Testing the browser window width and making decisions on what image to serve based on that is pretty cool and fundamental to the concept of responsive images. But it’s really only half of what the decision of what image should be served should be based on. The other half is available bandwidth. If the user has a very fast internet connection speed, serving large images is OK. If the user has a very slow internet connection speed, they should get smaller images (regardless of screens size). Unfortunately native bandwidth media queries don’t exist.

Two of the current techniques do bandwidth testing as part of their decision making: Foresight.js and HiSRC (both are based on the technique in Foresight.js). It works by downloading a test file and measuring how long it took (configurable). The test itself is a slight performance hit, but theoretically the savings gained by serving images based on knowing the current bandwidth is a net (GET IT?) gain.

Do I care about relying on third parties?

Sencha.IO is a completely third-party way of handling responsive images. As far as I know, it works great and hasn’t been inflicted with any major downtime, but of course you always run that risk.

You might be thinking: Wow, the Sencha.IO technique is really cool but I worry about the third-party dependency. I wish I could run that on my own server. If you want to go down that road, there is the public WURFL database and this Server Side Responsive Images technique which puts that to work locally.

There are also third-party services like Device Atlas Cloud which does device detection for you. It’s also a third-party dependency for your app. No doubt their goal and focus is staying up and fast at all times, but you have to be very careful about who and what you rely on for your business.

A couple of other third party services: ReSRC.it, Responsive.io, Thumber.io

Is there a specific CMS with specific CMS powers involved?

Say your project is in WordPress. WordPress has a media uploader built in. When you upload an image with it, it can create multiple versions (scaling down) of that image for you. That’s pretty cool and powerful and you could/should take advantage of that.

This isn’t just a WordPress thing though. I’m sure the concepts at work here could be done (or made to be done) in any Content Management System.

Do I care about double-requests as long as the solution is mobile first?

Many of these solutions try to solve the the problem the best way possible: only making a single request for the right resource. If you are OK with linking up the smallest version of the file (so that request is made no matter what) and replacing it with larger images when needed, perhaps Source Shuffling would work for you. Do note that the library that uses now suggests using font-family rather than content to detect the media query changes.

Can I wait for the future?

The release of the “new iPad” (the third one, for longevity) is what sparked a lot of these techniques and conversations. Its high pixel density is great for vectors and big photos, but actually not great for things like small icons that need to be scaled up to be the correct size and can be blurry. But serving higher resolution icons means larger file sizes and slower websites. Hence, the need to only serve them in situations/environments that need them.

The world of web standards is aware of this problem. There is a whole group dedicated to talking about it. In time, they may solve it and then we can start using whatever way they come up with (assuming its awesome and better than what we have now).

It may be flipping out the src of images through CSS content like Nicolas Gallagher suggested. It might be the <picture></picture> element. It might be a srclist attribute in HTML or src property in CSS. It might be a prefix.

It maybe sending browser information requests, like in Client-Hints.

More resources