Skip to content
fa
Search Frontend Atlas
Search questions and concepts across the handbook.
Browser & Web Platformintermediate13 min read

How do you load images without hurting performance or layout?

srcset, sizes, priority, lazy loading, dimensions, and the CLS/LCP trade-offs that show up in real pages.

Useful prerequisites

A phone with a narrow screen usually does not need the same image file as a large desktop display. Sending one oversized asset wastes bandwidth, while choosing an image that is too small makes it look blurry. HTML gives the browser hints about available image sizes so it can make a sensible choice for the actual layout and device.

A phone with a narrow screen usually does not need the same image file as a large desktop display. Sending one oversized asset wastes bandwidth, while choosing an image that is too small makes it look blurry. HTML gives the browser hints about available image sizes so it can make a sensible choice for the actual layout and device.

The quick version

Give the browser enough information to pick the right resource and reserve space before the image arrives. Use srcset + sizes (or <picture>) for resolution and art direction, set explicit dimensions or aspect-ratio to avoid layout shift, lazy-load below-the-fold images, and reserve fetchpriority="high" (and avoid lazy loading) for the LCP image. Format and compression still matter; the loading strategy cannot fix a multi-megabyte asset.

Build the mental model

Images are often the largest bytes on the page and a frequent source of:

  • Slow LCP — the main hero or product image arrives late or is too large.
  • CLS — the image appears and pushes content down because no space was reserved.
  • Wasted bandwidth — a 2000px image shown in a 400px slot on a 1x screen.

The browser can only make good choices if you tell it what candidates exist (srcset), how wide the image will be displayed (sizes), and whether this image is critical or not (loading, fetchpriority).

Responsive selection: srcset and sizes

<img
  src="/hero-800.jpg"
  srcset="/hero-400.jpg 400w, /hero-800.jpg 800w, /hero-1200.jpg 1200w"
  sizes="(max-width: 600px) 100vw, 600px"
  width="1200"
  height="800"
  alt="Product hero"
/>
  • srcset with w descriptors — candidate files and their intrinsic widths.
  • sizes — how wide the image will be laid out in different conditions. This is not the same as CSS max-width; it is a hint for resource selection.
  • width / height — used for aspect ratio and space reservation even when CSS scales the image.

If sizes is wrong, the browser may download an unnecessarily large file. If dimensions are missing, you invite layout shift when the image loads.

Use <picture> when you need art direction (different crop on mobile) or format negotiation (type="image/avif" / image/webp) with fallbacks.

Priority and lazy loading

LCP image (usually the main hero)

  • Do not lazy-load it.
  • Consider fetchpriority="high".
  • Preload it if it is discovered late (e.g. background image or CSS-loaded) — but prefer a real <img> in the initial HTML when you can.

Below-the-fold images

  • loading="lazy" is appropriate.
  • Still provide dimensions so scroll layout stays stable.

Above-the-fold but not LCP

  • Default loading is often fine; avoid demoting the LCP image by giving every image high priority.

Lazy loading is a tool, not a default for every image. Lazy-loading the LCP image is a common self-inflicted performance wound.

Layout stability

Always reserve space:

  • Intrinsic width and height attributes, or
  • CSS aspect-ratio with a defined width or height, or
  • A fixed-size container the image fills.

Without that, the image’s arrival moves everything below it. That is CLS, and it is visible and measurable.

For responsive images that change aspect ratio between breakpoints (art direction), reserve space per breakpoint or accept a carefully designed shift — do not leave it to chance.

Format and compression

Modern formats (AVIF, WebP) often shrink bytes significantly. Serve them when supported, with a JPEG/PNG fallback. Compression and correct dimensions still beat a fancy format applied to an oversized source. Generate a few sensible widths rather than one giant image scaled down in the browser.

See it in code

<picture>
  <source
    type="image/avif"
    srcset="/product-480.avif 480w, /product-960.avif 960w"
    sizes="(max-width: 640px) 100vw, 640px"
  />
  <img
    src="/product-960.jpg"
    srcset="/product-480.jpg 480w, /product-960.jpg 960w"
    sizes="(max-width: 640px) 100vw, 640px"
    width="960"
    height="640"
    loading="lazy"
    alt="Blue running shoe viewed from the side"
  />
</picture>

This is a below-the-fold product image: the browser can choose a candidate based on the rendered slot and device, the dimensions reserve a 3:2 box, and lazy loading avoids competing with the initial viewport. For the page’s LCP hero, remove loading="lazy" and consider fetchpriority="high" instead.

Where it gets tricky

Background images in CSS
Harder to prioritise and to give srcset-style behaviour. Prefer an <img> (positioned as needed) for important content images so the browser’s loader and LCP logic can see them.

Images in carousels / tabs
Only the visible one needs high priority. Lazy-load or defer the rest. Watch for layout shift when a slide first appears if dimensions were not reserved.

User-generated content
You may not control the source dimensions. Use a consistent container aspect ratio and object-fit so the layout stays stable even when the asset is awkward.

Dark mode / theme-dependent images
<picture> or CSS can swap sources. Avoid downloading both when only one will be shown if you can structure the markup accordingly.

Print and offline
Cached images and print stylesheets still care about sensible resolution. Extreme optimisations aimed only at mobile screens can look poor on a large printed page or a high-DPI desktop.

A common misunderstandings

  • “Lazy loading everything is always better.”
    Not the LCP image. Not images that are immediately visible.

  • “If I use a CDN, responsive images are optional.”
    A CDN helps delivery; it does not choose the right width or prevent CLS by itself.

  • “Width and height in HTML conflict with responsive CSS.”
    They establish the aspect ratio. CSS can still scale the image; the ratio prevents the layout jump.

What this changes in an application

Images dominate many sites’ weight and are a top contributor to poor LCP and CLS. Getting selection, priority, and space reservation right is one of the highest-ROI performance tasks on content-heavy pages. It is also one of the most visible: users see the hero load cleanly or they see the page jump and crawl.

Questions worth exploring next

  • How do you choose sizes for a fluid grid of cards?
  • When would you preload an image, and when is that a mistake?
  • How do you handle LCP when the hero is a CSS background?
  • What is the difference between art direction and resolution switching?

References and next steps

Explore next