After watching a recent video posted by Google Chrome Developers on YouTube, I wanted to blog about using the “srcset” attribute in the “img” tag in your HTML code to make more web developers aware of this easy-to-use/add code that will help your website. Definitely make sure to watch the video and check out the example HTML code using “srcset” in an “img” tag all down below.
Reduce image size: use srcset to automatically choose the right image (from Google Chrome Developers YouTube channel)
So to implement the “srcset” attribute into your “img” tag all you need to do is use the HTML as in the below example.
<img src="images/default.jpg" srcset="images/default-200.jpg 200w, images/default-400.jpg 400w, images/default-600.jpg 600w">
As stated in the above video, the web browser will do all of the hard work as it will decide what image best fits on the website and if the web browser doesn’t support “srcset” then it will default to the image in the “src” attribute. Need more information about “srcset”? Then check out MDN’s documentation on responsive images.
July 2022 update
Jashele, of devjashele.tech, a developer I follow on LinkedIn and Twitter, wrote an article, Simple Ways to Improve Website Image Performance, and also posted a YouTube video (see below) talking about responsive images using “srcset” step-by-step. Make sure to check out Jashele’s article and video, and give her a follow.