A practical guide to responsive images in HTML

Responsive images let a browser receive an image suited to the screen, layout and connection in front of the reader. Instead of sending a large camera export to everyone, a page can offer smaller variants, reserve the right space and prioritise the picture that matters. That matters on Australian mobile connections, where a heavy image can consume data and delay the first view.

The restrained approach on the personal domain offers a useful starting point. Typography, whitespace and photography benefit from clear boundaries and careful proportion, while the HTML beneath them controls delivery. Responsive images are a set of understandable decisions that should remain easy to maintain as a site grows.

Start with the image's job

Begin with the job each image performs. A portrait might introduce a person, a product photograph might show construction details, and a decorative texture might establish a visual tone. Give meaningful images alternative text describing what they communicate. For decoration, use an empty alt value so assistive technology can skip it.

Decide what must remain visible at narrow widths. A face, ingredient label or sign may need a different crop from a wide landscape. If the subject would be lost when a photograph is merely reduced, the page needs an alternative composition or more room. This decision belongs before filenames, compression settings or CSS are chosen.

Set a sensible size limit before producing assets. A small editorial site may need only a few widths, whereas a shop with detailed products may need larger files and careful zooming. Choose dimensions that cover the largest space the image can occupy on the largest supported screen, then remove variants that never serve a real layout.

Reserve space before images arrive

Images without reserved space can shift text downwards as they load, producing cumulative layout shift. Give every image width and height attributes describing its intrinsic proportions, even when CSS scales it. These attributes do not force the browser to display the file at that size; they establish the aspect ratio so it can reserve the correct box.

<img src="/images/coast-800.jpg"
     alt="Waves breaking along a sandy Australian beach"
     width="1600" height="900">

Keep the ratio consistent across resized variants. If the original is 1600 by 900, a generated 800-pixel version should be 800 by 450, not 800 by 900. A simple CSS rule can then make the image fluid without distorting it. The declared ratio also prevents a visible jump when a different source arrives.

img {
  display: block;
  width: 100%;
  height: auto;
}

Fixed-ratio galleries need a different treatment. Set an aspect ratio on the image container, fill that container and crop the contents with object-fit. This allows a portrait and a landscape to share a tidy row without stretching either one. Choose the crop's focus deliberately; the default centre position is not always right.

.photo {
  aspect-ratio: 4 / 3;
  overflow: hidden;
}

.photo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
}

Offer alternatives with srcset

The srcset attribute supplies multiple image candidates for one slot. Each candidate describes its intrinsic width using the w suffix, such as 480w or 800w. The browser compares those widths with the space the image will occupy and the device's pixel density, then chooses a suitable file. This avoids treating every screen as though it were a full-size desktop display.

<img src="/images/coast-800.jpg"
     srcset="/images/coast-480.jpg 480w,
             /images/coast-800.jpg 800w,
             /images/coast-1200.jpg 1200w,
             /images/coast-1600.jpg 1600w"
     sizes="(min-width: 70rem) 70rem, 100vw"
     width="1600" height="900"
     alt="Waves breaking along a sandy Australian beach">

The width value is a fact about the file, not a request to display it at that many CSS pixels. It must match the candidate actually on disk. A browser may select the 800-pixel file for an 800-CSS-pixel slot on a standard-density screen, while a high-density screen may select the 1600-pixel file for the same visual area.

Keep the list manageable and relevant. Width descriptors work naturally with sizes, while density descriptors such as 2x suit a fixed-size thumbnail where that distinction is simpler. Do not mix w and x descriptors in one srcset. Naming conventions help organise the folder, but the browser reads the descriptors, not digits embedded in filenames.

Describe layout with sizes

The sizes attribute explains the space an image will occupy before the browser selects a candidate. Without it, many browsers assume the image could fill the entire viewport and favour an unnecessarily large file. This attribute does not control layout; CSS still sets the rendered dimensions. It supplies a realistic estimate for selection.

<img src="/images/coast-1200.jpg"
     srcset="/images/coast-480.jpg 480w,
             /images/coast-800.jpg 800w,
             /images/coast-1200.jpg 1200w,
             /images/coast-1600.jpg 1600w"
     sizes="(min-width: 70rem) 70rem, 100vw"
     width="1600" height="900"
     alt="Waves breaking along a sandy Australian beach">

For a full-width photograph limited to 70rem, that size description matches the intended layout: 70rem once the viewport is wide enough, otherwise the full viewport width. A grid needs a different answer. Three cards across a 64rem container may each occupy about 30rem, while a single column may fill the available width. The closer the hint comes to reality, the better the selection.

Measure the layout rather than guessing from device names. A phone can show a narrow image beside a caption, and a desktop browser can show a small card inside a wide window. Test resizing rather than checking a handful of presets. Small differences between the hint and final CSS are harmless, but a large mismatch can produce an oversized download.

Control crops with picture

The picture element gives an image a more flexible set of sources. It can offer modern formats, different crops for different screens or both. The browser evaluates source elements in order and uses the first match, then falls back to the img element when none applies. This makes picture useful for art direction, but unnecessary when a single srcset is enough.

<picture>
  <source media="(max-width: 47.999rem)"
          srcset="/images/harbour-1200.jpg">
  <source media="(min-width: 48rem)"
          srcset="/images/harbour-1800.jpg">
  <img src="/images/harbour-1800.jpg"
       alt="Ferries crossing Sydney Harbour at dusk"
       width="1800" height="1200">
</picture>

Keep the mobile and desktop crops at the same ratio when using intrinsic dimensions, as this example does. For tighter framing, object-position can shift the visible area within a fixed-ratio box. Source media conditions must agree with the CSS breakpoints; a mismatch can select a wide crop for a narrow container. Type attributes can identify AVIF or WebP candidates before the fallback.

This selective approach reflects the restrained philosophy in writing about web design: serve only what supports the page rather than adding technology for its own sake. It also keeps the markup understandable for a personal site, where a small collection of well-chosen files is usually easier to maintain than a complex matrix of variants.

Manage loading priority

Delivering an image quickly is only part of the job. The browser should also avoid competing with essential content. A large photograph near the top of the page may be the largest contentful element, so delaying it can make the page feel slow. Images well below the fold can wait until they approach the viewport.

<!-- The likely largest contentful element -->
<img src="/images/coast-1200.jpg"
     srcset="/images/coast-800.jpg 800w,
             /images/coast-1200.jpg 1200w"
     sizes="(min-width: 70rem) 70rem, 100vw"
     width="1600" height="900"
     alt="Waves breaking along a sandy Australian beach"
     fetchpriority="high" decoding="async">

<!-- A gallery image below the fold -->
<img src="/images/gallery-800.jpg"
     width="1600" height="900"
     alt="A native garden path"
     loading="lazy" decoding="async">

Use loading="lazy" for content that is not initially needed, and leave the likely largest contentful element eager. Native lazy loading uses the browser's viewport heuristics, so it requires no custom observer. decoding="async" is a timing hint rather than a guarantee that decoding will happen off the main thread. fetchpriority="high" belongs on the one image that truly deserves it.

Do not combine every feature simply because it is available. A small thumbnail may need no srcset, while a full-bleed hero certainly does. A tiny decorative graphic can be delivered as a fixed asset. Clear intent produces smaller markup and fewer decisions to revisit when a design changes.

Test on real devices and connections

Test the finished page on a real phone as well as in browser developer tools. Inspect the network panel, note the selected source and compare the transferred file size with the image's rendered dimensions. A 1600-pixel file filling a 320-pixel thumbnail wastes bandwidth; a 480-pixel file stretched across a 1200-pixel hero looks soft.

Australian connections make this testing practical. Commuters in Sydney and Melbourne may move between fast 5G areas and weaker coverage during a journey, so a page that feels instant at the office can consume a monthly data allowance on the train. A throttled 4G test is useful, but field performance on local routes is the better measure of a regional business's audience.

Fixed-line access varies too. An address on the NBN may use FTTP, HFC or FTTN, and each has different upload and latency characteristics. Two photographs can therefore take noticeably different times to arrive for customers in the same city. Public agencies in Canberra work under accessibility requirements, so meaningful alternative text and stable zoom behaviour belong in the same testing pass.

Keep generation rules in the build process or asset notes: one naming pattern, a small set of widths and an explicit list of which images receive eager loading. Revisit those rules when the layout changes or a new image type arrives. A responsive image set is complete when every screen receives an intentional crop and a file suited to the space it occupies, without changing the page's structure.