Hello is this correct to show srcset in img tag?
<img src="png-shat-should-be-showed-when-under-400px.png" alt="image" srcset="png-shat-should-be-showed-when-under-400px 400w, png-shat-should-be-showed-when-over-400px.png 400w" sizes="(max-width: 400px) 400px, 50vw">
I want to one img when under 400px viewport, and second port when viewport is over 400px.
lets say i have 2 image
adding dimensions in tag
1. png-shat-should-be-showed-when-under-400px.png
2. png-shat-should-be-showed-when-over-400px.png
2
Answers
Yeah that seems correct comparing to https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-srcset
Your tag should look like this:
In the
srcset
attribute you would use the intrinsic size of the image. That means the actual true and correct width of the image. Thesrcset
will not resize your image for you. Remember that in thesrcset
you will usew
and notpx
.You need to understand media queries to understand the
sizes
attribute. Themax-width
shown in the media query defines the breakpoint where the browser will alternate the images. If your viewport width is smaller than this breakpoint it will show the image that is smaller and if your viewport width is larger than this breakpoint it will show the larger image.