An image can look correct and still be delivered badly.
A phone may download the same oversized photograph as a desktop. A hero image may wait behind a lazy-loading rule. A product grid may move down the page as each image appears.
Compressing the file helps with one part of the problem. It does not decide which file the browser downloads, when the request starts or how much space the image occupies before it arrives.
Responsive images need three decisions: the appropriate source, the correct layout dimensions and a loading policy that matches the image’s role.
Separate display size from download size
This CSS makes an image fit its container:
img {
max-width: 100%;
height: auto;
}
It does not provide a smaller file.
If the image source is a 3,000-pixel-wide photograph, shrinking its visible width to 360 CSS pixels does not change the source URL. The browser still requests that file.
The implementation needs alternatives to choose from.
HTML provides srcset for image candidates and sizes for describing the expected display width when using width descriptors. The browser uses that information, together with factors such as pixel density, to select a source. HTML image selection.
Start by identifying the actual layout:
- Does the image span the viewport?
- Is it inside a column with a maximum width?
- Does a card switch from one column to three?
- Does the crop change on narrow screens?
Those answers determine the markup. A generic image component cannot describe every layout accurately with the same sizes value.
Make sizes match the CSS
Consider an article image that fills the available width, with 16 pixels of space on each side, until reaching a maximum width of 960 pixels.
.article-image {
display: block;
width: calc(100vw - 32px);
max-width: 960px;
height: auto;
margin-inline: auto;
}
Its markup can describe that same width:
<img
class="article-image"
src="/images/workspace-960.jpg"
srcset="
/images/workspace-480.jpg 480w,
/images/workspace-960.jpg 960w,
/images/workspace-1920.jpg 1920w
"
sizes="(max-width: 992px) calc(100vw - 32px), 960px"
width="1920"
height="1280"
alt="A desk with a monitor, notebook and adjustable task light"
>
The files must actually have the widths stated in srcset. In this example, they also share the same crop and aspect ratio.
The sizes attribute describes the display slot. It does not set the CSS width.
That distinction matters when a component moves. An image originally used across a full page may later appear in a narrow card. If its sizes value still says it occupies the full viewport, the browser receives misleading information about the required source size.
MDN’s responsive-image guidance explains how width candidates and source sizes work together. It also distinguishes this resolution-selection problem from changing the image composition.
Treat the layout and sizes value as one implementation decision. Review both when the component changes.
Account for pixel density
A 400-pixel-wide display slot does not always call for a 400-pixel-wide source.
On a screen with a device pixel ratio of two, an approximately 800-pixel-wide image can provide the corresponding pixel density. The browser retains discretion over source selection; the relationship is a useful planning guide rather than a command to download one exact candidate.
This is why “mobile image” is an incomplete specification.
A narrow screen can have a high pixel density. A desktop image can sit inside a small card. Choose candidate widths around actual display sizes and supported density needs.
The practical trade-off is between visible detail and transfer cost. Exporting every possible width adds processing and storage without necessarily improving the result. Exporting only one enormous version removes the browser’s opportunity to choose a smaller one.
Use a different crop when the composition needs it
Sometimes a smaller copy of the same image is not enough.
A wide photograph may show a product in the right-hand third of the frame. Reduced to a narrow column, the product becomes difficult to see. A closer crop may communicate the same information more clearly.
Use <picture> when the browser needs to choose between compositions:
<picture>
<source
media="(max-width: 600px)"
srcset="/images/product-closeup.jpg"
>
<img
src="/images/product-wide.jpg"
alt="A brushed steel desk lamp with an adjustable arm"
>
</picture>
This abbreviated example demonstrates source selection only. A production component also needs a sizing strategy that reserves the appropriate space for each crop.
The HTML standard distinguishes art direction, where the composition changes, from offering different resolutions of the same image. <picture> supports that selection through its sources.
Define the crop around the content. A centered crop can cut off the subject just as easily as an oversized download can waste bandwidth.
For a product image, preserve the details needed to understand the item. For a portrait, check the face and framing. For a screenshot, check whether the important interface remains readable.
Reserve space before the image loads
Without a known size or aspect ratio, an image can arrive after surrounding content has already been laid out and push that content elsewhere.
For images whose candidates share an aspect ratio, include accurate width and height attributes:
<img
src="/images/studio-1200.jpg"
width="1200"
height="800"
alt="A studio workbench beside a large window"
>
Then allow CSS to scale the image:
.content-image {
width: 100%;
height: auto;
}
The dimensions provide aspect-ratio information; they do not require the image to remain 1,200 pixels wide on a phone. Google’s layout-shift guidance recommends specifying image dimensions so the browser can reserve space before loading completes.
If the component deliberately crops images into a fixed shape, define that shape:
.product-card__image {
display: block;
width: 100%;
aspect-ratio: 4 / 3;
object-fit: cover;
}
This reserves a predictable box, but object-fit: cover can hide parts of the image. Review the result with real content rather than a single convenient placeholder.
When mobile and desktop sources have different aspect ratios, reserve the matching space at each breakpoint. One desktop ratio applied everywhere can leave a jump or an unintended crop.
Load the main image early
A hero image and a photograph near the bottom of an article have different jobs.
If the hero is the page’s Largest Contentful Paint element, delaying its request can delay the main visible content. Google’s LCP guidance advises against lazy-loading the LCP image and recommends making important image resources discoverable early.
For a confirmed important image, the markup might include:
<img
src="/images/hero-1280.jpg"
width="1280"
height="720"
fetchpriority="high"
alt="A modular shelving system installed in a living room"
>
This example assumes ordinary eager loading. fetchpriority="high" is a priority hint, not a guarantee of immediate loading.
Use it selectively. Giving every card image high priority does not explain which resource matters most.
Also inspect how the image enters the page. If JavaScript must execute before creating the hero’s <img>, the browser cannot discover that element’s source in the initial HTML. Compressing the file will not remove that discovery delay.
Confirm the actual LCP element before changing priorities. It can be text, and it can differ between layouts.
Lazy-load images that can wait
Images farther down the page are reasonable candidates for native lazy loading:
<img
src="/images/detail-960.jpg"
width="960"
height="640"
loading="lazy"
alt="Close-up of the shelving bracket and its wall fixing"
>
The browser determines when a lazy-loaded image is close enough to request. It may load before the image enters the viewport; lazy loading does not mean waiting until the exact moment of visibility.
Google’s browser-level lazy-loading guidance recommends using this capability for offscreen images while avoiding it for images needed in the initial viewport.
A practical starting policy is:
| Image role | Loading approach |
|---|---|
| Confirmed LCP image | Discover early; avoid lazy loading; consider high fetch priority |
| Other images visible immediately | Usually ordinary eager loading |
| Images well below the initial viewport | Consider native lazy loading |
| Images inside interactive galleries | Define loading around the interaction and verify actual requests |
This is an implementation starting point, not a universal rule for every page. A short viewport, an expanded banner or a different content length can change which images are initially visible.
Inspect what the browser actually downloads
Correct-looking markup is not enough evidence.
Open browser developer tools and inspect image requests across representative viewport sizes and pixel densities. For an individual image, currentSrc identifies the selected source:
const image = document.querySelector(".article-image");
console.table({
source: image.currentSrc,
displayedWidth: image.getBoundingClientRect().width,
devicePixelRatio: window.devicePixelRatio
});
Then inspect the network response for the transferred size and the actual file dimensions.
Check:
- Whether the selected source is appropriate for the display slot.
- Whether the hero request begins early enough.
- Whether offscreen images are being deferred.
- Whether image arrival causes layout movement.
- Whether different crops preserve the intended content.
- Whether any image is downloaded twice through conflicting loading mechanisms.
Use a fresh navigation for controlled comparisons. Resizing an already-loaded page may reuse a previously downloaded larger image, which can make source-selection tests confusing.
Compare results under consistent network and cache conditions. A repeat visit with cached images answers a different question from a first visit.
Make image delivery part of the content workflow
Responsive markup depends on the files behind it.
If the content workflow uploads one original and never produces variants, the frontend has little useful choice. If editors replace files without preserving their dimensions, the declared candidates may become inaccurate.
A practical image workflow should define:
- Required display roles, such as hero, card and article image.
- Supported crops and focal points.
- Candidate widths for each role.
- Compression and visual-review requirements.
- How dimensions reach the rendered HTML.
- Which components use eager or lazy loading.
- How published pages are checked after content changes.
The exact tools can vary. The output contract should remain clear.
A well-delivered image has enough detail for its display size, occupies predictable space and arrives when the page needs it. Those decisions belong in the component and publishing workflow, where they can survive the next upload.
Sources
- WHATWG: HTML images
- MDN: Using responsive images in HTML
- Google web.dev: Optimize Largest Contentful Paint
- Google web.dev: Optimize Cumulative Layout Shift
- Google web.dev: Browser-level image lazy loading
Code samples illustrate implementation patterns and use hypothetical image assets.
Conversation
Discussion
Loading comments…