← All posts

Responsive images: srcset and sizes explained (and what WordPress already does for you)

Why your site sends oversized images to phones, how srcset and sizes fix it, and how to write a sizes value that matches your layout, in plain HTML and in WordPress.

Responsive images: srcset and sizes explained (and what WordPress already does for you)

Key points

  • srcset lists the versions of an image you have; sizes tells the browser how wide it will be displayed. Without an accurate sizes, srcset does little.
  • WordPress generates srcset for you, but its default sizes often assumes a full-width image, so your theme needs to correct it.
  • Don’t lazy-load the main image at the top of the page; do lazy-load the rest.

In the performance audits we run, images are nearly always the heaviest part of a page. And the issue usually isn’t compression, it’s dimensions: a 2,000-pixel-wide photo sent to a phone that shows it at 360 pixels. HTML solved this long ago with the srcset and sizes attributes, but the second one is very often wrong, which wipes out most of the benefit of the first. Here’s how they work, how to write a sizes value that matches your layout, and what to adjust in WordPress.

How the browser picks an image

With srcset, you list several versions of the same image along with each one’s real width in pixels, followed by w. With sizes, you say how wide the image will be displayed depending on the screen. The browser combines both with the screen’s pixel density and downloads the smallest version that’s good enough.

<img src="/img/workshop-800.jpg"
     srcset="/img/workshop-400.jpg 400w,
             /img/workshop-800.jpg 800w,
             /img/workshop-1200.jpg 1200w,
             /img/workshop-1600.jpg 1600w"
     sizes="(min-width: 1200px) 370px,
            (min-width: 768px) calc((100vw - 64px) / 2),
            100vw"
     width="1200" height="800"
     alt="The team in a design workshop"
     loading="lazy" decoding="async">

In this example, on a wide desktop the image sits in a 370-pixel column; on a Retina screen (density 2) it needs about 740 pixels, so the browser picks the 800 version. On a 390-pixel-wide phone at density 3, the image fills the width: it needs about 1,170 pixels and takes the 1,200 version.

The key point: the browser picks the image before it has worked out the layout. It hasn’t read your CSS yet, which is why you have to describe the display width in sizes. Leave it out and the browser assumes 100vw, the full screen width, so on desktop a 300-pixel thumbnail gets its largest version.

For an image that’s always shown at the same size, such as a logo, the density version is simpler:

<img src="/img/logo-160.png"
     srcset="/img/logo-160.png 1x, /img/logo-320.png 2x"
     width="160" height="40" alt="Company name">

Writing a sizes value that matches your layout

A good sizes comes straight from your CSS, breakpoint by breakpoint. Our method:

  • List your grid’s breakpoints (say 768 and 1200 pixels).
  • For each, note the image width: fixed (370px) if the container has hit its maximum width, or relative (calc((100vw - 64px) / 2)) if it depends on the screen.
  • Write the conditions from widest to narrowest: the browser uses the first one that matches.
  • Finish with an unconditional value for small screens.

There’s no need to be pixel-perfect: being a few pixels out is fine, since the browser rounds to the nearest available version anyway. What costs you is being wrong by an order of magnitude, like declaring 100vw for an image that takes up a third of the screen.

To check the result, this short script flags images that were downloaded much larger than needed. Run it at several window widths and in the dev tools’ mobile mode:

// Paste into the browser console on the page you want to check
for (const img of document.images) {
  if (!img.currentSrc || !img.clientWidth) continue;
  const copy = new Image();
  copy.src = img.currentSrc;              // already cached: no new download
  await copy.decode().catch(() => {});
  const needed = img.clientWidth * devicePixelRatio;
  if (copy.naturalWidth > needed * 1.5) {
    console.warn(img.currentSrc, `${copy.naturalWidth}px loaded, ${Math.round(needed)}px needed`);
  }
}

If an image keeps showing up, its sizes is too wide or its srcset lacks a small enough version. Bear in mind the browser may reuse a larger version it already has cached, so clear the cache before drawing conclusions.

Recent browsers also accept sizes="auto" on images with loading="lazy": because they load after layout, the browser can measure their width itself. Put it first in the list (sizes="auto, (min-width: 1200px) 370px, 100vw") and browsers that don’t understand it use the rest.

picture: switching crops or formats

srcset offers several sizes of one image. To offer different images, you need the picture element. Two cases justify it: a different crop on mobile (a very wide banner becomes unreadable on a narrow screen), and a lighter format such as AVIF with a fallback.

<picture>
  <!-- Mobile: a square crop centred on the product -->
  <source media="(max-width: 600px)"
          srcset="/img/hero-square-600.avif 600w, /img/hero-square-1200.avif 1200w"
          sizes="100vw" type="image/avif">
  <source media="(max-width: 600px)"
          srcset="/img/hero-square-600.jpg 600w, /img/hero-square-1200.jpg 1200w"
          sizes="100vw">

  <!-- Wider screens: the wide crop, in AVIF when supported -->
  <source srcset="/img/hero-1200.avif 1200w, /img/hero-2000.avif 2000w"
          sizes="100vw" type="image/avif">

  <img src="/img/hero-1200.jpg"
       srcset="/img/hero-1200.jpg 1200w, /img/hero-2000.jpg 2000w"
       sizes="100vw" width="2000" height="900"
       alt="Our new range" fetchpriority="high">
</picture>

The browser takes the first source whose media condition and type suit it, then applies the same srcset/sizes logic inside it. The img element is still required: it’s what actually gets displayed, and it carries the alt text, dimensions and loading attributes.

The markup gets heavy fast, so we keep picture for hero images and visuals where cropping really changes readability. For a simple format switch, many hosts and CDNs already serve WebP or AVIF automatically.

In WordPress: what’s automatic and what isn’t

WordPress does a good share of the work: on every upload it creates several sizes, and it adds srcset and sizes to images in post content and to those output by wp_get_attachment_image(). Since version 6.7 it also adds auto to lazy-loaded images. Its default sizes, though, is generic: it assumes the image may fill the whole screen up to its real size. For a grid of cards, that’s far too wide.

The most effective fix is to pass an explicit sizes in your theme templates and register an image size that fits each use:

// A dedicated size for cards (cropped to 3:2)
add_action( 'after_setup_theme', function () {
    add_image_size( 'card', 740, 494, true );
} );

// In the grid template: a sizes value that describes the real layout
echo wp_get_attachment_image( get_post_thumbnail_id(), 'card', false, [
    'sizes' => '(min-width: 1200px) 370px, (min-width: 768px) calc((100vw - 64px) / 2), 100vw',
] );

// The main image at the top of the page: no lazy loading, high priority
echo wp_get_attachment_image( $image_id, 'full', false, [
    'sizes'         => '100vw',
    'loading'       => false,
    'fetchpriority' => 'high',
] );

For images inserted into post content, the wp_calculate_image_sizes filter lets you describe the real width of the text column:

// Images inside post content: the column is 760px wide at most
add_filter( 'wp_calculate_image_sizes', function ( $sizes, $size ) {
    $width = is_array( $size ) ? (int) $size[0] : 0;

    if ( $width >= 760 && is_singular( 'post' ) ) {
        return '(min-width: 800px) 760px, calc(100vw - 32px)';
    }
    return $sizes;
}, 10, 2 );

After adding an image size, images already in the media library don’t have the new version yet. wp media regenerate --only-missing creates it without touching existing files; on a large library, run it outside peak hours.

One last setting that matters for Core Web Vitals: the main image visible on load, often the one that sets LCP, must not use loading="lazy". WordPress tries to guess, but with a header built by a page builder or a custom block it often gets it wrong. Check the source of your main templates.

FAQ

How many versions should srcset include?

In practice, three to five sizes cover most cases, from a phone’s width to a large high-density screen. Beyond that, the gain is small and storage grows quickly.

WebP or AVIF?

AVIF usually compresses better; WebP encodes faster and is very well supported. Both work in current browsers. Test on your own images, because the difference varies a lot with the type of visual.

Correctly sized images often do more for a site’s speed than any caching plugin. We audit and optimise WordPress sites so they stay fast on mobile. Ask us for a performance review.

Share LinkedIn Facebook X WhatsApp
</>

webtestudio

Web agency: custom WordPress sites, premium themes and SaaS software.

Discuss your project →