In the Images chapter, we put one image file in src and every visitor downloaded that same file. Now think about who visits a site like this one: some students are on a 27-inch monitor with fast Wi-Fi, many are on a ₹10,000 phone with a 6-inch screen and limited mobile data.
If you send a 1600-pixel-wide, 400 KB photo to that phone, which only needs about 400 pixels, most of the download is wasted. If you send a 400-pixel photo to the big monitor, it looks blurry. Responsive images fix this by offering the browser several files and letting it pick the best one.
Responsive images are images that offer the browser multiple candidate files (different sizes, pixel densities, crops or formats), so each device downloads the most suitable one. HTML provides this through the
srcsetandsizesattributes and the<picture>element.
There are three different problems, each with its own tool:
| Problem | Tool |
|---|---|
| Same image, but the display size changes with the screen | srcset with w descriptors + sizes |
| Same image at a fixed size, but sharp on high-resolution (Retina) screens | srcset with x descriptors |
| A different crop or a different format depending on the situation | <picture> with <source> |
Background: CSS Pixels vs Device Pixels
First, an important idea. Sizes in HTML and CSS are in CSS pixels. Modern phones pack 2 or 3 real (device) pixels into each CSS pixel to make text crisp. That ratio is the device pixel ratio (DPR).
Phone screen: 375 CSS pixels wide, DPR = 3
Real pixels: 375 × 3 = 1125 device pixels wide
An image shown 375 CSS px wide needs a file about 1125 px wide to look perfectly sharp.So the "right" file depends on two things: how wide the image is displayed and the screen's DPR. The browser knows both. You don't. That's why we give the browser choices instead of deciding ourselves.
srcset with Width Descriptors + sizes
This is the most common case: a photo that is full-width on phones and half-width on desktops.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Responsive Image Demo</title>
</head>
<body>
<img src="campus-800.jpg"
srcset="campus-400.jpg 400w, campus-800.jpg 800w, campus-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw, 50vw"
alt="Students walking in front of the main college building">
</body>
</html>Let's decode each attribute.
srcset: The Menu of Files
srcset is a comma-separated list of candidates. Each one is a URL followed by a width descriptor: the file's real width in pixels, written with a w.
campus-400.jpg 400w → "this file is 400 pixels wide"
campus-800.jpg 800w → "this file is 800 pixels wide"
campus-1600.jpg 1600w → "this file is 1600 pixels wide"The w value must match the file's actual width. It's information for the browser, not a display size.
sizes: How Wide the Image Will Be Displayed
The browser chooses an image before it has downloaded your CSS and done the layout, so it can't know how wide the image will appear. sizes tells it:
sizes="(max-width: 600px) 100vw, 50vw"
Read it like an if / else:
if the viewport is at most 600px wide → the image is 100vw (full viewport width)
otherwise → the image is 50vw (half the viewport)Each entry is a media condition followed by a length. The browser uses the first condition that matches.
The last entry has no condition; it's the default.
Lengths can be
vw,px,em, or acalc()such ascalc(100vw - 32px). Percentages are not allowed.If you leave
sizesout, the browser assumes100vw, which makes it download bigger files than needed for images that aren't full-width.
How the Browser Picks: Worked Examples
The browser computes: needed width = slot width (from sizes) × DPR, then picks a candidate that is at least that wide, usually the smallest such one. We tested the example in Chrome and checked which file it actually loaded:
| Device | Viewport | DPR | Slot (from sizes) | Needed | Chrome loaded |
|---|---|---|---|---|---|
| Laptop | 882px | 1 | 50vw = 441px | 441px | campus-800.jpg |
| Basic phone | 375px | 1 | 100vw = 375px | 375px | campus-400.jpg |
| Mid-range phone | 375px | 2 | 100vw = 375px | 750px | campus-800.jpg |
| Flagship phone | 375px | 3 | 100vw = 375px | 1125px | campus-1600.jpg |
On the laptop, the image appeared 441 CSS pixels wide (half the viewport), even though we never set a width. With w descriptors, the browser displays the chosen file at the size given by sizes.
The final choice belongs to the browser. It may pick a smaller file on a slow connection or when data saver is on, or reuse a larger file it already has in its cache. That's the point: you describe the options, the browser decides.
What About src?
Keep a normal src as a fallback for very old browsers that don't understand srcset. In browsers that do, when srcset uses w descriptors, src is not used for the selection. A middle-sized file is a sensible src.
srcset with Density Descriptors (1x, 2x)
Some images always appear at the same CSS size, like a 200 × 100 logo in the header. Only the screen's sharpness varies. For those, use density descriptors:
<img src="logo.png" srcset="logo.png 1x, logo@2x.png 2x"
width="200" height="100" alt="CS Learning Portal">logo.pngis 200 × 100 pixels, for normal screens (1x).logo@2x.pngis 400 × 200 pixels, for screens with DPR 2 or more.
We tested it: on a normal screen Chrome loaded the 1x file, and with the screen scale set to 2 it loaded the 2x file. In both cases the logo occupied the same 200 × 100 CSS pixels; the 2x version was just sharper. No sizes is needed here (in fact, sizes is only meaningful with w descriptors).
| Descriptor | Example | Use when | Needs sizes? |
|---|---|---|---|
w (width) | photo-800.jpg 800w | Display size changes with the layout | Yes (otherwise 100vw is assumed) |
x (density) | logo@2x.png 2x | Display size is fixed | No |
Never mix
wandxdescriptors in the samesrcset. That's invalid. Also don't list the same descriptor twice.
The <picture> Element
srcset assumes every candidate is the same picture at different sizes, and lets the browser choose freely. Sometimes you need to be in control: a different crop for phones, or a newer file format with a fallback. That's what <picture> is for.
<picture>
<source ...>
<source ...>
<img src="fallback.jpg" alt="...">
</picture><picture>is a wrapper. It contains zero or more<source>elements followed by exactly one<img>, which must be last.The browser checks the
<source>elements in order and uses the first one that matches (by itsmediaandtype). If none matches, it uses the<img>.The
<img>is what's actually displayed. The chosen source just changes which file it shows. Soalt,loadingand CSS all go on the<img>.On a
<source>inside<picture>, usesrcset(notsrc).
Use 1: Art Direction
A wide banner of the campus looks great on a laptop, but on a narrow phone the building shrinks to a tiny strip. Better to show a square crop focused on the building:
<picture>
<source media="(max-width: 600px)" srcset="campus-square-600.jpg"
width="300" height="300">
<img src="campus-800.jpg" width="400" height="225"
alt="Students walking in front of the main college building">
</picture>Viewport wider than 600px: Viewport 600px or narrower:
┌──────────────────────────┐ ┌──────────────┐
│ wide 16:9 campus shot │ │ square crop │
└──────────────────────────┘ │ of building │
└──────────────┘Notice the width and height on the <source>. We first tested this without them, and on a 375px-wide viewport the square image was squashed into the <img>'s 400 × 225 box. Because the crops have different shapes, each <source> needs its own width and height (allowed only when the parent is <picture>). With them added, the phone view showed a proper 300 × 300 square.
Rule of thumb: different sizes of the same image →
srcset/sizes(browser chooses). Different content (crop, orientation) →<picture>withmedia(you choose). The browser must obeymedia; it only treatssrcsetas suggestions.
Use 2: Modern Image Formats with a Fallback
AVIF and WebP files are often much smaller than JPEG at similar quality (see the format table in the Images chapter). Offer them first, and let browsers fall back:
<picture>
<source srcset="photo.avif" type="image/avif">
<source srcset="photo.webp" type="image/webp">
<img src="photo.jpg" width="400" height="225" alt="Computer lab with rows of desktops">
</picture>The type attribute lets a browser skip a format it doesn't support without downloading it, just like <source type> in the Audio & Video chapter. In our test, current Chrome displayed the AVIF version. Today all major browsers support both AVIF and WebP, so the JPEG mainly protects very old browsers and other software (some email clients and tools) that read your HTML.
Combining Everything
Each <source> can have its own srcset with w descriptors and its own sizes, so you can mix formats and sizes:
<picture>
<source type="image/avif"
srcset="campus-400.avif 400w, campus-800.avif 800w, campus-1600.avif 1600w"
sizes="(max-width: 600px) 100vw, 50vw">
<img src="campus-800.jpg"
srcset="campus-400.jpg 400w, campus-800.jpg 800w, campus-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw, 50vw"
width="1600" height="900"
alt="Students walking in front of the main college building">
</picture>Browsers with AVIF support pick the best-sized AVIF; others pick the best-sized JPEG.
Don't Forget width, height and Loading
Everything from the Images chapter still applies:
widthandheightlet the browser reserve the right shape (aspect ratio) before the file arrives, so the page doesn't jump. With responsive images, set them to the dimensions of one of the candidates (they all share an aspect ratio), and use the CSS ruleimg { max-width: 100%; height: auto; }so the image scales down to fit while keeping that shape.loading="lazy"for images below the fold. Never lazy-load the main image at the top of the page.fetchpriority="high"on that main (hero) image tells the browser it's more important than other images, which can make it appear sooner.
New: sizes="auto"
Writing sizes by hand means duplicating your CSS layout in HTML. For lazy-loaded images, the browser has already done layout by the time it loads them, so it can measure the real width itself:
<img loading="lazy"
sizes="auto, (max-width: 600px) 100vw, 50vw"
srcset="campus-400.jpg 400w, campus-800.jpg 800w, campus-1600.jpg 1600w"
src="campus-800.jpg" width="1600" height="900"
alt="Students walking in front of the main college building">auto is only valid together with loading="lazy". It arrived in Chrome and Edge 126 (2024), Firefox 150 (April 2026) and Safari 27 (September 2026), so current versions of all major browsers support it, but many people still use older versions. Browsers that don't understand auto skip it and use the rest of the list, which is why you keep normal fallback sizes after it.
Common Mistakes
Using
wdescriptors withoutsizes. The browser assumes 100vw and downloads a much bigger file than needed for a half-width image.Wrong
wvalues.800wmust be the file's real pixel width, not the display width.Percentages in
sizeslike50%. Usevw,px,emorcalc().Mixing
wandxin onesrcset.Forgetting the
<img>inside<picture>, or putting it before the sources. Without it, nothing is displayed.Putting
alton<picture>or<source>. It belongs on the<img>.Using
<picture>just for sizes. If it's the same image in different sizes, a plain<img srcset sizes>is simpler and lets the browser optimise.Different crops without
width/heighton each<source>, which distorts the image.
Interview Questions
Q1. What is the difference between srcset with w descriptors and <picture> with media?
srcset with w descriptors and <picture> with media?srcset with w lists the same image at different resolutions; the browser treats it as hints and chooses based on layout width, DPR, network and cache. <picture> with media is for art direction: the browser must use the first <source> whose media condition matches, so you control which content is shown.
Q2. A page has sizes="(max-width: 600px) 100vw, 50vw" and srcset with 400w, 800w and 1600w files. Which file does a 375px-wide phone with DPR 2 typically load?
sizes="(max-width: 600px) 100vw, 50vw" and srcset with 400w, 800w and 1600w files. Which file does a 375px-wide phone with DPR 2 typically load?375px ≤ 600px, so the slot is 100vw = 375 CSS px. Needed width = 375 × 2 = 750px. The smallest candidate at least that wide is the 800w file. (The browser may still choose differently, for example because of cache or data saver.)
Q3. Why does the browser need sizes at all? Can't it look at the CSS?
sizes at all? Can't it look at the CSS?To start downloads early, the browser's preload scanner selects images while parsing HTML, before CSS is loaded and layout is done. sizes gives it the display width in advance. For lazy-loaded images, layout is already known, which is why sizes="auto" is allowed only with loading="lazy".
Q4. Why put type on <source> when offering AVIF and WebP?
type on <source> when offering AVIF and WebP?So a browser that doesn't support the format can skip that source without downloading it, and move on to the next source or the <img> fallback.
Q5. When should you use x descriptors?
x descriptors?When the image is always displayed at the same CSS size (logos, icons, avatars). Provide 1x and 2x (and maybe 3x) files; the browser picks based on the device pixel ratio. No sizes is needed.
Conclusion
Responsive images let each device download the right file: srcset and sizes for different sizes of the same image, x descriptors for fixed-size images on sharp screens, and <picture> when you must control the crop or format.
wdescriptor = the file's real width;sizes= how wide it will be displayed.Needed pixels ≈ slot width × device pixel ratio; the browser makes the final choice.
<picture>: sources are checked in order, the first match wins, and the<img>is required and last.Use
mediafor art direction andtypefor AVIF/WebP with a JPEG fallback.Keep
alt,width,heightandloadingon the<img>; give each differently shaped<source>its ownwidth/height.
Next, in Canvas & SVG, we'll move from images stored in files to graphics drawn by code.