html srcset always show bigger image

29 views Asked by At

I'm trying to use srcset but they don't work

here is my code

<img style="width: 100%"
     src="https://fakeimg.pl/400x267"
     srcset="https://fakeimg.pl/400x267 400w,
                https://fakeimg.pl/825x550 800w"
     alt=""
     title="">

but at any size of view port I have 825x550 size image

1

There are 1 answers

2
Simon Weber On

From MDN:

If the srcset attribute uses width descriptors, the sizes attribute must also be present, or the srcset itself will be ignored.

You need to give a sizes attribute like so:

 sizes="(max-width: 600px) 400px,
         800px"

Further information on this here.