This proposal adds new elements and attribute to [[!HTML5]] to enable different sources of images based on browser and display characteristics. The proposal addresses multiple use cases such as images used in responsive web designs and different images needed for high density displays.
This document was proposed by the Responsive Images Community Group as a solution to bug 18384.

Introduction

This proposal allows content authors to provide user agents with the information they need to select the best image source. The current img element only allows for a single source of an image, but there are numerous use cases where document authors need to define different image sources depending on the factors such as the design, size resolution, and display density.

The best image source may be an image sized appropriately for the display size or pixel density. Or the best image source may be a different version of an image that has been modified by the author to be suitable for a particular use (see: art direction use case).

Goals

This proposal was designed with the following goals in mind:

A detailed list of use cases is provided as an appendix.

Requirements

A conforming user agent must meet the following requirements:

The goals and requirements sections overlap. They should be reconciled.

Dependencies

This specification relies on several other underlying specifications.

HTML5
The terms and algorithms img element, source element, media resource, resource selection algorithm, fallback content, valid non-empty URL potentially surrounded by spaces and valid media query are defined by the HTML 5 specification [[!HTML5]].
RFC2046
The term media type is defined by the Media Queries Specification [[!RFC2046]].
CSS4-IMAGES
The term image-set notation is defined as proposed by the CSS Image Values and Replaced Content Module Level 4 Specification [CSS4-IMAGES].
ALT-TECHNIQUES
The term techniques for providing useful text alternatives for img elements is defined by the HTML5: Techniques for providing useful text alternatives Specification [ALT-TECHNIQUES].

Picture Element

The picture element represents a list of sources of image data and associated attributes that define when an image should be used. Image data sources may be explicitly declared based on media queries or can be suggested to the browser via the srcset attribute on the picture element.

Sample picture element markup:

<picture alt="">
	<source media="(min-width: 45em)" srcset="large-1.jpg 1x, large-2.jpg 2x">
	<source media="(min-width: 18em)" srcset="med-1.jpg 1x, med-2.jpg 2x">
	<source srcset="small-1.jpg 1x, small-2.jpg 2x"> 
	<img src="small-1.jpg"> 
</picture>

Picture element permitted attributes

global attributes & src & srcset & type & media

Source element

The source element is a child of the picture element and extends the existing source element. Each source defines one or more image data sources and the conditions under which that source should be utilized.

Note that all supplied sources for a given picture element SHOULD represent the same subject matter, while cropping and zooming may differ.

It should be codified that this is not a mechanism by which to swap disparate images depending on screen size. See: https://www.w3.org/Bugs/Public/show_bug.cgi?id=18384#c7

Source element permitted attributes

global attributes & src & srcset & type & media

Srcset attribute

The srcset attribute is based on the proposed image-set notation and follows the same syntax:

srcset = srcset( [ <srcset-decl>, ]* [ <srcset-decl> | <color>] ) <em>srcset-decl</em> = [ <url> | <string> ] <resolution>

Sample srcset attribute:

srcset="med-1.jpg 1x, med-2.jpg 2x"

Algorithms

The basic algorithm for processing the picture element is as follows:

  1. Evaluate media/src/srcset attributes on the parent picture element first.
    1. If the media attribute on the picture element should match, evaluate srcset ( per the image-set notation algorithm ) or the src attribute.
    2. If not, continue to step two below.
  2. Evaluate source child elements per the resource selection algorithm already defined for the media attribute to select the appropriate source element.
  3. Once the source element has been identified, look to see if the element has srcset defined. If srcset exists, follow the the image-set notation algorithm. If src is specified on the source element, let that be the chosen picture source.
  4. The user agent will ignore any img elements that are children of the picture element.
It should be possible to determine from script which source was selected. For media elements, this is done with the currentSrc IDL attribute.

Fallback behavior

User agents that do not natively support the picture element will render the fallback content (including any child img element) per the behavior of existing HTML5 media elements.

Accessibility

The picture element has an alternate text (alt) attribute. The alt attribute MUST be handled according to the techniques for providing useful text alternatives for img elements exactly the same way as the alt attribute on the img element.

In order to fall back gracefully, the alt attribute and its value should be repeated on the child img element.

The alt attribute is NOT permissible on source elements. If the alternate text description of a source element differs from other source elements, it would suggest that the listed sources are not different versions of the same image, but are likely to be different images entirely.

It should be noted that the above represents an absolute minimum in terms of the accessibility of the proposed element. Further accessibility discussion can be viewed at http://lists.w3.org/Archives/Public/public-html/2012Jun/0118.html and https://www.w3.org/Bugs/Public/show_bug.cgi?id=18384.

Examples

TODO: add examples

Use Cases

What use cases does this support?

There are many use cases that are supported as listed below. There are two primary use cases.

  1. The need for different image sources at different viewport sizes in responsive web designs.
  2. The need for different image sources depending on the pixel density of the display.

Most of the more specific use cases fall under one of these two umbrella needs.

Viewport Sizes

There are many different screen sizes that are in common daily usage, ranging from small phones to giant high-definition televisions. This change in how we access the web was the main reason for needing to make responsive websites in the first place.

A common practice in responsive design is delivering images without height and width attributes and letting the browser resize the image. This technique is commonly called flexible images or fluid images.

However, delivering an image at a size optimized for large displays to a small display is not ideal. Large images incur unnecessary download and processing time, slowing the experience for users.

To solve this problem, web authors will provide multiple sources of the same image at different resolutions and then pick the correct size image based on the viewport size. This is commonly done for CSS background images in responsive designs, but web authors lack the tools to do so for images in HTML without the use of JavaScript.

Display Density

Since the high-density devices (e.g., retina displays on Apple products) came out, the quality of images on the web has changed. Beforehand, even though we had a variety of device sizes, the DPI has always been the same. This is no longer the case and it is very likely that the current resolution/pixel density on retina devices will not be the only one.

We should be ready and able to support the current resolutions as well as any others that manufacturers may use in the future.

Mobile-first and desktop-first responsive design

A common approach in sites that cater to a wide range of devices using a single codebase is a “mobile-first” development pattern—starting with a simple, linear layout and increasing layout and functional complexity for larger screen sizes using media queries.

“Desktop-first” responsive design takes the opposite approach and starts from the desktop design and simplifies it using media queries to support small displays. Authors retrofitting existing sites often take a desktop-first approach out of necessity because changing to a mobile-first approach would be a significant undertaking.

These two approaches require that a solution for images support the following:

Relative Units

A common practice in creating flexible layouts is to specify the size values in media queries as relative units: em, rem, vw/vh etc. This is most commonly seen using ems in order to reflow layouts based on users’ zoom preferences, or to resize elements through JavaScript by dynamically altering a font-size value.

Matching image source breakpoints to design breakpoints

Web authors would like to be able to optionally match the breakpoints for images to the breakpoints that they have defined in their responsive designs. Being able to match the breakpoints ensures that images are operating under the same rules that define the layout. It also helps authors verify their approach by ensuring that the same viewport measurement tests are being used in both HTML and CSS.

This desire is a facet of the two preceding use cases (mobile/desktop-first responsive design and relative units). If a breakpoint in the design is defined as:

@media screen (max-width: 41em) {}

Then web authors would like to be able to define a similar breakpoint for images at a max-width of 41em and not have to translate that measurement into another unit like pixels even if it is possible to calculate that measurement:

When debugging a document, if the author cannot specify breakpoints for images in the same manner that they are defined for the design, authors will need to convert the breakpoints back to the values specified in the layout in order to verify that they match. This increases authoring time and the likelihood that math errors on the part of authors (possibly due to a different rounding scheme in a particular user agent) cause unexpected behavior.

Mobile Networks

It should be noted that many devices are used on mobile networks which are often very slow or exhibit high latency. Often times conferences suffer from slow networks as well due to many users attempting to use a single network connection simultaneously. Many people also have very slow or erratic connections in their homes and workplaces. While it may not be possible for a solution to be based on bandwidth, anything that can be done to reduce latency and HTTP requests should be done.

Allowing authors to specify different images for different viewport sizes and display densities is one step towards providing a better experience on slow networks. In the future, user agents may be able to select different images based on network speed or user preference.

Zooming

Images blur when the user resizes the page. Users may zoom an image in order to see more detail. In these cases, user agents could select a higher-resolution version of an image to display.

It's not clear whether the picture element is prescriptive (i.e. the user agent MUST show a particular image given certain device properties) or suggestive (i.e. the user agent has control over picking the best image).

Art Direction

Web authors often want to provide different versions of the same image at different sizes depending on the viewport.

A simple example of this would be changing the crop of an image based on display area:

Examples: Large photo of Obama at a Chrysler plant vs. tighter cropped thumbnail

A more complex example that changes orientation of the image, crop, and how text flows around an image based on the size of the viewport:

Alternate Print Sources

Printed web documents generally have pixelated images due to printers having a higher DPI than most images currently served on the web. Defining higher resolution images for printing would increase the abilities of web authors to define great printed versions of their documents. For example, a photo sharing site could provide a bandwidth-optimized image for display on screen, but a high-resolution image for print.

Gray Scale and High Contrast Modes

Displaying a color image on a monochrome display does not always work well, as two different colors of similar luminosity would be impossible to distinguish in a monochrome environment.

Microsoft is proposing a media query which lets you detect that the user agent has been put in high contrast mode (for accessibility reasons), and that the content should follow along [http://msdn.microsoft.com/en-us/library/windows/apps/hh465764.aspx]. Being able to switch images based on high contrast mode would be nice.

Extracted from WhatWG mailing list thread.

Support the DRY Principle (Don't Repeat Yourself)

If the same image is used multiple times on a single page, it would be useful to define the resource selection in a single place in the document and have this affect all instances of the image.

Pre-existing Polyfills

Acknowledgements

TODO: add thanks