Start Using SRCSET IMG Attribute For Serving Device Specific Images

The article, JavaScript Low Resolution Image Replacer, discussed a JavaScript solution for replacing low resolution images with higher resolution ones after the page finishes loading all the initial resources (thereby reducing the load time of your pages). But what about loading different image sizes based on the resolution of the user’s device. We could write a JavaScript solution for this (and some developers have), but HTML 5 already introduces the concept of <img srcset[2] to serve different images for different resolutions.

How do it…

Here is how to use srcset by specifying image widths:

<img src="<pathToImages>/image-small.png"
     srcset="<pathToImages>/image-medium.png 500w, <pathToImages>/image-large.png 1000w"
     alt="my image">

Here is how to use srcset by specifying pixel density (eg. non-retina == 1x):

<img src="<pathToImages>/image-normalRez.png"
     srcset="<pathToImages>/image-highRez.png 2x"
     alt="my image">

Both pixel density and pixel width can also be specified for srcset (the large image will only be shown on retina devices):

<img src="<pathToImages>/image-small.png"
     srcset="<pathToImages>/image-medium.png 500w, <pathToImages>/image-large.png 1000w 2x"
     alt="my image">

How it works…

In srcset, define any number of comma separated image URLs, each followed by a pixel width (w) descriptor and/or a pixel density (x) descriptor. If you don’t provide a width, the browser will assume infinity, and if you don’t provide a pixel density, then the browser will assume 1x. The browser uses the pixel density descriptor to determine if the image should be shown to the current device, and then calculates the best size image using pixel width descriptor. For example, assuming we have three images each twice as large as the previous (500, 1000, 2000) and a device with a 320 pixel screen width and 1x pixel density, then the following calculations are made[1]:

500 / 320 = 1.5625
1000 / 320 = 3.125
2000 / 320 = 6.25

The closest value larger than 1 (1x) is 1.5625, so the browser will use the smallest image. But, increasing the pixel density of the device to 2x (eg. a retina device) will cause the browser to use 3.125 for the same reason: 3.125 is the closest value larger than 2 (2x).

As you can imagine, like many HTML 5 features, this attribute probably doesn’t work on the browser that visitors to your site are using[3]. Fortunately, there is a good polyfill[4] to enable the feature in legacy browsers, so there is little reason not to begin using srcset today.

References

  1. Responsive Images: If you’re just changing resolutions, use srcset
  2. MDN: IMG Attribute SRCSET
  3. Can I use SRCSET
  4. Picture Fill - Image Polyfill

Network Information API Polyfill

One of the many new HTML5 APIs slowly being implemented by browsers is the Network Information API[2]. It exposes information about the type of network that the connecting device is using. In theory, this allows developers to optimize content around the connection speed of the user. However, as with most HTML5 APIs it is supported only by some browsers with/without prefixes, and has a legacy implementation, so a polyfill is useful when working with ...

Using jQuery Mobile With Django HTML5 Boilerplate

In the last article, I introduced the Django HTML5 Boilerplate package, which incorporates HTML5 Boilerplate into an easy to use Django package. Today’s article will take that one step further by showing how to use DH5BP to create a base template for your HTML 5 mobile app. We will be using jQuery mobile and the jQuery Widget for Showing Add To Home Message on iOS.

Getting ready

Install Django ...

Django HTML5 Boilerplate

I am a big fan of the HTML 5 Boilerplate project, and in that past, I have made some half-hearted attempts to incorporate it into a reusable Django module. I think the problem with my attempts and the other attempts that I have found recently on the web is that nobody automated the process of converting H5BP into a django module, so each time H5BP is updated (which happens frequently), somebody has to ...

jQuery Widget for Showing Add To Home Message on iOS

I recently wrote an article discussing Making HTML5 Apps Appear Native on iOS where I mentioned building a widget for telling the users to add your application to the home screen. This article introduces a simple jQuery plugin that can be used to show the Add to Home message on an iOS device. It is fully customizable, but looks good right out of the box.

Getting ready

Download the plugin at the

Rotating Multi-State Button

One of the designers at Ariba, recently came up with a design that needed a tri-state button to govern toggling some content. We came up with an interesting solution where the button has three colors on the outside, with the bottom color governing the content being shown. I am not sure if the widget will survive a usability study, but I put together a proof of concept using HTML 5 technologies and wanted to share ...

Cross Browser and Legacy Supported RequestFrameAnimation

There is an awesome new feature in HTML 5, window.requestAnimationFrame, which tells the browser that you wish to perform an animation and requests that the browser schedule a repaint of the window for the next animation frame1. This allows you to do animations more efficiently and without using setInterval or series of setTimeout function(s). I have created a Demo that attempts to use the browser’s requestAnimationFrame side-by-side with the legacy polyfill executing.

Article ...

Legacy Support for HTML 5 Forms

As discussed in the article, HTML 5 Feature Detection Using Modernizr, HTML 5 contains many new input types and attributes. While, most new browsers support them (although often imperfectly), the majority of people still use browsers that do not. However, we can use JavaScript to provided legacy support for the new HTML 5 input attributes and types in browsers that lack support. Toadys article introduces a widget for the YUI 3 gallery, that emulates ...

HTML 5 Feature Detection Using Modernizr

HTML 5 is probably the most exciting improvement to come to the web, since the popularization of AJAX. However, as shown by last weeks article (HTML 5 Forms), only the most cutting edge browsers support many of the new HTML 5 features. To progressively enhance a site for HTML 5 enabled browsers, developers need a way to detect if the browser supports a desired HTML 5 feature. Enter Modernizr by Faruk Ates ...

HTML 5 Forms

HTML 5 is quickly gaining in popularity and is appearing more frequently across the web. While you may hear about the awesomeness of HTML5s new JavaScript and CSS features, the actual HTML improves are often missed. Lets look at the thirteen new form field types and some of the more useful new form field attributes. Not all browsers support HTML 5 forms yet, and those that dont will render the new input types as text ...