An intro to responsive image optimization with HTML5 and Intersection Observer

Riccardo Canella
We’ve moved to freeCodeCamp.org/news
5 min readMay 29, 2018

--

Images often account for most of the downloaded bytes on a web page and often occupy a significant amount of visual space. As a result, optimizing images can frequently yield some of the largest byte savings and performance improvements for your website. The fewer bytes the browser has to download, the less time the user will have to wait for the render of useful content on the screen.

Image optimization is both a science and an art. It is a science because there are many well developed techniques and algorithms that can significantly reduce the size of an image. It is an art because there is no one definitive answer.

But how we can optimize our images from mobile form in addition to using image magic or any other web optimization tool?

The img element

The HTML5 <img> element has been designed to give the developer the ability to optimize the images according to the screen resolution. This is done through two attributes, srcset and sizes. With a very simple syntax, you can instruct the browser to decide which one of the different image sizes are needed:

<img srcset="the-death-star-320w.jpg,
the-death-star-480w.jpg 1.5x,
the-death-star-640w.jpg 2x"

--

--

Riccardo Canella
We’ve moved to freeCodeCamp.org/news

Riccardo Canella @ricanella92 Love #basket, #bike and #HIMYM since my childhood. #Fullstack #Javascript addicted - https://canellariccardo.it