Picturefill
A responsive image polyfill
- Officially endorsed by the RICG + Officially endorsed by the RICGThe picture
element, srcset
and sizes
attributes, and associated features allow web developers to deliver an appropriate image to every user depending on a variety of conditions like screen size, viewport size, screen resolution, and more. Picturefill enables support for the picture
element and associated features in browsers that do not yet support them, so you can start using them today!
Contributing, Bug Reports, and More
Downloading Picturefill
- Picturefill Version 3.0.0 (RC 1)
-
Version 3 is a full rewrite of the codebase featuring optimized performance, better emulation of native behavior, and new spec-based parsers. It also handles some of the quirks, shortcomings, and edge cases related to first-generation native implementations. For IE 9 and earlier, version 3 includes a small shim that polyfills the use of common media conditions (min-width
, max-width
, min-height
, max-height
). If you need old IE support for other media conditions, such as orientation
or aspect-ratio
, please additionally include the matchMedia polyfill. Feedback on this release candidate is highly welcomed—if you encounter any problems, please file an issue on GitHub.
Picturefill Version 3.0.0 (Release Candidate 1)
+ +Version 3 is a full rewrite of the Picturefill codebase, featuring optimized performance, better emulation of native behavior, and parsers that adhere much more closely to the specification. It also handles many of the quirks, shortcomings, and edge cases related to first-generation native implementations.
+ +Feedback on this beta release is highly welcomed—if you encounter any problems, please file an issue on GitHub.
-
picturefill.js
@@ -73,7 +76,7 @@
Picturefill Version 2.3.1 (Stable)
Getting Started with Picturefill
-For basic usage of Picturefill: download one of the files listed above and reference it from the
+head
section of your HTML document with the following code:To start using Picturefill download one of the files listed above and reference it from the
head
section of your HTML document with the following code:
@@ -93,7 +96,7 @@<script src="picturefill.js"></script>
Getting Started with PicturefillMarkup Patterns
-Once you've included picturefill.js, you can start adding responsive image elements to your site. Picturefill adds support for both the
+picture
element and also new responsive attributes to theimg
element.Once you've included picturefill.js, you can start adding responsive image elements to your site! Picturefill adds support for the the entire suite of responsive image solutions, including the
picture
element and newimg
element attributes.There are a number of different use cases addressed by some combination of features from the responsive images specification—here are some of the most common ones:
@@ -112,17 +115,17 @@Using the `srcset` attribute
-<img srcset="examples/images/large.jpg 1x, examples/images/extralarge.jpg 2x" alt="…">
Note that the
-2x
source will be shown at the inherent width of the1x
source. The two sources will occupy the same space in your layout, but the2x
source will be at double the pixel density. This only applies to the natural size of theimg
—any sizing changes done in CSS are unchanged.Here's how that renders on your display:
-Using the `srcset` & `sizes` attributes
-The
+srcset
andsizes
syntaxes are used to provide the browser with a list of image sources that are identical apart from their size (same aspect ratio, same focal point) and how they’ll be displayed, then allow the browser to choose the source best for the user’s current viewport size, display density, and the size of that image in the page layout.The
-2x
source will be shown at the inherent width of the1x
source—so the two sources will occupy the same space in your layout, but the2x
source will be displayed at double the pixel density. This only applies to the natural size of theimg
—resizing the image via CSS will behave as expected.The
+sizes
syntax is used to define the spaces your image will occupy in your layout.srcset
then defines a list of images and their inherent widths. This allows the browser to choose the smallest appropriate source for the size available in that part of the layout, rather than the viewport size alone.Modern browsers that support
-srcset
natively may select a cached file that meets the minimum media condition, even if it is “overkill” for the current media condition. For example, a2x
file may be shown on a1x
device, if that2x
file is already in the cache—there’d be no reason to make an additional request when the user will see no discernable difference, after all. This is typically encountered only on sites with multiple versions of the same image displayed in multiple elements at different sizes (like our demo page). The occasional selection of "oversize" resources—depending on the cache—is currently an expected behavior in native implementations and you may encounter it during testing.It's beyond the scope of this guide to get into much detail about how to use the new
+srcset
&sizes
attributes, so we'd recommend reading the following post by Eric Portis: Srcset and Sizes. Read that first, then check out the demos below!Eventually
+ +srcset
will be extended to allow the browser to override requests for higher-resolution options based on a bandwidth limitations or a user preference (see #9 in the Responsive Images Use Cases and Requirements).Using the `srcset` & `sizes` attributes
+The
srcset
andsizes
syntaxes are used to provide the browser with a list of image sources that are identical apart from their size (same aspect ratio, same focal point) and how they’ll be displayed, then allow the browser to choose the source best for the user’s current viewport size, display density, and the size of that image in the page layout.<img sizes="(min-width: 40em) 80vw, 100vw" @@ -137,16 +140,15 @@
Using the `srcset` & `sizes` attribut srcset="examples/images/medium.jpg 375w, examples/images/large.jpg 480w, examples/images/extralarge.jpg 768w" alt="A giant stone face at The Bayon temple in Angkor Thom, Cambodia"> Standalone
srcset
andsizes
demo +The
+ +sizes
syntax is used to define the spaces your image will occupy in your layout.srcset
then defines a list of images and their inherent widths. This allows the browser to choose the smallest appropriate source for the size available in that part of the layout, rather than the viewport size alone.It's beyond the scope of this guide to get into much detail about how to use the new
srcset
&sizes
attributes so we’d highly recommend reading the following post by Eric Portis: Srcset and Sizes. Keep in mind that the same caching behavior is applied to this usage ofsrcset
, as well.Using the `picture` element
The
picture
element is used when you need explicit control over which source is shown at set viewport sizes.The
-picture
element requires a little more markup than the example above, but it allows you to use features like CSS3 Media Queries to pair image source with varying sizes, zoom levels, and aspect ratios with the layout conditions in your designs. It should not, however, be used to serve radically different image sources—all sources must be described by thealt
attribute of the innerimg
.Your
- -picture
element should contain a series ofsource
elements followed by animg
element. Eachsource
element must have asrcset
attribute specifying one or more image url sources (which can use expanded srcset syntax if desired for resolution switching), and theimg
element should have a srcset attribute for fallback purposes as well (some browsers like Android 2.3's won't see thesource
elements). Additionally, you may add amedia
attribute containing CSS3 Media Queries, and/or asizes
attribute to pair withsrcset
.Here's one with
-srcset
andmedia
. The firstsource
with amedia
attribute that matches the user’s context will determine the src of theimg
element at the end, so you’ll want to present larger options first when usingmin-width
media queries (like in the examples below), and larger options last when usingmax-width
media queries.<picture> <source srcset="examples/images/extralarge.jpg" media="(min-width: 1000px)"> <source srcset="examples/images/art-large.jpg" media="(min-width: 800px)"> @@ -162,6 +164,9 @@
Using the `picture` element
Standalonepicture
demo +Your
+ +picture
element should contain a series ofsource
elements followed by animg
element. Eachsource
element must have asrcset
attribute specifying one or more image url sources (which can use expanded srcset syntax if desired for resolution switching), and theimg
element should have a srcset attribute for fallback purposes as well (some browsers like Android 2.3's won't see thesource
elements). Additionally, you may add amedia
attribute containing CSS3 Media Queries, and/or asizes
attribute to pair withsrcset
.The first
source
with amedia
attribute that matches the user’s context will determine the src of theimg
element at the end, so you’ll want to present larger options first when usingmin-width
media queries (like in the examples below), and larger options last when usingmax-width
media queries. Since these image sources are meant to align with layout breakpoints,srcset
’s caching behavior and the potential for a bandwidth or user preference override do not apply here.Supporting Picture in Internet Explorer 9
While most versions of IE (even older ones!) are supported well, IE9 has a little conflict to work around. To support IE9, you will need to wrap a
@@ -175,8 +180,7 @@video
element wrapper around the source elements in yourpicture
tag. You can do this using conditional comments, like so:Supporting Picture in Internet Explorer 9
`media` and `srcset` syntax:
- -The
+1x
,2x
syntax insrcset
acts as a shorthand for more complex resolution media queries. When natively supported,srcset
will allow the UA to override requests for higher-resolution options based on a bandwidth limitations or a user preference (see #9 in the Responsive Images Use Cases and Requirements).The
1x
/2x
syntax can be used withinsource
elementsrcset
attributes as a shorthand for more complex resolution media queries.<picture> <source srcset="examples/images/large.jpg, examples/images/extralarge.jpg 2x" media="(min-width: 800px)"> @@ -192,7 +196,7 @@
-`media` and `srcset` synt Standalone extended
picture
demoThe `type` attribute in `picture`
-The
+types
attribute is used to send an alternate image source format only to browsers that support that format, and a fallback source to browsers that do not.The
types
attribute is used to send an alternate image source format only to browsers that support that format, and a fallback source to browsers that do not. Unlike existing solutions, thepicture
element makes these negotiations using a single request.Picturefill supports SVG and WebP as part of its core, but the following MIME types can be used via the “typesupport” plugin:
-
@@ -211,8 +215,8 @@
- Elements: An array of
img
elements you'd like picturefill to evaluate. The Default value foroptions.elements
is allimg
elements in the page that have asrcset
attribute or have apicture
element as a direct parent. @@ -244,26 +248,33 @@Picturefill function options
The `type` attribute in `picture`
Here's how that renders in your browser:
Standalonetype
attribute demo @@ -228,7 +232,7 @@The Picturefill function
Picturefill function options
-When running the
+picturefill()
function, you can pass options specifying the following settings:When running the
picturefill()
function, you can pass options specifying the following configuration options:Picturefill source selection option
-By default Picturefill tries to mimic the resource selection algorithm of Chrome (except that Picturefill, unlike the browser, doesn't know which image files are already cached). Picturefill 3.0 also comes with a new image custom source selection algorithm unique to Picturefill called
+saveData
. ThesaveData
algorithm weights the resource selection algorithm towards smaller image candidates on -very high dppx devices (3dppx +), to conserve bandwidth.Source selection algorithm option
-To use
saveData
, create a picturefillCFG array that runs before the main plugin: +By default Picturefill attempts to mimic the resource selection algorithms current native implementations, but Picturefill 3 also includes an optional and experimental custom source selection algorithm. The
+ +saveData
algorithm skews the resource selection algorithm toward smaller image candidates(2x
on extremely high dppx devices (3x
and up), to conserve bandwidth in situations where there may be little or no visual difference between sources.To use
saveData
, create apicturefillCFG
array that runs before the main plugin://generating the config array window.picturefillCFG = window.picturefillCFG || []; picturefillCFG.push([ "algorithm", "saveData" ]);
Developer feedback on this new algorithm is welcome, especially if you've got some hard data. +
Developer feedback on this new algorithm is highly welcomed. We’d love to see some data around potential bandwidth savings, and this data may go on to inform native responsive image implementations in the future.
Browser Support
-Picturefill supports a broad range of browsers and devices, provided that you stick with the markup conventions documented above.
+Picturefill supports a broad range of browsers and devices, provided that you stick with the markup conventions documented above.
+ + + -Support caveats to keep in mind
+Support caveats
Picturefill is tested broadly and works in a large number of browsers. That said, it does have some browser support considerations to keep in mind:
-
+
- For IE 9 and earlier, version 3 includes a small shim that polyfills the use of common media conditions (
min-width
,max-width
,min-height
,max-height
). If you need old IE support for other media conditions, such asorientation
oraspect-ratio
, please additionally include the matchMedia polyfill.
+
+ - Picturefill 3 includes a small shim that polyfills common media conditions for IE9 and earlier (
min-width
,max-width
,min-height
, andmax-height
). If you need old IE support for other media conditions, such asorientation
oraspect-ratio
, please additionally include the matchMedia polyfill.
+
- JS-Disabled Browsers only see alt text: When using the
picture
element, non-picture
supporting browsers will only seealt
attribute text as a fallback when JavaScript fails or is disabled. This is because anynoscript
-based workarounds (such as the one used in Picturefill version 1) will cause future browsers that support thepicture
element to show two images instead of one when JavaScript is off. Unfortunately, adding asrc
attribute with an external source to theimg
element in yourpicture
element isn't a good workaround either, as any browser that exists today will fetch thatsrc
url even if it is not going to be used (which is wasteful), and an emptysrc
can result in unexpected requests. For valid markup, the shortest possible value forsrc
(without firing anonerror
event or a potential request) issrc="data:image/gif;base64,R0lGODlhAQABAAAAADs="
- Elements: An array of
- Temporary extra HTTP Requests for
picture
usage in some browsers: In browsers that natively supportsrcset
but do not yet support thepicture
element, users may experience a wasted HTTP request for eachpicture
element on a page. This is because the browser's preparser will fetch one of the URLs listed in thepicture
element's childimg
'ssrcset
attribute as soon as possible during page load, before the JavaScript can evaluate the potentialpicture
source
elements for a better match. This problem will only affect browsers that have implementedsrcset
but notpicture
, which will hopefully be short-lived.