Every responsive website development tutorial recommends using the display:none CSS property to hide content from loading on mobile browsers so the website load
HTML5 tag will help you to resolve the right image source depending on the screen width
Apparently the browsers behaviour hasn't changed much over the past 5 years and many would still download the hidden images, even if there was a display: none property set on them.
Even though there's a media queries workaround, it could only be useful when the image was set as a background in the CSS.
While I was thinking that there's just a JS solution to the problem (lazy load, picturefill, etc.), it appeared that there's a nice pure HTML solution that comes out of the box with HTML5.
And that is the tag.
Here's how MDN describes it:
The HTML element is a container used to specify multiple elements for a specific contained in it. The browser will choose the most suitable source according to the current layout of the page (the constraints of the box the image will appear in) and the device it will be displayed on (e.g. a normal or hiDPI device.)
And here's how to use it:
The logic behind
The browser would load the source of the img tag, only if none of the media rules applies. When the element is not supported by the browser, it will again fallback to showing the img tag.
Normally you'd put the smallest image as the source of the and thus not load the heavy images for larger screens. Vice versa, if a media rule applies, the source of the will not be downloaded, instead it will download the url's contents of the corresponding tag.
Only pitfall here is that if the element is not supported by the browser, it will only load the small image.
On the other hand in 2017 we ought to think and code in the mobile first approach.
And before someone got too exited, here's the current browser support for :
Desktop browsers
Mobile browsers
More about the browser support you can find on Can I use.
The good thing is that html5please's sentence is to use it with a fallback. And I personally intend to take their advise.
More about the tag you can find in the W3C's specification. There's a disclaimer there, which I find important to mention:
The picture element is somewhat different from the similar-looking video and audio elements. While all of them contain source elements, the source element’s src attribute has no meaning when the element is nested within a picture element, 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 element that enables it to choose from multiple URLs.
So what it says is that it only helps you improve the performance when loading an image, by providing some context to it.
And you can still use some CSS magic in order to hide the image on small devices:
Thus the browser will not display the actual image and will only download the 1x1 pixel image (which can be cached if you use it more than once). Be aware, though, that if the tag is not supported by the browser, even on descktop screens the actual image won't be displayed (so you'll definitely need a polyfill backup there).