srcset
and sizes
do the job.
srcset
and sizes
do the job.
img src="mask.jpeg" srcset=" Optimized-300.jpg 300w, horizon_zero_dawn_thunderhawk-HD.jpg 1920w" sizes=" (max-width:400px) 200px, (min-width:400px) 1300px">
srcset
hold a list of different image resolutions files.next to them is resolution value-this is the 200px and 1300px above.this is exactly the part that tells the browser the different resolutions we have in the document (200px standard resolution for standard-image screens,1300px for Retina display screens).
srcset
is telling the browser this is the img url (Optimized-300.jpg
).and this is it's resolution and how fancy it is(200w
).incase screen size is not an issue browser will only look for screen resolution.then (x) descriptor is used like 1x,2x,.. to indicate img resolution degree to help the browser selecting one.
sizes
is a mix of two parts: first is media queries and second is a number that will help select the resolution and the size the image will be rendered on in the final stage(css also handel this step).one number with two jobs.
sizes
tells the browser if media query is true (max-width:400px) then render the image that has a resolution that fit with this size(200px).now the size will be consistent with the image resolution in the srcset
attribute.200px will fit the image resolution 300w.however size 301 for instance will not because we have no image resolution that much.the browser will choose the (1300w) resolution instead.and the image will be rendered with 200px width in the final stage.the last size value has no media query part only size part is a fallback option if no condition matches and works just like the size part only media query is missing-usually 100vw(100% viewport width) is good option-this is the default if no fallback specified.no srcsset no sizes effect.picture source media="(max-width: 552px)" srcset="hero-small.jpg"> source media="(max-width: 1062px)" srcset="hero-medium.jpg"> img src="hero-big.jpg" width="1500" height="400" alt="Hero"> picturemedia query will tell browser wich image source to use depending on the viewport-smartphones or desktops for instance.
srcset
and sizes
on img tag is different than using it on picture
tag.ok they both can render based on resolution (size is intrinsic with picture
and is set using sizes
attribute with img
tag) but using them with img
will let the browser do the math and select the best img for us.described all here.