← dominic.balass.one

sizes="auto": let the container pick the file

A follow-up to the <picture> post. Hand-written sizes goes stale the moment your CSS changes. With loading="lazy" and sizes="auto", the browser measures the image's laid-out width and picks the right srcset file itself. Same image, three containers, three different downloads below.

The one-line fix

Delete your hand-written sizes media query and write auto instead. That is the whole change. The browser does the measuring your CSS used to promise.

sizes-auto.html
<img src="hero-wide-640.jpg"
     srcset="hero-wide-320.jpg 320w,
             hero-wide-640.jpg 640w,
             hero-wide-1280.jpg 1280w"
     sizes="auto"
     loading="lazy"
     width="1280" height="549"
     alt="Lighthouse on a coastal cliff at golden hour">
Why this beats hand-written sizes. Without any sizes, the browser assumes 100vw and downloads for your whole viewport even when the image renders in a 300px card. A hand-written sizes="(max-width: 900px) 100vw, 900px" fixes that until the layout changes and nobody updates the markup. auto reads the real slot width, so it cannot go stale.

Watch it work

The exact same image markup in three different containers. Each readout shows the slot width, the file the browser actually chose, and what a 100vw fallback would have downloaded. Resize the window, hit re-run, and watch the files change.

A. Full-width article — sizes="auto"
Lighthouse on a coastal cliff at golden hour
B. Card grid (~400px cards) — sizes="auto"
Lighthouse on a coastal cliff at golden hour
Lighthouse on a coastal cliff at golden hour
C. Sidebar column (~280px) — sizes="auto"
Lighthouse on a coastal cliff at golden hour

This is the case hand-written sizes gets wrong most often: a narrow rail inside a wide page. Nobody writes a media query for "the sidebar", so the image quietly downloads the desktop file. auto just measures the rail.

D. The mistake: same sidebar, sizes="100vw"
Lighthouse on a coastal cliff at golden hour

This is what the browser does when sizes is missing or wrong: it assumes the image spans the viewport and grabs the biggest file it needs for that. Same 280px slot as C, wildly different download.

Selection happens once per load, not on resize: the browser picks a file when it starts fetching. That is why the re-run button exists. Shrink the window, re-run, and compare A against C.

Browser support

BrowserSupport
Chrome / Edge126+ (June 2024)
Firefox150+ (April 2026)
Safari / iOS SafariNot supported
BaselineNo — blocked by Safari

Roughly 70% global coverage as of mid-2026, and under 1% of page loads actually use it. Genuinely underused.

Honest notes

Do not "fix" the demo by removing loading="lazy". Without it, sizes="auto" is invalid and the browser falls back to 100vw. If every image on the page is above the fold, you do not need lazy loading and you do not need auto either. This attribute is for below-the-fold images in real layouts.