Responsive images

Also known as: srcset

On this page 7

Responsive images are an HTML technique in which a page offers the browser several candidate files for one image, together with hints about the layout, so that the browser can download the version that best fits the viewport width, the device pixel ratio and the supported formats. In web development the technique rests on the srcset and sizes attributes of the img element and on the picture element, all defined in the HTML Living Standard.

The goal is twofold: a phone on a slow connection should not download a 2,400-pixel photograph meant for a desktop monitor, and a high-density display should not show a blurry upscaled thumbnail. Because images typically account for the largest share of page weight, responsive images are one of the most effective levers for web performance and for metrics such as Largest Contentful Paint.

How the browser selects an image candidate

The browser selects an image candidate by combining the author’s declarations with information that only the client knows. The author lists files and describes them; the browser knows the viewport size, the screen density, the user’s zoom level and, in some browsers, a data-saving preference. This division of labor is deliberate: the author describes, the browser decides.

With width descriptors such as photo-800.jpg 800w, the author tells the browser the intrinsic width of each file. The sizes attribute then states how wide the image will be rendered, for example (min-width: 60em) 50vw, 100vw. The browser multiplies the rendered slot width by the device pixel ratio and picks the smallest candidate that covers the result. With density descriptors such as logo@2x.png 2x, the author instead targets fixed-size images on screens of different density.

The following table summarizes the main building blocks and the job each one performs.

MarkupRoleTypical use
srcset with w descriptorsLists files by intrinsic pixel widthFluid images that change size with the layout
srcset with x descriptorsLists files by pixel densityFixed-size logos, icons and avatars
sizes attributeDeclares the rendered width per media conditionRequired companion to w descriptors
picture elementWraps alternative source elementsArt direction and format fallback
source elementOffers a srcset with media or type conditionsAVIF or WebP first, JPEG as fallback
Core markup for responsive images

Resolution switching versus art direction

Responsive images solve two distinct problems, and confusing them is a common source of broken markup. Resolution switching serves the same picture at different sizes, and the browser is free to choose any candidate. Art direction serves visibly different crops or compositions at different breakpoints, and the author must force the choice.

For resolution switching, a plain img with srcset and sizes is enough, and the browser may even prefer a cached larger file or a smaller file on a constrained network. For art direction, the picture element with source elements carrying a media attribute is required, because media conditions on source are binding, whereas candidates inside a single srcset are only suggestions.

SituationRecommended markupReason
Same photo, fluid widthimg with srcset and sizesBrowser picks the most efficient file
Fixed-size icon on retina screensimg with x descriptorsOnly density varies, not layout width
Wide banner cropped tightly on phonespicture with media on sourceComposition must change, not only size
Modern format with safe fallbackpicture with type on sourceUnsupported types are skipped reliably
Purely decorative backgroundCSS image-set()Image carries no content meaning
Choosing the right pattern

The type attribute on source, such as image/avif, lets browsers skip formats they cannot decode without downloading them. Because the first matching source wins, sources should be ordered from the most efficient format to the most widely supported one, with the inner img acting as the universal fallback and the carrier of the alt text.

Writing accurate sizes values

The sizes attribute is where most responsive image implementations go wrong, because the browser must choose a file before CSS layout is known. The preload scanner fetches images early, so it cannot measure the rendered element; it relies entirely on the author’s declaration. An inaccurate sizes value therefore leads directly to oversized or undersized downloads.

Each entry pairs a media condition with a length, and the first matching condition applies. Lengths may use vw, px, em or calc(), but not percentages, since percentages would require layout information. When sizes is omitted with width descriptors, the browser assumes 100vw, which wastes bandwidth for any image narrower than the viewport.

The table below shows how one layout translates into a sizes declaration and what file a typical screen ends up requesting.

Layout breakpointRendered widthChosen file at 2x density
Below 40em100vw minus 2em paddingAbout 750w on a 390-pixel phone
40em to 64em50vwAbout 1,000w on a 1,000-pixel tablet
Above 64emFixed 600px columnAbout 1,200w on any desktop
Example sizes declaration and resulting choice

Newer browsers accept sizes="auto" for lazily loaded images, because by the time a lazy image is fetched, layout is already available. This option works only together with loading="lazy" and still needs an explicit fallback list for older engines.

Performance and layout stability

Responsive images improve performance only when they are combined with correct loading and sizing hints. The width and height attributes should always be present on the img element, because modern browsers derive an aspect ratio from them and reserve space before the file arrives, which prevents Cumulative Layout Shift.

The hero image that usually forms the Largest Contentful Paint element should load eagerly and may carry fetchpriority="high"; images below the fold benefit from loading="lazy". When a hero image is preloaded, the link rel="preload" tag must repeat the same candidates through imagesrcset and imagesizes, otherwise the browser may download two different files.

AttributeEffectCommon error
width and heightReserve space and set aspect ratioOmitted, causing layout shift
loading=”lazy”Defers offscreen downloadsApplied to the hero image
fetchpriority=”high”Raises priority of the key imageApplied to many images at once
decoding=”async”Avoids blocking rendering on decodeExpected to reduce file size
altProvides the text alternativePlaced on source instead of img
Attributes that complement srcset

Common mistakes and limitations

Even well-intended responsive markup can fail in predictable ways. The most frequent problems come from mismatched descriptors, missing fallbacks and a misunderstanding of how much control the author actually has.

  • Mixing w and x descriptors in one srcset is invalid, and browsers will ignore part of the list.
  • Declaring width descriptors without a sizes attribute makes every image behave as if it filled the full viewport.
  • Generating too many candidates adds build and cache overhead with little benefit; steps of roughly 300 to 500 pixels are usually sufficient.
  • Expecting a specific file to load is a misconception: outside of art direction, the browser may legitimately choose a different candidate.
  • Serving density candidates above 3x rarely produces visible gains, because the human eye cannot resolve the extra detail at normal viewing distance.

A structural limitation is that markup cannot react to element width directly: sizes responds to the viewport, not to the container. Component-based layouts that rely on container queries in CSS therefore need careful approximations, sizes="auto" with lazy loading, or an image CDN that negotiates sizes through client hints.

Stripped to the essentials

A web page often has to look good on a small phone and on a large, sharp desktop screen at the same time. Responsive images handle this by giving the browser a short menu of the same picture in several sizes, plus a note about how big the picture will appear on the page.

The browser then orders from that menu. A phone takes a small, light file and saves data and time, while a high-quality screen takes a larger file so the picture stays crisp. The key idea is simple: the page offers choices, and the browser picks the best fit for the device.

Where it is applied

An online furniture retailer shows product photos in a grid: one column on phones, two on tablets and a fixed 600-pixel column on desktops. The original photographs are 3,000 pixels wide and weigh about 1.8 MB each as JPEG files, which made the mobile product pages slow on cellular networks.

The front-end team generates each photo at 400, 800, 1,200 and 1,600 pixels in AVIF and JPEG, wraps them in a picture element with an AVIF source, and writes sizes="(min-width: 64em) 600px, (min-width: 40em) 50vw, 100vw". The first product image receives fetchpriority="high", and all others are lazy loaded with explicit width and height.

After the change, a typical phone downloads an 800-pixel AVIF of roughly 70 to 90 KB instead of the full JPEG. Image transfer on mobile product pages drops by more than 80 percent, Largest Contentful Paint improves noticeably on slower connections, and layout shift from images disappears.