The srcset and sizes attributes on an <img> element let the browser choose the best file from a list of candidates. You list the same image at several widths in srcset, describe how wide the image will be displayed in sizes, and the browser combines that with the viewport width and screen density to download only what it needs. This is the core of html srcset sizes responsive images.
Without them, a phone can download the same 2,400-pixel photo that a large desktop monitor needs. With them, each device fetches a file close to the size it will actually paint, which saves bandwidth and improves Largest Contentful Paint.
An image has a fixed number of pixels, but it can be displayed in a slot that is 320 CSS pixels wide on a phone and 1,000 on a desktop. On a screen with a device pixel ratio of 2, the browser needs twice as many image pixels as CSS pixels to look sharp.
If you ship one big file, small screens waste data. If you ship one small file, large or high-density screens look blurry. srcset supplies the options and sizes gives the browser the layout information it needs to choose, because the browser picks an image before CSS has finished laying the page out.
A width descriptor, written with a w suffix, tells the browser the intrinsic pixel width of each file. It is not a CSS size.
The src attribute is the fallback for browsers that do not understand srcset. The width and height attributes give the intrinsic aspect ratio so the page does not shift when the image arrives.
The sizes attribute is a comma-separated list of media conditions, each followed by the width the image will occupy. The browser uses the first condition that matches, and the last entry, with no condition, is the default.
In the example above, (min-width: 1000px) 800px, 100vw means: on viewports at least 1,000 pixels wide the image is 800 CSS pixels wide, otherwise it fills the viewport width. Values can use px, vw, em and calc(), for example calc(100vw - 2rem).
The browser multiplies the slot width by the device pixel ratio and picks the smallest candidate that is at least that wide. A 600 px slot on a 2x screen needs 1,200 pixels, so it would choose the 1600w file here.
When you use width descriptors but leave out sizes, the browser assumes 100vw. That is fine for full-width images and wasteful for anything smaller, such as a card in a three-column grid, because it will pick a file that is too large.
For fixed-size images, such as a logo that is always 200 CSS pixels wide, use density descriptors instead. Do not mix w and x descriptors in the same srcset, and do not use sizes with x.
The browser picks the file that matches the screen density: 1x for a standard display, 2x for a typical high-density display.
You do not need a file for every pixel width. Aim for a ladder where each step is roughly 1.25 to 1.5 times the previous one, covering your smallest layout slot up to the largest slot multiplied by 2 for high-density screens.
| Layout slot (CSS px) | Useful candidates (px) |
|---|---|
| Up to 400 | 400, 800 |
| Up to 800 | 480, 800, 1200, 1600 |
| Full width up to 1600 | 640, 960, 1280, 1920, 2560 |
More candidates give a better fit but take more time to generate and cache. Build tools and image CDNs can create the whole ladder for you.
w as a CSS width. It is the file's real pixel width, so a mismatch with the actual file makes the browser choose badly.sizes describes the layout. If your CSS makes the image 50% wide, say so in sizes.srcset for art direction. Cropping a different composition for mobile needs the <picture> element, not srcset alone.To check your result, open the browser developer tools, look at the Network tab and read which file was downloaded. The currentSrc property of the image element also reports the chosen URL.
Resize the window and reload to see how the selection changes. Some browsers keep a larger file they already downloaded rather than fetch a smaller one.
Yes. Add loading="lazy" to the same <img> element. The browser still selects the best candidate when it decides to fetch the image.
They ignore it and load the src file. All current browsers support srcset and sizes.
It has no effect on its own. sizes only matters when srcset contains width descriptors.
Use srcset and sizes when the same image only needs different resolutions. Use <picture> when you need different crops or formats.
srcset lists image candidates with w or x descriptors, and sizes tells the browser how wide the image will be displayed so it can choose the right file before layout. Keep a sensible ladder of widths, always set src, width, height and alt, and verify the chosen file in the Network tab.