# Responsive images

## What it is

Responsive images let a browser choose a suitable image resource for the place a
picture occupies. A small screen may need fewer pixels than a large one, and a
high-density display may need more than a standard-density display of the same
layout size. The page can describe its available resources and the expected
display conditions so the browser can select one that fits.

HTML provides several ways to express the choices. A `srcset` list can associate
candidate files with widths or pixel-density descriptors. A `sizes` attribute
can describe an expected layout slot when width descriptors are used. The
browser uses this information along with its actual conditions. A picture
element can also provide a different crop for a particular layout or offer a
different encoding supported by the recipient.

Image dimensions are another part of the design. Accurate width and height
information can help the browser reserve space before an image arrives. The
displayed size can still respond to CSS. Verify the relationship between the
declared dimensions, the resources' actual pixels and the layout, especially
where a page uses crops of different shapes.

## Why it matters

Sending the largest available photograph to every visitor can increase download
time and data use. Sending a small version everywhere can make an important
image blurry when it is displayed larger or at higher density. The site can
offer useful choices and let the browser pick among them. Verify the browser’s
actual choice at different viewport widths, display densities and cache states.

Different layouts can change the image's slot in ways the viewport width alone
does not explain. A wide desktop page might give an image only part of its width
in a grid. A narrower layout can place the same picture across a whole column.
An inaccurate sizes declaration can cause the selected resource to be larger or
smaller than the layout needs. Derive it from the component's actual
breakpoints.

Art direction is a separate decision about what the reader sees. A portrait on a
small screen may need a tighter crop if the full scene makes the important
person too small. The crop still needs to preserve what the image contributes,
and its text alternative should keep that meaning. Inspect the resource and
composition together at the layout where people see it.

## What it looks like in practice

Imagine a portrait in a profile and on a wide article page. Measure the display
slot in each use. Generate smaller derivatives that preserve the intended crop
and enough candidates for the display densities the product cares about. Use an
ordinary image fallback and supply the expected sizes for the width-based
candidates. Check that the decoded image looks sharp at the actual rendered
size.

Make the network test part of the review. In a fresh browser at a small
viewport, observe the selected resource and measure the slot. Repeat at the same
width with a higher-density setting and then at a wider layout. A warm cache can
affect which resource the browser reuses, so exercise a return visit separately.
Record the resource dimensions, content type and bytes next to the captured
result.

Engineer's
[stylesheet and asset delivery case study](/portfolio/cut-the-stylesheet-bundle-from-87-kb-to-31-kb/)
records a trial of a modern format for the brand mark. The trial file was
larger, so the site kept the existing format. The same case describes checking
original pixel dimensions against the actual display size. Measure the selected
file, its bytes and its appearance under the conditions readers use to see
whether each candidate should be offered.

Include the initial layout before the image has decoded. Reserve the correct
proportions and look for a later shift. If the portrait moves between a sidebar
and a wide column, test both placements and ensure the metadata describes the
resource used in each.

When the image belongs at the top of an article, check how early the browser
finds it and whether the page defers it. A below-the-fold gallery and an opening
portrait can have different loading policies while sharing the same
candidate-generation pipeline. Compare the initial page request, the moment the
first photograph appears and the downloads caused by scrolling. Repeat in a
realistic connection profile, since a large resource that arrives instantly from
the local cache can mask a substantial cost on the first visit. Keep the loading
priority decision close to the image's role so it stays correct when the layout
moves.

## Where it goes wrong

If the sizes value describes the full screen, the browser can fetch an
unnecessarily large resource for an image in a narrow column. Compare the
expected and measured widths around layout changes. Keep the media conditions in
the markup aligned with the CSS, or use a design that makes the slot clear
through a supported default.

A resource can also declare an inaccurate width or contain a crop different from
what the layout assumes. Verify the actual dimensions of the files offered. If a
candidate was resized from a smaller master, it can use more bytes without
improving detail. Check the delivered pixels at the native sizes and densities
that motivated the candidate.

Finally, review the whole image experience. A picture can have correct network
behaviour while its crop loses an important detail, its reserved area is
inaccurate, or its alternative no longer describes the visible subject. A useful
responsive image delivers appropriate bytes, leaves the layout stable and
preserves meaning across the contexts in which it appears.

## Further reading

- [The HTML Standard on viewport-based image selection](https://html.spec.whatwg.org/multipage/images.html#viewport-based-selection).
  Describes srcset and sizes for responsive slots.
- [Chrome's web.dev guide to descriptive image choice](https://web.dev/learn/images/descriptive).
  Explains how candidate widths and densities are described.
- [Chrome's web.dev guide to prescriptive picture sources](https://web.dev/learn/images/prescriptive).
  Covers art direction and format selection.

<https://engineer.company/glossary/responsive-images/>
