To the extent possible under law, the editors have waived all copyright and related or neighboring rights to this work. In addition, as of 19 December 2013, the editors have made this specification available under the Open Web Foundation Agreement Version 1.0, which is available at http://www.openwebfoundation.org/legal/the-owf-1-0-agreements/owfa-1-0. Parts of this work may be from an existing specification document. If so, those parts are instead covered by the license of the existing specification document.
The picture
element is intended to give authors a way to control which image resource a user agent presents to a user, based on media query and/or support for a particular image format.
The primary purpose of the picture
element is to address use cases [respimg-usecases] that are left unaddressed by the srcset
attribute [HTML]; the most important being art direction
. Other use cases, such as matching media features and media types
and matching on supported image formats
, are also addressed by this specification.
This specification provides developers with a means to declare multiple
sources for an image, and, through [MEDIAQ] (CSS
Media Queries), it gives developers control as to when those
images are to be presented to a user. This is achieved by introducing
the picture
element to HTML, and by enhancing the
source
element to support specifying multiple source urls.
By relying on [MEDIAQ], a user agent can respond to changes in the browsing environment by selecting the image source that most closely matches the browsing environment - thus embodying a technique known as responsive web design directly in the HTML markup. Media features that a user agent can potentially respond to include, but are not limited to, pixel widths and heights, and pixel densities, as well as environmental lighting conditions, changes in orientation, and changes in media type such as going from screen to print.
The picture
element remains backwards compatible with
legacy user agents by degrading gracefully through fallback
content (i.e., through the img
element) while also
potentially providing better accessibility than the existing
img
element.
This specification also defines the HTMLPictureElement
interface, through which developers can interact with
picture
elements.
picture
The picture
element is intended primarily for art
directing
graphical content. For cases where a single image
source is available, and where no responsive adoption is needed,
authors are encouraged to use the img
element instead.
picture
element
with the source
element, while also providing fallback
content for legacy user agents through the img
element.
<picture> <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="" width="500" height="500"> <p>Accessible text</p> </picture>
picture
element can be used
with the srcset
attribute to provide a
range of sources suitable for a given media query:
<picture> <source media="(min-width: 45em)" src="large-1.jpg, large-2.jpg 2x"> <source media="(min-width: 18em)" src="med-1.jpg, med-2.jpg 2x"> <source srcset="small-1.jpg, small-2.jpg 2x"> <img src="small-1.jpg" alt="" width="500" height="500"> <p>Accessible text</p> </picture>
<figure> <picture> <source media="(min-width: 45em)" src="large-1.jpg, large-2.jpg 2x"> <source media="(min-width: 18em)" src="med-1.jpg, med-2.jpg 2x"> <source srcset="small-1.jpg, small-2.jpg 2x"> <img src="small-1.jpg" alt="" width="200" height="200"> </picture> <figcapture>A figure of a fox jumping over a lazy box.</figcaption> </figure>
Need a bunch more examples + explanation.
srcset
The srcset
attribute allows authors to define various
image resources and “hints” that assist a user agent in determining
the most appropriate image source to display. Given a set of image
resources, the user agent has the option of either following or
overriding the author’s declarations to optimize the user experience
based on criteria such as
display density, network connection type, user preferences, and
so on.
The picture
element defines conditions under which the
use agent needs to follow the author’s explicit instructions when
selecting which resource to display. This includes image sources with
inherent sizes designed to align with layout variations specified in
CSS media queries (see: design
breakpoints, and relative
units) or content variations for increased clarity and focus
(i.e., art
direction
) based on the client’s viewport.
The proposed solutions are not mutually exclusive. They work together to address the complete set of use cases and requirements for responsive images (see [respimg-usecases]).
The following terms are used throughout this specification so they are gathered here for the readers convenience. The following list of terms is not exhaustive; other terms are defined throughout this specification.
The follow terms are defined by the [HTML] specification: fallback content, and valid media query.
Name: picture Categories: Flow content, Phrasing content, Embedded content, Palpable content Contexts: Where embedded content is expected Content Model: Zero or more source elements, followed by one img element. Attributes: Global attributes
The picture element is a container which provides multiples sources to its contained img element, allowing the displayed image to vary based on the density of the screen or other environmental factors expressed as media queries.
Note: picture is somewhat different from the similar-looking elements video and audio. While all of them contain source elements, the syntax of the src attribute is different when the element is nested within picture, and the resource selection algorithm is different. As well, the picture element itself does not display anything; it merely provides a context for its contained img that enables it to choose from multiple source urls.
A picture element is associated with a list of source sets, which is a list of zero or more source sets.
A source set is a set of zero or more image sources, optionally a source size list, and optionally a media query.
An image source is a URL, and optionally either a density descriptor, or a width descriptor.
A source size list is a list of zero or more pairs of a media query and a <length>, and optionally a default <length>.
This is meant to be the HTML integration point.
When asked to select an image source for a given img element el, user agents must do the following:
This just selects a single image and then sticks with it,
unlike CSS’s image(),
but similar to srcset
.
That okay?
When asked to update the list of source sets for a given picture element picture, user agents must do the following:
I’d like to allow individual sources to specify a type as well with a type() function, overriding the default type specified by the type attribute, but I’m keeping things simple for now.
src
Attribute When asked to parse a src attribute from an element,
parse the value of the element’s src
attribute with the following grammar:
<image-source-list> = <image-source># <image-source> = <url> [ <resolution> | <source-width> ]?
The above grammar must be interpreted per the grammar definition in [CSS3VAL]. For the purposes of the above grammar, the <url> production is simply any sequence of non-whitespace characters that does not end in a comma. The <source-width> production is a dimension with a unit of w. All other terminal productions are defined as per CSS.
If the value does not parse successfully according to the above grammar, return an empty source set.
Otherwise, let source set initially be an empty source set. For each <image-source> parsed, do the following:
Then return source set.
sizes
Attribute When asked to parse a sizes attribute from an element,
parse the value of the element’s sizes
attribute with the following grammar:
<source-size-list> = <source-size>#? <source-size> = <media-query>? <length>
The above grammar must be interpreted per the grammar definition in [CSS3VAL].
If the value does not parse successfully according to the above grammar, return an empty source size list.
Otherwise, let source size list initially be an emtpy source size list. For each <source-size> parsed, do the following:
An image source can have a density descriptor, a width descriptor, or no descriptor at all accompanying its URL. Normalizing a source set gives every image source a density descriptor.
When asked to normalize the source densities of a source set source set, user agents must do the following:
size descriptor / effective size
and a unit of x.
When asked to find the effective size from a source size list list, user agents must do the following:
Some UAs utilize a “preload scanner”, which is a simplified parser that runs ahead of the main HTML parser during the initial load of the page, looking for URLs that it can begin loading as quickly as possible.
The definition of the picture element is compatible with a preload scanner, at least for some media queries.
If a UA uses a preload scanner, it must recognize when a an img element is a child of a picture, and use the full algorithm to select an image source to determine which URL to pre-load, with the following caveats:
HTMLPictureElement
interfaceinterface HTMLPictureElement { };
We could maybe add some more things here, like a list of the relevant source elements and the active img element. Readonly? I dunno.
When a source element is a child of picture element, the syntax of its src attribute is changed. Instead of being a URL, it must instead match the following grammar:
<source-src> = <image-source>#
Additionally, the source element gains a sizes attribute, which contains a set of intrinsic sizes for the sources it provides. The sizes attribute is reflected by the sizes attribute on the element’s DOM interface.
partial interface HTMLSourceElement { attribute DOMString sizes; };
A complete list of participants of the Responsive Images Community Group is available at the W3C Community Group Website.
Contributions also from: David Newton, Ilya Grigorik and Leon de Rijke. Special thanks to Adrian Bateman for providing the group with guidance.
Conformance requirements are expressed with a combination of descriptive assertions and RFC 2119 terminology. The key words “MUST”, “MUST NOT”, “REQUIRED”, “SHALL”, “SHALL NOT”, “SHOULD”, “SHOULD NOT”, “RECOMMENDED”, “MAY”, and “OPTIONAL” in the normative parts of this document are to be interpreted as described in RFC 2119. However, for readability, these words do not appear in all uppercase letters in this specification.
All of the text of this specification is normative except sections explicitly marked as non-normative, examples, and notes. [RFC2119]
Examples in this specification are introduced with the words “for example”
or are set apart from the normative text with class="example"
, like this:
Informative notes begin with the word “Note”
and are set apart from the normative text with class="note"
, like this:
Note, this is an informative note.
No properties defined.
srcset
.
That okay?
↵