What the Hell! Can anyone help me with scrset and sizes in HTML?
(Source/Credits: https://dev.to/francoscarpa/what-the-hell-can-anyone-help-me-with-scrset-and-sizes-in-html-3i3d)
I'm trying to figure out how srcset and sizes work in HTML, but I'm not able to understand why a spec...
I'm trying to figure out how srcset
and sizes
work in HTML, but I'm not able to understand why a specific image instead of another one is chosen. Can anyone help me? I don't know any other place to ask.
Comments section
phacus
•May 1, 2024
I don't know if you're being serious or not.
The best example is responsive images, you can see an example at MDN Docs
The idea is... you have a set of sources (in this case, images in different sizes) that you control with CSS (
media queries
).But let's use the example from MDN:
```
```
Notice the different images in the
set
ofsrc
?Well, that's your
srcset
!In this example the author is using the CSS queries within the
<img>
tag with the attributesizes
.Let's transform it into a tiny CSS:
``` img { width: 800px; }
@media screen and (max-width: 480px) { img { width: 440px; } }
@media screen and (max-width: 320px) { img { width: 280px; } }
```
I'm using
img
as a selector.Another thing, you might ask:
"Why is the author setting the size to
280px
and the image has a width of 320px"? That's - IMO - a way to avoid distortion (I can imagine some others explanations, but that's is the most simple).Anyway, take a few minutes to read the link I sent you and search the web for "responsive image srcset example" and you'll get some way better explanation that I just gave you!
(I'm on mobile and kinda in a hurry, but feel free to hit me up!)
I hope I cleared the path a little.
francoscarpa Author
•May 1, 2024
I'm serious. For example:
```
```
Why
rufy2.jpg
is shown when the viewport is 1200 pixels wide?