The problem of responsive images has been on the minds of web developers for a long time. This is a task when you need to show a large image on large screens and a small image on small screens. This problem is solved through HTML attributes srcset and sizes, as well as the <picture>. Let’s consider them in more detail below.
srcset contains a set of image URLs so that the browser can choose the most suitable option depending on the screen parameters of the device from which the page is viewed.
You can specify one or more URLs in srcset.
In srcset it is implied to specify different sizes of the same picture. You can specify different ones, but the logic of its work implies the same picture.
Each line in the list must contain a URL and a descriptor. If no descriptor is specified, then the default is taken which is 1x.
The descriptor can be of two types:
A descriptor is an instruction to the browser. It helps the browser choose the correct URL from the set of images because the browser does not know anything in advance about the image located at that URL. Due to the descriptor, the browser understands what the width of the picture is or for which DPR it is suitable for.
The syntax of srcset is as follows:
<img srcset="image-1200.jpg"
src="full.jpg">
<img srcset="image-640.jpg 640w, image-960.jpg 960w, image-1200.jpg 1200w"
src="full.jpg">
<img srcset="image-640.jpg, image-1280.jpg 2x, image-1920.jpg 3x"
src="full.jpg">
How it works by example:
<img srcset="image-640.jpg 640w, image-960.jpg 960w, image-1200.jpg 1200w"
src="full.jpg">
In this example, the picture full.jpg will not be used because there is a srcset attribute — the browser will select the most suitable image from srcset
Which picture will the browser choose?
How it works by example:
<img srcset="image-640.jpg, image-1280.jpg 2x, image-1920.jpg 3x"
src="full.jpg">
Which picture will the browser choose?
DPR often reaches 4 on Samsung and other smartphones!
Examples to play with:
https://miliutin.ru/tests/srcset/2.html (without sizes) →
https://miliutin.ru/tests/srcset/1.html (with sizes) →
https://miliutin.ru/tests/srcset/4.html (with the sizes attribute) →
https://imagekitio.github.io/responsive-images-guide/srcset-density.html (DPR) →
If the image occupies only part of the screen width, using srcset without sizes is a bad idea. The browser does not know what the page layout looks like and will think that the image occupies the entire screen width. As a result, it will load a larger image than necessary.
Okay, what? The browser doesn’t know about the layout?
You might think that the browser can get the width of the image from CSS styles. But there is a caveat: at the time the IMG tag is parsed, the CSS styles are not yet processed and the page layout is not yet built. If the browser waits for all styles to be applied, this will delay the loading of images.
If no dimensions are specified in css for the image, then sizes will also limit the size of the image, as the width attribute does.
If we use the CSS property max-width: 100% for the picture, it overrides the value of the sizes attribute.
What does it mean? If the width of the picture specified in sizes is larger than the width of its parent element, then the width of the picture will be equal to the width of the parent element (due to max-width: 100%).
sizes is an addition to srcset that helps the browser choose the optimal image size.
sizes contains a list of elements (comma-separated), where each element describes the size of the image in relation to the width of the screen (viewport).
Using sizes gives the browser additional information to correctly select the appropriate image from srcset and start loading it as soon as it sees the <img> tag, without waiting for CSS styles parsing.
Syntax:
<img
srcset="small.jpg 300w,
medium.jpg 600w,
large.jpg 900w"
sizes="(max-width: 300px) 100vw,
(max-width: 600px) 50vw,
(max-width: 900px) 33vw,
900px"
src="image.jpg"
/>
Please note that the last size has no media condition — this is the default value. It will be used when none of the previous media conditions fit.
Be careful about the order of declaration of checks, because the browser ignores all subsequent checks after the first successful one.
Elements in sizes can have:
Consider this example:
<img
srcset="w-225.jpg 225w,
w-300.jpg 300w,
w-350.jpg 350w,
w-640.jpg 640w"
sizes="(max-width: 400px) 100vw,
(max-width: 700px) 50vw,
(max-width: 900px) 33vw,
225px"
src="women-dress.jpg"
>
Based on this data, the browser will select one of the images from the srcset set (including all DPR checks — see srcset description).
Here is an example: https://imagekitio.github.io/responsive-images-guide/srcset-sizes.html →
Here is a complete list of the possible units that can be used in the sizes attribute.
When the page loads, the browser starts loading images before loading and interpreting CSS and JavaScript. This technique reduces page load time by an average of 20%. But it is not suitable for creating responsive images, for this the srcset attribute was created.
For example, it would be impossible to load an<img> element, then determine the screen width using JavaScript and dynamically change the image URL. The original image would have already been loaded (or would have been sent for download) by the time its smaller version was loaded.
Link to the original article-https://wp-kama.com/2224/html-attributes-srcset-sizes-and-picture-tag →