There are two key contenders addressing the issue of serving images responsively. Léonie Watson weighs up some of the pros and cons
This article first appeared in issue 240 of .net magazine – the world's best-selling magazine for web designers and developers.
If you’re thinking about responsive design (and who isn’t these days), the chances are you’ll also need to think about image quality. The problem is that an image designed to look good on a 27 inch monitor probably won’t look too good on a 3.5 inch screen. Even if it did, it makes no sense to serve a huge image to a small screen device, then force it to waste resources scaling it down to fit.
Enter two possible solutions, in the form of HTML extensions for adaptive images: the picture element, and the srcset attribute. Both are in draft status but, if added to the HTML specification, they will make it possible to display an image that can come from a choice of sources.
The picture element works in combination with the source element and CSS media queries. It uses the img element as fallback for legacy devices. The extension includes the following example:
<picture width="500" height="500"> <source media="(min-width: 45em)" src= "large.jpg"> <source media="(min-width: 18em)" src= "med.jpg"> <source src="small.jpg"> <img src="small.jpg" alt=""> <p>Accessible text</p> </picture>
The srcset attribute can optionally be applied to the img element. It takes one or more comma separated URLs, each with descriptors defining the maximum viewport dimension and pixel density. The extension includes the following example:
<h1><img alt="The Breakfast Combo" src="banner.jpeg" srcset="banner-HD.jpeg 2x, banner-phone.jpeg 100w, banner-phone-HD.jpeg 100w 2x"></h1>
When it comes to accessibility, the advantage of the srcset attribute is that it’s applied to the img element. Browsers and assistive technologies (screen readers in particular) know how to handle the alt attribute, so it degrades gracefully right across the board.
The picture element extension takes a different tack: it refers to “accessible text” rather than to “alternate text”, and places it in a child paragraph of the picture element. It isn’t clear what purpose the accessible text is supposed to fulfil. The definition of the picture element doesn’t appear to require accessible text to be provided, or give any hint about how it should be handled by browsers or assistive technologies.
But one thing is evident. For older browsers that ignore the picture element and fall back to the img element, the associated alt attribute will provide the text description used by assistive technologies. The trouble is, the child paragraph of the picture element will also be rendered by the browser, causing a duplication of the ‘alternate’ information.
Accessibility bugs have been opened on the picture element and work is also under way to include the srcset attribute within the picture element extension. So, it’s early days for both specification extensions, but this is definitely one area of the HTML5 arena to keep a close eye on.