Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
65 lines (45 sloc) 2.28 KB

Srcset Polyfill

This implementation does not now match the updated spec and will also fail feature detection tests in current browsers. Unfortunately I do not have time to maintain the repo but if someone is willing to take over I am happy to hand over the reins.

An image srcset polyfill that provides fallback behaviour for browsers that do not support the srcset attribute.

Supports the current syntax as defined below.


Just reference the script from your webpage and the script will automatically do the rest. For images loaded dynamically call window.srcset() to force the browser to set the correct source for those images.


<img src="mobile.jpg" 
     srcset="mobile.jpg 480w, mobile-hd.jpg 480w 2x, tablet.jpg 768w, desktop.jpg 979w" 
     alt="srcset example" />


Browser support is IE8+ and all other modern browsers.


Using the polyfill will result in 2 requests per element if the image is to be changed. A possible workaround would be to use a base 64 encoded image as the default src. This however, will not gracefully fallback for browsers where JavaScript is turned off. It may also be an issue for SEO but this is unknown at this time.

26byte gif with no color palette.
<img src="data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs="
     alt="no color-palette 1x1 gif" />

This image could be hidden easily enough using CSS.

img[src="data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs="] {
    visibility: hidden;

Using about:blank or an empty src src="" is invalid as they both violate section 4.8.1 of the HTML5 specification:

The src attribute must be present, and must contain a valid non-empty URL potentially surrounded by spaces referencing a non-interactive, optionally animated, image resource that is neither paged nor scripted.

Discussion regarding this problem is encouraged.


Licenced under the Apache 2.0 licence.