responsive images srcset not working

后端 未结 7 1610
小蘑菇
小蘑菇 2020-11-28 07:01

I\'ve been trying to implement the new srcset approach to responsive images, using the following HTML

        

        
7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 07:37

    We faced the same issue while working with srcsets for the first time in our project. After investing some time, one of our team members was finally able to crack the issue. The reason why your code doesn't work is because of the mismatch of the sizes in srcset and sizes attribute. You have to provide double the width in the srcset for the browser to be able to match it with the width in sizes. For example,

     Sample
    

    Here's a sample working code for you to try out.

    Make sure you open it in incognito (since once the browser loads higher resolution image, it will not go back lower resolution image) and run the code first in browser size less than 1920px to see the smaller resolution image(sunset) and once you start increasing the browser size, you will be able to see the larger image (flower).

提交回复
热议问题