◉ANTIGRAVITY LABJP
Articles/Tips & Best Practices
✦ Tips & Best Practices/2026-11-17Intermediate

Why a Small sizes Value Shrinks the Image Itself, and the CSS Line to Add When width and height Attributes Stretch It

The day I added responsive images, one artwork shrank and another stretched sideways. How sizes sets the displayed size, the one CSS line for width and height attributes, and how to work out sizes for tall art in a frame, with numbers and a check script.

srcsetsizesresponsive imagesPageSpeedPlaywright6indie development31Antigravity416

✦ Premium Article

It was late July, and I was running PageSpeed on an artist's official site I build and maintain. Under "Properly size images" I saw the names of artworks I knew well. On phones, a painting that only ever filled a slot of roughly 380×507 was being served at its full 1108×1477. The wasted transfer came to 734 KiB.

I thought I knew the fix. Generate a few WebP widths, add srcset and sizes, done. I worked through it with an agent, shipped it, and opened the live page. The artwork at the top had become noticeably smaller. To lock the aspect ratio I then added width and height attributes, and then the hero image stretched sideways. Two breakages, two different causes, one afternoon.

Both had the same root. A value I treated as "which file should load" was also deciding "how big it should look". Below are the numbers from reproducing both, the way I decide these values now, and the small script I run before every release.

sizes decides the displayed size, not only the file

sizes tells the browser, ahead of layout, roughly how wide the image will be. From that and the screen density the browser chooses a candidate from srcset. Most explanations stop there.

What they rarely mention is the next step. An image chosen from a w-descriptor srcset takes the width you declared in sizes as its own intrinsic width, not the pixel width of the file. If your CSS never sets width on that image, it is drawn at that intrinsic width. Underestimate sizes, and you do not just download a smaller file; the picture on screen shrinks.

My layout held artwork in check with max-height: 74vh and percentage max-width, and never set width. I reasoned that CSS already capped the size, so a conservative sizes would be harmless. That reasoning was the first breakage.

What I measured

I rebuilt the situation with Playwright's Chromium in headless mode. From a 1108×1477 image I generated 320, 480, 800 and 1108 WebP variants, and styled the page with nothing but max-height: 74vh; max-width: 90vw. Then I read the rendered box and currentSrc on a desktop-like viewport (1280px, 1x) and a phone-like one (390px, 3x).

SetupDesktop 1280px, 1xPhone 390px, 3x
A. sizes="200px" (conservative guess)200×267 (320 file)200×267 (800 file)
B. sizes="(max-width: 860px) 90vw, 900px"444×592 (1108 file)351×468 (1108 file)
C. width and height attributes only1108×592 (stretched wide)351×625 (stretched tall)
D. C plus width:auto; height:auto444×592351×468

In row A the CSS would allow 444px, yet the image stops at 200px. On the phone the browser correctly fetches the 800 file for the 3x screen, and still draws it at 200px. The download is right and only the appearance is wrong, so the network panel shows nothing unusual.

The other surprise: in row A, naturalWidth returned neither 320 nor 800, but 200. If you assume naturalWidth is the file's pixel width, your checking script will misread the page too. With srcset, you get the value already divided by the chosen density.

✦

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
✦Before adding srcset, you can pick a sizes value from your own layout that never shrinks the image, knowing that sizes decides the displayed size as well as which file loads
✦You can prevent the sideways stretch that appears the moment you add width and height attributes with one CSS line before release, instead of hunting for the cause afterwards
✦Even where tall artwork sits inside a fixed frame with object-fit contain, you can work out sizes from the width the art actually occupies and make the browser pick your small variants
Secure payment via Stripe · Cancel anytime
✦

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Antigravity Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • ✦Copy-paste ready implementation code
  • ✦New advanced guides published daily
  • ✦$5/mo or $10 for lifetime access
View Membership →

Related Articles

✦ Tips2026-10-19
After I Rewrote the URL, Only the Not-Yet-Loaded Images Returned 404: Checking replaceState and Relative Paths
After rewriting the URL with history.replaceState when moving from a list to a detail view, only the images that had not loaded yet returned 404. Native loading=lazy broke too. Here are a minimal test page, a check script, and three fixes compared.
✦ Tips2026-10-15
The Images Weren't Broken, My Way of Checking Them Was: Doubting the iframe First, Then Reading currentSrc
A record of misdiagnosing healthy images as unloaded because I audited them through an iframe in a separate tab. In a hidden frame, naturalWidth reads 0 at a rendered width of 0, and lazy images never start. Here are the numbers I reproduced locally, an audit script that reads currentSrc and rendered width on the real page, and the trap of bulk-replacing CSS values with a regex.
✦ Tips2026-10-30
The sed -i That Worked on Linux and Stalled on My iMac: Checking an Agent's Steps Before You Run Them
A sed -i step that ran fine on Linux can stall on a Mac or leave backup files behind. Here is what I reproduced with GNU sed, the forms that behave the same on both, and a short check to run first.