Skip to content

Loading images

Which images the browser fetches first, which it puts off, and how much room each gets before it arrives: fetchpriority for the first screen, loading="lazy" below it, width and height so nothing jumps, srcset for phone-sized files.

On this page

What it is

Loading an image is a set of choices the markup makes before a byte of the file arrives: when the browser asks for it, at what priority, which of several files, and how much room to hold while it waits. Browsers don't start images at the top priority because an image doesn't block rendering. Chrome's priority table puts an image that layout has placed inside the viewport at High, the first five images larger than 10,000 square pixels at Medium, and the rest at Low. Images below the viewport still get fetched during page load, just later.

The loading attribute controls deferral. lazy holds the request until the image is within a calculated distance of the viewport; eager is the default and loads the image wherever it sits on the page. The distance varies by browser and can depend on the connection type, and in Chrome it's meant to be large enough that the file finishes before the person scrolls to it. Deferral happens when JavaScript is enabled; with it disabled the image loads normally. A lazy image has to wait for layout to learn whether it's in view, so one that sits in the first viewport is requested after CSS has downloaded and applied, instead of the moment the preload scanner sees the tag. Inside a picture element, the attribute goes on the child img, not the wrapper.

fetchpriority changes the priority of a fetch the browser has already decided to make. It takes high, low or auto (the default), and it's a hint: the browser's own rules can override it. It's separate from a preload link, which makes the browser discover a resource early but fetches it at the default priority. A link rel="preload" is for images the parser can't see, such as a hero set as a CSS background; in that case add fetchpriority="high" to the link. Setting high on more than one or two images flattens the ordering so it stops helping.

Room comes from the width and height attributes. Browsers derive a default aspect ratio from them, so with CSS like width: 100%; height: auto the image scales to its container while the box is already the right height before the download starts. Without the attributes an image often has no size until the browser has read enough of the file to know its dimensions, and the text below it moves down when it does. That default also confuses lazy loading: a gallery of 0 by 0 images can all look like they fit in the first viewport, so none of them is deferred.

Which file to send is srcset and sizes. Each srcset entry carries a width descriptor (800w) that must match the file's real width; sizes tells the browser how wide the slot will be under each media condition. The browser evaluates sizes before downloading anything, multiplies the slot width by the device pixel ratio, and picks the closest candidate. When w descriptors are present src is ignored by browsers that understand srcset, and the spec lets a browser pick a smaller candidate than the math suggests, for a data-saving preference for instance. x descriptors (2x) are the simpler form for an image that stays one CSS size, such as an avatar. The picture element adds source children with media for a different crop per layout and type so the browser skips a format it can't decode.

text
<!-- First post: probably the largest thing on screen -->
<img src="/img/p1-800.jpg"
     srcset="/img/p1-400.jpg 400w, /img/p1-800.jpg 800w, /img/p1-1200.jpg 1200w"
     sizes="(max-width: 600px) 100vw, 600px"
     width="1200" height="800"
     fetchpriority="high"
     alt="Crowd at the harbor festival">

<!-- Tenth post: well below the fold -->
<img src="/img/p10-800.jpg"
     srcset="/img/p10-400.jpg 400w, /img/p10-800.jpg 800w, /img/p10-1200.jpg 1200w"
     sizes="(max-width: 600px) 100vw, 600px"
     width="1200" height="800"
     loading="lazy"
     alt="Repaired bridge reopened to traffic">

When to use it

The first screen of a feed or article is where the priority hint pays off. Largest Contentful Paint reports when the largest image, text block or video inside the viewport rendered, measured from the start of the navigation. A good value is 2.5 s or less, and the figure to watch is the 75th percentile of page loads, split by mobile and desktop. Field numbers include redirects, connection setup and time to first byte, which is one reason they can differ from a lab run. Our advice: pick the single image most likely to be that element, give it fetchpriority="high", keep it in the HTML the server sends, and judge the result by the field percentile rather than one lab score.

Long lists are where loading="lazy" belongs: every image past the first screen. The attribute is supported in all major browsers, so a JavaScript lazy-loading library adds script weight for something the browser already does, and that extra script can hurt INP, the responsiveness metric. Chrome's experiments on Android found 97.5% of lazy-loaded images fully loaded within 10 ms of becoming visible on 4G, and 92.6% on slow 2G, so the deferral usually isn't something people see.

Images that are early in the markup but not visible, such as the second to fourth slides of a carousel, are a case for fetchpriority="low". They can sit close enough to the viewport that lazy loading fetches them anyway, so lowering their priority is the fix that works. A lazy image hidden with display: none, on the element or a parent, isn't loaded by Chrome, Safari or Firefox.

When not to

Don't lazy-load anything likely to be in the first viewport, and especially not the probable Largest Contentful Paint image. The lazy image waits for layout, so it starts downloading later than it would have with no attribute at all, and the saving is zero because the person was going to see it anyway. Where you can't know the fold position for every device, leave the first few images eager and accept that a tall phone may fetch one image it didn't need yet.

Don't sprinkle fetchpriority="high" across the first screen; past one or two images the hint stops reducing LCP. Don't expect loading="eager" to speed anything up either: it's the default, and it doesn't load an image faster than one with no attribute. Combining loading="lazy" with fetchpriority="high" is rarely useful: the image still waits while off screen, and once it's near the viewport the browser tends to raise its priority on its own.

Don't reach for the picture element when srcset does the job. The wrapper is for art direction (a different crop per layout) and format negotiation; for the same picture at several sizes, srcset and sizes on a plain img are enough, and for high-density screens MDN recommends srcset with x descriptors over the wrapper so a browser in a data-saving mode can choose the lower-density file. A JavaScript loader is still the right tool for things the browser won't defer on its own: video elements and their posters, and CSS background images.

The width and height attributes need every candidate in srcset to share one aspect ratio. When uploads come in any shape and you can't normalize them, reserve the room another way: a CSS aspect-ratio on the container, or a min-height that matches the smallest size you'll show, and accept some shift for the larger ones. A wrong ratio in the attributes still shifts less than the 0 by 0 default.

Compared

ToolWhat it doesReach for it whenThe simpler choice wins when

loading="lazy"

Holds the request until the image is near the viewport

Images past the first screen

The image is in the first viewport: the default eager load starts at discovery, before layout

fetchpriority="high"

Raises the network priority of a fetch the parser already found

The one image likely to be the largest on screen

Several images could be largest: hints on all of them flatten out, so order the markup instead

fetchpriority="low"

Lowers priority without deferring

Hidden carousel slides early in the markup

The image is far down the page: lazy already defers it

Preload link

Makes the browser discover a file early, at default priority

The image lives in CSS or JavaScript

The image is an img tag in the HTML: the parser finds it, and a preload is a second place to keep in sync

width and height

Give the browser an aspect ratio to reserve the box

Every image whose ratio is known

The ratio isn't known when the HTML is written: CSS aspect-ratio or min-height on the container, with some shift

srcset + sizes

Lets the browser pick a file by slot width and pixel density

Fluid images in a column

The image is one fixed CSS size: x descriptors, no sizes

picture element

Switches crop by media query or format by type

A different crop per layout, or a new format with fallback

Same crop, different sizes: srcset alone

JavaScript observer loader

Custom deferral for images, iframes and other elements your script controls

Video posters and CSS backgrounds

A plain img tag: the browser's own lazy loading, with no script cost

In a picture

HTML parsedBoxes reservedfrom width andheight

Step 1 of 5

The HTML arrives

The server sends the first posts as HTML. Every image tag carries width and height, so the browser lays out boxes of the right height before any image bytes arrive, and the text around them doesn't move later.

How to say it in an interview

The common mistake

Quiz

A feed template adds loading="lazy" to every image. What happens to the first post's image on a slow phone?
A lazy-loaded gallery has no width or height attributes. Why might every image load at once?
You set fetchpriority="high" on all six images in the first viewport. What's the likely effect on LCP?
The probable LCP image is a CSS background image. What helps the browser start it early?

Questions that use this

Hear it in

Related

Sources

  1. MDN: <img>: The Image Embed element
  2. MDN: fetchpriority HTML attribute
  3. MDN: <picture>: The Picture element
  4. MDN: Using responsive images in HTML
  5. web.dev: Largest Contentful Paint (LCP)
  6. web.dev: Optimize Largest Contentful Paint
  7. web.dev: Browser-level image lazy loading for the web
  8. web.dev: Lazy load images and <iframe> elements
  9. web.dev: Optimize resource loading with the Fetch Priority API
  10. web.dev: Optimize Cumulative Layout Shift
Sign in to report a mistake