Explain it simply
Explain it simply
Think of a clothes shop. If it sells a single size, the clothes are too big for children and too tight for adults. A responsive image offers the same picture in S, M and L, and lets the shopper, the browser, measure itself and take the size that fits.
The browser knows how wide its screen is and how dense its pixels are (1x or 2x). Your job is to supply the list of files (srcset) and how wide the image will be on the page (sizes). The browser does the rest of the arithmetic.
Problem → Why → Solution
The problem: making a phone download a 3 MB photo
A 4000-pixel-wide photo straight from a camera is added with a single img and shrunk with CSS. It looks great on a desktop, but a phone 375 pixels wide downloads the full file too. On a slow mobile connection the picture takes seconds to appear and burns the visitor's data for nothing.
<!-- one 4000px, 3.2 MB file for every device -->
<img src="bagan-original.jpg" alt="Temples of Bagan at sunrise"
style="width: 100%;">Why isn't shrinking with CSS enough?
CSS only decides how large the picture is drawn on screen; it does not change which file is downloaded. With a single src the browser has no choice, so every device downloads every byte. Only when you provide a srcset can the browser pick a smaller file that suits its screen.
Core concept: srcset lists the choices, sizes states the slot
srcset is a comma-separated list of candidates. Each candidate is a file URL plus a descriptor: photo-800.jpg 800w says "this file is 800 pixels wide". The browser has to choose before downloading anything, which is why you state each file's width up front.
But the browser chooses before the CSS has loaded, so it does not yet know how wide the image will be on the page. sizes tells it. sizes="(max-width: 800px) 100vw, 800px" means "at a viewport of 800px or less the image fills the screen width, otherwise it is 800px wide". Media conditions are tested left to right, the first match wins, and the last value without a condition is the default. Leave sizes out and it defaults to 100vw.
Next the browser multiplies the slot width by the device pixel ratio. A 375px phone with a 2x screen needs 375 × 2 = 750 device pixels, so the 800w file is the smallest one that is big enough. A 1x screen needs only 375, so it takes the 400w file. The spec gives the browser more freedom than this: it may choose a smaller file in data-saver mode or reuse a larger one already in its cache. srcset is advice, not a command.
Keep src as well; it is the fallback for tools that do not understand srcset. The width and height attributes tell the browser the aspect ratio in advance so it reserves space before the file arrives, and max-width: 100%; height: auto; in CSS keeps the picture inside its container without distortion.
- Read the slot width from sizes
- Multiply by the device pixel ratio
- Pick the smallest w file that is enough
- CSS draws the image into the slot
srcset and sizes decide which file is downloaded; CSS decides how large it is drawn.
General pattern
<!-- width switching: same picture, different file widths -->
<img src="name-medium.jpg"
srcset="name-small.jpg SMALLw, name-medium.jpg MEDIUMw, name-large.jpg LARGEw"
sizes="(media-condition) slot-width, default-slot-width"
width="W" height="H" alt="Description">
<!-- density switching: fixed-size image -->
<img src="icon.png" srcset="icon.png 1x, icon@2x.png 2x"
width="W" height="H" alt="Description">When the image width follows the layout, use w descriptors with sizes; for images that never change size, such as a logo or avatar, use x descriptors. Never mix w and x descriptors in one srcset.
Example: a landscape photo in a content column
This photo fills the screen on a phone and stays inside an 800px column on larger screens. Three files are offered, and the output shows which one the browser picks on four kinds of device.
<style>
img { max-width: 100%; height: auto; }
</style>
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1200.jpg 1200w"
sizes="(max-width: 800px) 100vw, 800px"
width="800" height="533"
alt="Hot-air balloons over the temples of Bagan at sunrise">Code breakdown
- src
src="photo-800.jpg"The fallback for tools that do not understand srcset. A middle-sized file is the usual choice.
- srcset
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1200.jpg 1200w"Three files, each with its real pixel width (w). The w values must match the actual file widths, or the browser's arithmetic will be wrong.
- sizes
sizes="(max-width: 800px) 100vw, 800px"At viewports of 800px or less the slot is 100vw; otherwise it is 800px. This mirrors the CSS max-width: 100%; sizes must restate what the CSS will do.
- width / height
width="800" height="533"Tells the browser the aspect ratio (800:533, about 3:2) so it reserves space before the file arrives and the text below does not jump.
- CSS
img { max-width: 100%; height: auto; }Stops the image from overflowing its container, and height: auto prevents the width/height attributes from stretching it.
One photo appears: full screen width on a phone, 800px wide on large screens. On a first visit (empty cache) the file downloaded is:
375px phone, 1x → photo-400.jpg
375px phone, 2x → photo-800.jpg
1280px desktop, 1x → photo-800.jpg
1280px desktop, 2x → photo-1200.jpg
If a file is missing, the alt text is shown instead.picture: art direction and format fallbacks
srcset is for different sizes of the same picture. When a phone should get a different crop, such as a tight square around the person, you need art direction, and that is what picture is for. The browser checks the source elements in order and uses the first whose media or type matches; if none does, it uses the img. The img is required, and alt lives on the img only.
<picture>
<source media="(max-width: 600px)" srcset="team-square.jpg"
width="600" height="600">
<source type="image/avif" srcset="team-wide.avif">
<img src="team-wide.jpg" width="1200" height="500"
alt="Our five-person support team in the Yangon office">
</picture>When to use loading="lazy"
Adding loading="lazy" to images further down the page delays their download until the user scrolls near them. Do not lazy-load the hero image at the top; it is the first thing people see and would appear late.
In real projects
Where this shows up in real projects
Production sites do not export every image size by hand. A build tool, CMS or image CDN takes one original and generates widths such as 400, 800 and 1200 plus AVIF/WebP versions, then writes the srcset for you. Framework components such as Next.js's Image component also output exactly this srcset and sizes markup behind the scenes, so understanding this lesson is what lets you pass them a correct sizes value.
Teams check image performance with the Network tab in browser DevTools and with tools such as Lighthouse. They look at which file is downloaded under phone emulation and whether an image is far larger than the size it is displayed at. On e-commerce sites product photos are often most of the page weight, so a correct srcset tends to make mobile pages noticeably faster and lighter on data.
Common mistakes
Common mistakes
1. Giving w descriptors but forgetting sizes
Without sizes the browser assumes the image is 100vw. If it is really a 300px sidebar image, a 1440px desktop at 2x thinks it needs 2880 pixels and downloads the biggest file. Always write a sizes value that matches the CSS layout.
2. Leaving out width/height so the layout jumps
Without the attributes the browser learns the height only after the download, so the text below suddenly jumps down and people mis-tap. Write the file's real width and height and add height: auto in CSS.
3. Using picture just to switch sizes
Sources with media queries force the browser to use that file, so it can no longer account for density or data saving. If only the size differs, use srcset and sizes on the img and keep picture for changing the crop or for format fallbacks.
Best practices
Best practices
- Write sizes by looking at the CSS layout, and update it whenever the layout changes.
- Three or four widths (for example 400, 800, 1200, 1600) are usually enough; you do not need ten.
- Check which file is downloaded yourself, using DevTools device emulation with the cache disabled.
- Write one alt per picture, on the img, describing what it shows rather than its file name.
Mini challenge
Mini challenge: a hero image that changes crop
Write the hero image for a travel blog post. Screens wider than 700px get a landscape crop, narrower screens get a square crop, and each crop exists in two widths.
- Use picture, with media="(min-width: 701px)" on the landscape source
- Landscape: hero-wide-1000.jpg (1000w), hero-wide-2000.jpg (2000w); square: hero-square-600.jpg (600w), hero-square-1200.jpg (1200w)
- The image is full width, so use sizes="100vw"
- The img has alt, width and height
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Trip report</title>
<!-- TODO: CSS so the image never overflows and keeps its ratio -->
</head>
<body>
<h1>Three days in Bagan</h1>
<!-- TODO: picture
- source for wide screens: two landscape widths
- img fallback: two square widths, sizes, width/height, alt -->
</body>
</html>Expected: On a first visit with an empty cache: 375px at 1x → hero-square-600.jpg, 375px at 2x → hero-square-1200.jpg, 1000px at 1x → hero-wide-1000.jpg, 1280px at 1x → hero-wide-2000.jpg. If a file is missing, the alt text is shown.
Show hint
The browser checks sources in order, so put the landscape source with its media condition first. Putting the square crop on the img makes it the default for narrow screens where the source does not match.
Show solution
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Trip report</title>
<style>
body { margin: 0; }
img { display: block; max-width: 100%; height: auto; }
</style>
</head>
<body>
<h1>Three days in Bagan</h1>
<picture>
<source media="(min-width: 701px)"
srcset="hero-wide-1000.jpg 1000w, hero-wide-2000.jpg 2000w"
sizes="100vw" width="2000" height="800">
<img src="hero-square-600.jpg"
srcset="hero-square-600.jpg 600w, hero-square-1200.jpg 1200w"
sizes="100vw" width="1200" height="1200"
alt="Hot-air balloons drifting over Bagan's temples at sunrise">
</picture>
</body>
</html>Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- srcset lists file candidates with their pixel widths (w); sizes states how wide the image's layout slot is.
- The browser computes slot width × device pixel ratio and picks the smallest file that is big enough.
- Use 1x/2x descriptors for fixed-size images, and keep picture for changing the crop or for format fallbacks.
- width/height plus max-width: 100%; height: auto; prevents layout shift.