htmlcssimagebrowsersrcset

Correct use of sensitive retinal images with "srcset"


I could not get the correct information or There is quite a lot of dirty information on this subject. It took me a while to understand the relationship between srcset and sizes and I was able to learn by trial and error.

If there is anything wrong with what I have understood so far, please correct it. Because we haven't started "Retina" yet.

Let's continue with "srcset"

<img srcset="./1000x1000.jpg 1000w, ./800x800.jpg 800w" sizes="(max-width:800px) 100vw, 800px" />

If we interpret the above scenario,

If I am lacking information up to this point, please correct it.

What about the retina?

In the scenario above, the browser preferred the most suitable image. But is it sensitive to "Retina" displays?

Ok now a new example,

<img srcset="./1000x1000.jpg 1000w, ./500x500.jpg 500w" sizes="(max-width:500px) 100vw, 500px" />

In the example above, we have defined two images that are 1000 and that is exactly 500px wide.

If we interpret the above scenario,


Solution

  • Your assumptions look mostly correct to me (not sure about the lack of materials).

    Although few points worth mentioning:


    Regarding your questions,

    Q: In a situation where it should prefer 500px, is it capable of automatically choosing "1000px" which is the "retina" equivalent of this?

    Yes. In your example it would automatically use 1000px image on @2x displays.

    Q: Or do we need to define it as "./1000x1000.jpg 500w 2x" via "srcset"?

    That's not valid HTML.

    Q: One last question, "750x750.jpg 500w 1.5x", why isn't anyone using this command? As far as I know, Android devices have a ratio of "1.5:1". But I see it is set to only "2x" in each example/guide.

    Using both descriptors is not valid as described before. Using 1.5x as one of the possible image densities is valid. An example of 1.5x is even listed in this MDN Web Docs article


    You could also experiment yourself by looking at the Network tab in developer tools, or use JS document.querySelector('img').currentSrc to check which images were actually requested. Additionally the MDN article provided above seems useful and run some of your examples through an HTML Validator.