Images are usually the heaviest thing on a web page, and very often the largest element in the first view, which makes them the Largest Contentful Paint (LCP) element. Get them wrong and you ship megabytes nobody needed, push your LCP past the 2.5 second target, and make the page jump around as it loads.

The good news is that the fixes are almost all plain HTML attributes that every current browser understands. You do not need a framework or a build step to get most of the benefit, although image CDNs and framework components can automate it.

This guide is for developers who want a clear checklist for images in 2026. We go through formats, responsive sizes, loading priority, lazy loading and layout stability, then put it all together in a template you can copy.

Step 1: choose a modern format

#

The format you pick has the biggest effect on file size.

  • AVIF usually gives the smallest files for photos, often well under half the size of a comparable JPEG. It also supports transparency and high dynamic range. Encoding is slower than other formats, which matters if you generate images on the fly.
  • WebP is a little larger than AVIF in most tests but still much smaller than JPEG and PNG, and it encodes quickly.
  • JPEG and PNG remain the universal fallbacks.
  • SVG is the right choice for logos, icons and simple illustrations, because it is resolution independent and often tiny.

AVIF and WebP are both supported in every current major browser; AVIF became Baseline in 2024. Still, offering a fallback costs nothing, and the picture element makes it easy:


_10
<picture>
_10
<source srcset="/img/hero.avif" type="image/avif">
_10
<source srcset="/img/hero.webp" type="image/webp">
_10
<img src="/img/hero.jpg" alt="A team working around a laptop" width="1200" height="800">
_10
</picture>

The browser goes through the source elements in order and uses the first type it supports. The img is required: it is the element that actually renders, and it carries the alt, width and height attributes. The source elements only swap which file it loads.

If you use an image CDN, you may not need picture for formats at all. Many CDNs read the browser's Accept header and return AVIF or WebP automatically from a single URL.

Compression settings

A quality setting that looks fine is usually lower than you expect. For AVIF, quality values in the 50 to 65 range are a common starting point for photos; for WebP and JPEG, 70 to 80. Check a few images by eye at the size they are displayed, not zoomed in. Tools such as Squoosh are good for comparing settings side by side, and sharp is a popular choice for automating it in Node.js.

Step 2: serve the right size with srcset and sizes

#

A phone with a 400 pixel wide screen does not need a 2400 pixel wide image. Responsive images let the browser pick a file that fits.

Width descriptors

List several widths of the same image in srcset, each followed by its real pixel width and a w:


_13
<img
_13
src="/img/product-800.jpg"
_13
srcset="
_13
/img/product-400.jpg 400w,
_13
/img/product-800.jpg 800w,
_13
/img/product-1200.jpg 1200w,
_13
/img/product-1600.jpg 1600w
_13
"
_13
sizes="(min-width: 64rem) 50vw, 100vw"
_13
alt="Blue running shoe, side view"
_13
width="1600"
_13
height="1200"
_13
>

Why sizes matters

The browser picks an image before it has laid out the page, so it does not know how wide the image will be displayed. sizes tells it. The value above means "on viewports at least 64rem wide, the image takes half the viewport width; otherwise it takes the full width".

The browser combines that with the device's pixel density. On a phone 400 CSS pixels wide with a 2x screen, the image needs to be 800 physical pixels wide, so it picks the 800w file. On a large desktop where the image is 700 CSS pixels wide at 1x, it also picks 800w.

If you leave sizes out, the browser assumes 100vw. For an image that only fills a small column, that means downloading a file far bigger than needed. Missing or wrong sizes is the most common responsive image mistake.

Your sizes value does not need to be exact. Rough breakpoints that match your layout are enough. A good check is to open DevTools, look at the image's displayed size and the file it picked, and make sure they are in the same range.

sizes="auto" for lazy images

For images that are lazy loaded, the browser waits until layout before fetching, so it can work out the size itself. The auto keyword asks it to do that:


_10
<img
_10
loading="lazy"
_10
sizes="auto, (min-width: 64rem) 33vw, 100vw"
_10
srcset="/img/card-400.jpg 400w, /img/card-800.jpg 800w"
_10
src="/img/card-800.jpg"
_10
alt="Coffee cup on a desk"
_10
width="800"
_10
height="600"
_10
>

Chromium browsers have supported auto since version 126, and other engines are adding it. Browsers that do not understand auto ignore it and use the rest of the list, which is why the fallback values stay. It only works together with loading="lazy".

Density descriptors for fixed-size images

For an image that is always the same CSS size, such as an avatar, use x descriptors instead:


_10
<img
_10
src="/img/avatar-64.jpg"
_10
srcset="/img/avatar-64.jpg 1x, /img/avatar-128.jpg 2x"
_10
alt=""
_10
width="64"
_10
height="64"
_10
>

No sizes is needed here, because the size never changes.

Step 3: load the LCP image first

#

The image that is the LCP element should start downloading as early as possible and at high priority. Browsers do not know which image that is until layout, so by default they often start it at low priority and only raise it later.

fetchpriority="high" tells the browser up front:


_10
<img
_10
src="/img/hero-1200.avif"
_10
srcset="/img/hero-800.avif 800w, /img/hero-1200.avif 1200w, /img/hero-2000.avif 2000w"
_10
sizes="100vw"
_10
fetchpriority="high"
_10
alt="Mountain range at sunrise"
_10
width="2000"
_10
height="1000"
_10
>

Use it on one image per page, or at most a couple. If everything is high priority, nothing is. You can also use fetchpriority="low" on images that are in the first view but not important, such as the second and third slides of a carousel.

Make sure it is discoverable

fetchpriority only helps if the browser can find the image early. It cannot help an image that:

  • is set as a CSS background-image, which the browser finds only after downloading and parsing the stylesheet
  • is inserted by JavaScript after the page loads
  • is lazy loaded with a JavaScript library that swaps data-src into src

If your LCP image has to be a CSS background, preload it in the head:


_10
<link
_10
rel="preload"
_10
as="image"
_10
href="/img/hero-1200.avif"
_10
imagesrcset="/img/hero-800.avif 800w, /img/hero-1200.avif 1200w, /img/hero-2000.avif 2000w"
_10
imagesizes="100vw"
_10
type="image/avif"
_10
fetchpriority="high"
_10
>

The imagesrcset and imagesizes attributes on the preload must match the ones the page will use, or the browser downloads two different files.

Step 4: lazy load everything else

#

Images below the fold should not compete with the important ones. The loading="lazy" attribute delays fetching an image until it is close to the viewport:


_10
<img src="/img/gallery-1.jpg" loading="lazy" alt="..." width="800" height="600">

Native lazy loading is supported in all current browsers, and it is better than a JavaScript library in almost every case: it needs no script, the browser picks sensible distances based on connection speed, and the real src is visible to the preload scanner and to search engines.

The one rule to remember: never lazy load the LCP image or anything in the first view. Lazy images wait for layout before loading, which delays exactly the image you most want to be fast. It is a common mistake in templates that add loading="lazy" to every image. Leave the attribute off (the default is eager) for the first few images, and add it to the rest.

Iframes support loading="lazy" too, which is a cheap win for embedded videos and maps below the fold.

decoding="async"

You may see decoding="async" in image snippets. It hints that the browser can decode the image off the critical path. Modern browsers already decode most images asynchronously, so the effect is small; it does no harm, but do not expect it to fix anything.

Step 5: always set width and height

#

If the browser does not know an image's size before it loads, it reserves no space, and the content below jumps down when the image arrives. That is layout shift, and it hurts your CLS score.

Set width and height to the image's intrinsic dimensions (or any values with the same aspect ratio). Then let CSS make it responsive:


_10
img {
_10
max-width: 100%;
_10
height: auto;
_10
}

Browsers use the two attributes to calculate an aspect ratio and reserve the right amount of space, even while the image is scaled by CSS. If an image needs a different shape at different sizes, set aspect-ratio and object-fit: cover in CSS instead.

Putting it together

#

Here is a template for a typical article page: a hero image that is the LCP element, and content images further down.


_35
<!-- Hero: high priority, not lazy -->
_35
<picture>
_35
<source
_35
type="image/avif"
_35
srcset="/img/hero-800.avif 800w, /img/hero-1200.avif 1200w, /img/hero-2000.avif 2000w"
_35
sizes="100vw"
_35
>
_35
<img
_35
src="/img/hero-1200.jpg"
_35
srcset="/img/hero-800.jpg 800w, /img/hero-1200.jpg 1200w, /img/hero-2000.jpg 2000w"
_35
sizes="100vw"
_35
fetchpriority="high"
_35
alt="Mountain range at sunrise"
_35
width="2000"
_35
height="1000"
_35
>
_35
</picture>
_35
_35
<!-- Content image: lazy, sized to the column -->
_35
<picture>
_35
<source
_35
type="image/avif"
_35
srcset="/img/chart-400.avif 400w, /img/chart-800.avif 800w"
_35
sizes="auto, (min-width: 48rem) 720px, 100vw"
_35
>
_35
<img
_35
src="/img/chart-800.jpg"
_35
srcset="/img/chart-400.jpg 400w, /img/chart-800.jpg 800w"
_35
sizes="auto, (min-width: 48rem) 720px, 100vw"
_35
loading="lazy"
_35
alt="Bar chart of page weight by year"
_35
width="800"
_35
height="500"
_35
>
_35
</picture>

Notice that fetchpriority, loading, alt, width and height all go on the img, even inside a picture. Each source has its own srcset and sizes.

If you use a framework image component, such as next/image, it generates most of this for you. You still need to tell it which image is the LCP image (with its priority or preload option, depending on the version) and give it an accurate sizes value.

Checking your work

#
  • PageSpeed Insights or Lighthouse will flag oversized images, missing dimensions, a lazy loaded LCP image and images that could use a modern format.
  • The Network panel in DevTools shows each image's priority (add the Priority column) and size. Reload with the cache disabled and check that the hero is fetched early at high priority.
  • The Performance panel shows the LCP element and breaks LCP into phases. If "resource load delay" is large, the image was discovered or prioritised too late.

Browser support

#

Everything in this guide is supported in all current major browsers, with one partial exception:

  • AVIF and WebP: all current browsers; AVIF became Baseline in 2024.
  • srcset****, sizes and picture: supported everywhere for many years.
  • loading="lazy" for images: all current browsers.
  • fetchpriority: Chrome and Edge 101, Safari 17.2 and Firefox 132; Baseline since October 2024. Older browsers ignore it.
  • sizes="auto": Chromium 126 and later, with other engines following. Unsupported browsers fall back to the rest of the sizes list.

Because unsupported attributes are ignored, you can use all of these today without breaking anything.

When not to use these techniques

#
  • Do not lazy load images in the first view, and especially not the LCP image.
  • Do not put fetchpriority="high" on lots of images. Use it on the one that matters.
  • Do not generate dozens of sizes. Four to six widths cover most layouts. More just fills your cache and storage.
  • Do not use AVIF for everything. For icons and simple graphics, SVG is smaller and sharper. For images generated on every request, AVIF's slow encoding may cost more than it saves unless you cache the result.
  • Do not preload images you also mark up normally unless the preload's imagesrcset matches exactly, or you will download two files.

Wrapping up

#
  • Serve AVIF or WebP with a JPEG fallback using picture, or let an image CDN choose the format.
  • Use srcset with width descriptors and an accurate sizes value so each device downloads a file that fits.
  • Mark the LCP image with fetchpriority="high", keep it discoverable in the HTML, and never lazy load it.
  • Add loading="lazy" to images below the fold, and sizes="auto" to those lazy images where you can.
  • Always set width and height so the browser can reserve space and avoid layout shift.

Further reading: MDN: Responsive images

file under:
Performance

Was this article helpful?

About the author

Danny Engineering

A software engineer with a strong belief in human-centric design and driven by a deep empathy for users. Combining the latest technology with human values to build a better, more connected world.