Performance and accessibility 4 min read Updated
Responsive images that fit the screen and the layout
In short
Send each screen a file that matches the space the image occupies, and declare its dimensions so the layout holds while it loads.
Give the browser a choice. Sending one large file to every screen wastes data, so supply several sizes and write the sizes attribute to match the real width of each slot. Then add width and height, so that the layout holds its place while the image loads.
Oversized images cost data
The web.dev guidance warns that serving desktop-sized images to mobile devices can use two to four times more data than needed. Send smaller files to smaller screens, and the largest image can reach the visitor sooner. It is common to create three to five sizes of each image. More sizes give a closer match, but they need more storage and more HTML.
Use width or density descriptors, not both
The srcset attribute lists the candidate files and what each one is. A width descriptor, such as 480w, states the intrinsic width of the file in pixels, and that value must match the file. The browser combines these widths with the sizes attribute to choose a candidate. A density descriptor, such as 2x, lets the browser pick by pixel density and suits images with a fixed display size. Do not mix width and density descriptors in the same srcset. A candidate without a descriptor counts as 1x.
Tell the browser how wide the slot is
The sizes attribute describes the width the image will occupy, using media conditions and lengths. A slot 480 pixels wide on narrow screens and 800 pixels wide otherwise can be written as “(max-width: 600px) 480px, 800px”. Three details matter:
- sizes accepts px, vw, em and calc(), but not percentages.
- The browser uses the first condition that matches and ignores the rest, so the order of the conditions matters.
- sizes does not change how the image is displayed. The CSS still sets the rendered size.
If sizes is omitted, its default is 100vw, the full width of the viewport. In a narrow column, that default is often too large.
Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea
Use picture for formats and art direction
The picture element wraps several source elements, and the browser uses the best match it supports. A source with a type attribute, such as image/avif or image/webp, is skipped by browsers that do not support that format, so a JPEG img remains as the final fallback. The img element must always be present, with src and alt. Use the media attribute only for art direction, when the crop or composition has to change at different widths. When you use media, do not repeat the same conditions in sizes.
Dimensions hold the space
Give each img width and height attributes, written as integers without units. The browser uses them to calculate the aspect ratio before the file loads, so the space is held. MDN recommends these attributes for all images to avoid layout shift. They matter even more for lazy-loaded images, because an image that has not loaded has a width and height of zero. Every candidate in a srcset should share the same aspect ratio, so that swapping files does not change the shape of the box.
A practical checklist
- Export each image at the largest size you need, then create three to five smaller widths.
- Write srcset with width descriptors for flexible layouts, and sizes that match the real width of each column.
- Add width and height to every img, and check that all candidates share one aspect ratio.
- Use picture with AVIF or WebP sources only when the img fallback is present and correct.
- Do not lazy-load the main image of the first view.
- Run Lighthouse and check the Properly size images result for files that are still too large.
Image delivery also changes how soon the largest element appears, see Fixing Largest Contentful Paint one delay at a time. Reserved space and layout stability are covered in fixing Cumulative Layout Shift without jumping content. For lazy loading below the fold, see Where lazy loading helps and where it hurts.
Common questions
- Do I need picture if srcset already works?
- Not for simple resizing. Use picture when you need a modern format such as AVIF or WebP with a fallback, or when the crop has to change at different widths.
- Does sizes change how big the image looks?
- No. The sizes attribute helps the browser choose a file, but the CSS still sets the rendered size of the image.
- Should I use width or density descriptors?
- Use width descriptors when the image fills a flexible layout and sizes describes that layout. Use density descriptors when the displayed size is fixed. Never mix the two in one srcset.
Sources
- Responsive images, MDN Web Docs (Mozilla). Accessed 24 November 2026
- picture: The Picture element, MDN Web Docs (Mozilla). Accessed 24 November 2026
- img: The Image Embed element, MDN Web Docs (Mozilla). Accessed 24 November 2026
- Serve responsive images, web.dev (Google). Accessed 24 November 2026
Further reading
-
Fixing Largest Contentful Paint one delay at a time
Split the LCP delay into four parts, then fix the largest one first instead of guessing at image tweaks.
-
Where lazy loading helps and where it hurts
Use lazy loading for images and iframes below the first screen, and never for the element that shows first.
-
Fixing Cumulative Layout Shift without jumping content
Give every element its space before it loads, because content that arrives late and pushes the page is the usual cause of layout shift.
Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea