Responsive Images srcset: Serve the Right File Size

By Anders Vik ·

Responsive Images with srcset and sizes: A Working Guide

A reader wrote in last month with a familiar complaint. He'd added responsive images srcset markup to his product pages exactly the way a tutorial showed him: three files listed in srcset, a sizes attribute copied from the same example, done. Except the browser was still pulling down the 2400px original on a 400px phone screen.

Nothing was broken. The markup validated, the page rendered fine, and imagecheck.tools still flagged the image as oversized.

That's the part that trips people up. srcset and sizes are not decorative extras you bolt onto an img tag and forget about. They're instructions, and if the instructions don't match your actual page layout, the browser follows them anyway, faithfully, to the wrong result.

I've spent a lot of years reading server logs and watching bandwidth get burned on files nobody needed, and this specific mistake, a sizes value that lies about the layout, is the one I see most often. Let's work through what each piece actually does, one at a time, with real markup you can check against your own pages.

How srcset, sizes, and picture Actually Work Together

What is srcset?

srcset is an attribute on the img element that lists a set of interchangeable image files, each one a different version of the same picture. Not different designs, not different crops (that's a job for a different element, which we'll get to). Just the same image at different intrinsic widths or pixel densities, so the browser can pick whichever file fits the situation instead of you guessing on its behalf.

There are two ways to write that list, and mixing them in the same srcset is not allowed. The HTML Living Standard is specific about this: if any candidate in a srcset uses a width descriptor (the "w" suffix), every candidate in that same srcset has to use a width descriptor too.

Density descriptors

The simpler form uses "x" descriptors and skips sizes entirely, because the image is always displayed at the same CSS size, you're just offering sharper files for high-density screens:

<img
  src="logo.png"
  srcset="logo.png 1x, [email protected] 2x, [email protected] 3x"
  width="160"
  height="48"
  alt="Company logo">

This tells the browser: at 1x pixel density use logo.png, at 2x use [email protected], at 3x use [email protected]. No sizes attribute needed, because the display size never changes, only the sharpness of the file does. This pattern fits logos, icons, and small fixed-size graphics well. It does not fit a hero image that shrinks and grows with the viewport, which is where width descriptors come in.

Width descriptors

<img
  src="photo-800w.jpg"
  srcset="photo-480w.jpg 480w, photo-800w.jpg 800w, photo-1200w.jpg 1200w"
  sizes="(max-width: 600px) 100vw, 50vw"
  width="800"
  height="533"
  alt="A warehouse loading dock at sunrise">

Here each number after the file name is the file's actual intrinsic pixel width, measured once, at export time, not a guess. 480w means that file is genuinely 480 pixels wide. This is the form that needs a sizes attribute, because without one the browser has no idea how large the image will actually render on the page, and it will assume 100vw (the full viewport width) by default, which is usually wrong and usually means it grabs your biggest file every time.

What is the sizes attribute for?

This is where most of the confusion actually lives, and I've seen the same misreading on a developer forum thread and in plenty of tickets I used to handle myself: people expect sizes to be a list of media queries that picks a file directly, the way a picture element's source conditions do. It isn't.

sizes describes the image's rendered layout width under different conditions. It never names a file. The browser reads that layout width, then goes and picks a matching file from srcset on its own.

The algorithm, per MDN's guide to responsive images, works like this: the browser reads the sizes list left to right, stops at the first condition that matches, multiplies that slot width by the device's pixel density, then picks the closest srcset candidate at or above that pixel figure. First match wins. Anything after it is never even evaluated, even if it's also technically true.

One more rule worth knowing before you write a sizes value: it can't use percentages. Only absolute lengths like 480px, or viewport-relative units like 50vw. The WHATWG HTML Living Standard spells out why: a percentage would be ambiguous about what it's relative to (the viewport? the parent element? something else?), so the spec rules it out entirely.

The bug that causes almost every oversized flag

Here's the failure mode. Say your image sits inside a two-column layout, so it actually renders at roughly half the viewport width on desktop. But the sizes value, copied from a tutorial, still says 100vw:

<!-- Wrong: claims full width, but CSS only gives it half -->
<img
  src="photo-1200w.jpg"
  srcset="photo-480w.jpg 480w, photo-800w.jpg 800w, photo-1200w.jpg 1200w"
  sizes="100vw"
  width="1200"
  height="800"
  alt="Product shot">

On a 1400px wide screen, the browser trusts what you told it: the image needs the full 1400px slot, so at 1x density it reaches for the 1200w file, the largest one available, even though your CSS only actually hands the image about 700px of space. That's a needless download roughly twice the size it should be, and it's exactly what imagecheck.tools's oversized issue code is built to catch: an intrinsic width at least 2.5 times larger than the space the image actually occupies, with no working srcset compensating for it.

Fix it by matching sizes to the real CSS behavior:

<!-- Correct: matches the two-column layout at desktop widths -->
<img
  src="photo-800w.jpg"
  srcset="photo-480w.jpg 480w, photo-800w.jpg 800w, photo-1200w.jpg 1200w"
  sizes="(max-width: 600px) 100vw, 50vw"
  width="800"
  height="533"
  alt="Product shot">

Under 600px viewport width, the image is assumed full width, so it gets the biggest file it needs. Above that, it's assumed to occupy half the viewport, so the browser reaches for a smaller, cheaper candidate.

Open your browser's network panel, resize the window, and reload at a few widths. If the requested file size tracks the change, sizes is doing its job. If it doesn't budge, go check what sizes actually claims against what your CSS actually does.

When do I need the picture element?

srcset and sizes handle resolution switching: the same image, offered at different sizes, with the browser choosing. The picture element exists for two different jobs entirely, and mixing these up is the second most common mistake I run into after the sizes bug above.

Art direction

Sometimes you don't want the same image smaller, you want a genuinely different crop or composition at different viewport widths, a tighter portrait crop on mobile instead of a wide landscape shot squeezed down. That's art direction, and it needs picture, because srcset alone has no concept of "swap to a different image entirely" based on layout:

<picture>
  <source media="(max-width: 600px)" srcset="hero-portrait.jpg">
  <source media="(min-width: 601px)" srcset="hero-landscape.jpg">
  <img src="hero-landscape.jpg" width="1200" height="675" alt="Team meeting in the office">
</picture>

Format negotiation

The other legitimate use is offering a modern format like AVIF or WebP with a JPEG fallback for browsers that don't support it, using the type attribute so the browser skips formats it can't decode:

<picture>
  <source type="image/avif" srcset="photo.avif">
  <source type="image/webp" srcset="photo.webp">
  <img src="photo.jpg" width="800" height="533" alt="Product shot">
</picture>

Both examples share a structural rule from web.dev's guide to the picture element: one or more source elements, then exactly one img as the last child, mandatory as a fallback. Drop the img and older or misbehaving browsers have nothing to fall back to.

Here's the part people miss: if your images are just the same picture at different sizes, with no crop change and no format negotiation, reach for plain img with srcset and sizes instead of picture.

A picture element's media conditions are hard rules the browser must obey. Plain srcset with sizes is a set of hints the browser can override, which means a browser in a data-saving mode can quietly choose a smaller file on its own. Picture with rigid media queries can't do that. Use the lighter tool unless you specifically need art direction or format fallback.

A complete srcset example

Putting the pieces together, here's a full, realistic block for a blog post's featured image, spanning the full width on mobile and half the width in a two-column layout above 700px:

<img
  src="article-hero-800w.jpg"
  srcset="
    article-hero-480w.jpg 480w,
    article-hero-800w.jpg 800w,
    article-hero-1200w.jpg 1200w,
    article-hero-1600w.jpg 1600w"
  sizes="(max-width: 700px) 100vw, 50vw"
  width="800"
  height="450"
  loading="lazy"
  alt="Diagram of the responsive image selection process">

Line by line: src is the fallback file for any browser that ignores srcset entirely, an ancient safety net that costs nothing to keep. srcset lists four candidate files at their real intrinsic widths. sizes tells the browser the image occupies the full viewport under 700px and half of it above that threshold.

width and height set the image's aspect ratio (800:450, a 16:9 crop) so the browser can reserve the right amount of space before any file finishes downloading. loading="lazy" defers the fetch until the image nears the viewport, which is worth doing on anything below the fold; our guide to lazy loading images covers when that tradeoff helps and when it can actually hurt.

Do width and height attributes still matter with srcset?

Yes, for two separate reasons, and neither of them goes away just because srcset is present.

First, the layout-shift reason, which has nothing to do with responsive images specifically. Modern browsers calculate an image's default aspect ratio directly from its width and height attributes and reserve that exact amount of space in the layout before any file, srcset candidate or not, has finished downloading. Skip the attributes and the browser has no ratio to reserve space with, so the page jumps once the image arrives. That's a measurable width and height attributes CLS problem, and it's the exact mechanism behind Google's Cumulative Layout Shift guidance, not a vague style preference.

Second, a newer and more specific reason: sizes="auto". Chrome and Edge shipped this in version 126 (June 2024), and Firefox 150 picked it up in April 2026, letting the browser measure an image's actual rendered layout width itself instead of you hand-writing a sizes value, on lazy-loaded images specifically. It sounds convenient, and it is, but it comes with a hard requirement: it only works correctly when width and height are set.

Without them, the spec mandates the browser treat the image as exactly 300 by 150 pixels for the purpose of picking a srcset candidate, "no exceptions," as spec explainer Eric Portis documented when the feature shipped. That guard exists to stop an infinite measure-load-remeasure loop, but it means a missing width or height silently locks you into a tiny fallback size regardless of how the image actually displays.

Safari hasn't shipped sizes="auto" as of this writing, so treat it as a progressive enhancement rather than something to depend on everywhere, but the underlying requirement, keep width and height on every responsive image, holds regardless of which browsers support it.

Diagnosing the four related issue codes

If you run a page through imagecheck.tools, four issue codes map directly back to the mistakes covered above:

  • oversized: the image's real intrinsic width is at least 2.5 times larger than what its width attribute claims, with no srcset doing the work of offering a smaller file. Usually a sizes value that overstates the layout, or no srcset at all.
  • upscaled: the opposite problem, the intrinsic file is smaller than the size it's actually displayed at, so the browser is stretching a low-resolution image and it looks soft.
  • aspect_mismatch: the width and height attributes don't match the file's real proportions, which throws off the reserved layout space and can cause the exact distortion or shift the attributes were meant to prevent.
  • no_dimensions_srcset: srcset is present but width and height are missing, which means you're getting none of the aspect-ratio protection and, if sizes="auto" is in play, the 300x150 fallback trap described above.

Run your page through the tool, check whether any of those four flags come up next to your images, and you'll know exactly which piece of markup to revisit rather than guessing at the whole block.

A quick decision checklist

Need a genuinely different crop or composition at different breakpoints, or a modern format with a fallback? Reach for picture. Need the same image, just smaller on small screens? Reach for srcset and sizes on a plain img.

Either way, set width and height on every image, every time, regardless of which approach you use. It's the one piece of markup that never becomes optional.

For background on what to put in your alt attributes once the sizing is sorted, our alt text guide covers that ground. And if you're serving AVIF or WebP through server-level content negotiation rather than a picture element, htaccess.tools has rewrite rules for exactly that. The full family of checking tools is worth a look too if you're auditing more than just images on a page.

Frequently Asked Questions

What is srcset?

srcset is an HTML attribute on the img element that lists a set of interchangeable files, either the same image at different pixel densities (using 1x, 2x style descriptors) or at different intrinsic pixel widths (using w descriptors), so the browser can choose the version that best fits the screen it's rendering on instead of always downloading one fixed file.

What is the sizes attribute for?

sizes tells the browser how wide the image will actually render under different layout conditions, expressed as a list of media conditions paired with lengths (never percentages). The browser reads that rendered width, multiplies it by the screen's pixel density, and then matches the result against the candidates listed in srcset. It does not name or choose a file directly, and mismatching it against your real CSS layout is the single most common cause of unnecessarily large image downloads.

When do I need the picture element?

Use picture when you need art direction (a genuinely different crop or image per breakpoint) or format negotiation (offering AVIF or WebP with a JPEG fallback via the type attribute on source elements). If you're serving the same image just at different sizes, plain img with srcset and sizes is the better fit, because it lets the browser override your hints in a data-saving mode, which picture's hard media conditions don't allow.

Do width and height attributes still matter with srcset?

Yes. They let the browser calculate the image's aspect ratio and reserve its layout space before any file downloads, which prevents layout shift regardless of whether srcset is present. They're also now a hard requirement for the newer sizes="auto" feature in Chromium and Firefox: without them, the browser falls back to a fixed 300 by 150 pixel size for choosing a srcset candidate, no exceptions.