You resize your hero photo, lower the JPEG quality, and ship three responsive versions. Then you open the page on a phone and discover that it still downloads the largest one. Before you touch the quality slider again, check sizes: the browser might think the image takes up the whole screen when it actually sits in a narrow column.
An image compression workflow has two jobs: make each file smaller, and make sure the browser picks a file that fits the layout. Here’s a small demo that lets you test those two decisions separately.
Export three versions from one source
Use a landscape JPEG you own, ideally about 1800 × 1200 pixels. On macOS, put it in a new demo folder as hero-original.jpg, then run:
mkdir -p images
for width in 480 800 1200; do
sips --resampleWidth "$width" --setProperty format jpeg \
--setProperty formatOptions 78 hero-original.jpg \
--out "images/hero-${width}.jpg"
done
wc -c hero-original.jpg images/hero-*.jpg
On another OS, export the same three widths with your usual image editor. The HTML doesn’t depend on the encoder. Here, 78 is a starting point for a photograph, not a setting to reuse for every asset. Compare gradients and fine texture against the original. For screenshots with small text, compare PNG or lossless WebP before settling on JPEG.
Keep the original. If a candidate looks rough, export it again from the source, not from the JPEG you already compressed.
Make sizes match the layout
Save this as index.html next to the images folder:
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Responsive image demo</title>
<style>
body { margin: 0; }
.hero { width: min(calc(100% - 32px), 600px); margin: 24px auto; }
.hero img { display: block; width: 100%; height: auto; }
</style>
<main class="hero">
<h1>Responsive image demo</h1>
<img
src="images/hero-800.jpg"
srcset="images/hero-480.jpg 480w,
images/hero-800.jpg 800w,
images/hero-1200.jpg 1200w"
sizes="(max-width: 632px) calc(100vw - 32px), 600px"
width="1200"
height="800"
alt="A mountain landscape reflected in a lake"
>
</main>
</html>
Adjust the alt text and the aspect ratio in width and height to match your own photo. Each w descriptor must match the file’s actual pixel width. The sizes attribute doesn’t set the CSS width; it tells the browser what width the layout is expected to give the image. See the HTML standard’s responsive image model.
At a viewport width of 390 CSS pixels, the slot is about 358 pixels wide. At 2× device pixel ratio, that’s roughly 716 physical pixels, so the 800-pixel file is a sensible candidate. The browser makes the final choice; don’t treat any one filename as guaranteed.
Check what the browser actually downloads
From the demo directory, start a local server and open http://localhost:8000:
python3 -m http.server 8000 --bind 127.0.0.1
- In Chrome DevTools, set the viewport to 390 CSS pixels and the device pixel ratio to 2. Open Network, filter to Img, enable Disable cache, and reload. Record the requested filename and its transfer size. The Network reference explains how to read these values.
- To see the effect of a wrong hint, use a 1100-pixel viewport at 1×. First reload with the correct
sizes: the browser is told the slot is 600 pixels wide. Then changesizesto100vwand reload: the hint now says 1100 pixels, even though the layout hasn't changed. Compare which file is requested in each run. The two hints favor different candidates, but the browser makes the final choice. - Run the same check on your deployed page. If you use a CDN, check the response type as well: the file delivered to the browser may use a different encoding than the one in your build folder.
If the request is hard to identify in the Network panel, this shows which URL the browser selected and how wide it rendered the image:
const image = document.querySelector('.hero img');
console.log({
source: image.currentSrc,
cssWidth: Math.round(image.getBoundingClientRect().width),
dpr: window.devicePixelRatio,
});
currentSrc tells you which URL was selected; use the Network panel to see whether and how many bytes were transferred.
Fix the expensive part first
If a phone is getting an unnecessarily large candidate, fix the layout hint before lowering quality. If it’s getting the right candidate but the file is still too large, change the encoding and compare the result at its actual display size. A smaller export is useful only if it’s the one your readers receive.
Top comments (0)