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.
After watching the above video, I came across another recent YouTube video that had a conference talk focused on modern image delivery techniques and what can be done to images to speed to up websites. I thought the presenter had some good informative points that all web developers should know about.