# Responsive Images

> `srcset` offers the browser a menu of image files at different sizes. `sizes` tells it how big the image will display. Together they let the browser serve the smallest file that still looks sharp.

Subject: HTML | Lesson 19 of 42 | 6 min read
Interactive lesson: https://codewithanimation.in/html/responsive-images
Previous: [width, height & Layout Shift](https://codewithanimation.in/html/width-height-and-cls.md) | Next: [Image Performance & Inline SVG](https://codewithanimation.in/html/image-performance-and-svg.md)

## What they are

`srcset` is an attribute on `<img>` that lists multiple image files with size descriptors. Width descriptors (`480w`, `1200w`) describe each file's intrinsic width in pixels. The browser reads the list, consults the `sizes` attribute to find out how wide the image will actually display, factors in the screen's pixel density, and downloads the smallest file that still covers the display need.

`sizes` is a media-condition list that maps viewport widths to display widths: `sizes="(max-width: 640px) 100vw, 50vw"` tells the browser the image fills the full viewport on small screens and half of it on larger ones. Without `sizes`, the browser assumes the image is always `100vw` wide, which causes it to download unnecessarily large files when the image only occupies part of the layout.

## Why it matters

A single image served at full desktop resolution to a mobile phone wastes the user's bandwidth and slows the page. A single image sized for mobile served to a 4K monitor looks blurry. `srcset` solves both: phones download the small file, desktops download the large one, and the browser makes the decision automatically based on the available information.

Device pixel ratio compounds the problem. A modern phone with a 2x display needs an image twice as wide as its CSS pixel count to look sharp. `srcset` with width descriptors accounts for this automatically: a phone that displays the image at 400 CSS pixels and has a 2x display will look for a source at least 800px wide.

## How it works

List files from smallest to largest in `srcset` with their intrinsic widths. Write `sizes` to match your layout: if the image is full-width on mobile but sits in a 50% column on desktop, write `sizes="(max-width: 768px) 100vw, 50vw"`. Always include a plain `src` as the fallback for browsers that do not support `srcset`.

For pixel density only (no responsive layout): use `x` descriptors instead of `w`. `srcset="photo.jpg 1x, photo@2x.jpg 2x"` serves the 2x image to Retina screens and the 1x to standard screens. This is simpler but does not help with different image sizes in different layouts.

Use `<picture>` when you need different image crops for different viewports (art direction), or when you want to serve a modern format like WebP or AVIF with a JPEG fallback. The browser tries each `<source>` in order and picks the first one whose `media` condition or `type` it supports. The `<img>` inside `<picture>` is the required fallback.

## Common mistake: Using srcset without sizes

Without `sizes`, the browser has no information about the image's layout width, so it falls back to assuming the image is 100vw. On a 1440px desktop where the image is actually 600px wide in a sidebar, the browser downloads the 1440w source instead of the 600w one: more than twice the bytes for no visible difference. Adding the correct `sizes` value is the fix, and it requires no server changes.

## Tip: Let your build tool compute srcset automatically

Maintaining srcset by hand means tracking file names, widths, and keeping them in sync when images change. Tools like Next.js `<Image>`, Astro's image integration, and Vite plugins (vite-imagetools) read the source image and generate the resized variants and the srcset attribute automatically. In plain HTML, Cloudinary and Imgix URL-based transformation services let you generate responsive srcset at request time without pre-generating files.

## Try it: Try it

Select a viewport size and watch the browser choose a different source from the `srcset`. Notice how device pixel ratio multiplies the display need.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/responsive-images

## Check yourself

### Question 1

An image is in a 400px sidebar. The srcset has 400w, 800w, and 1600w sources. Without a sizes attribute, which source does the browser pick on a 1440px viewport?

- A. The 1600w source, because the browser assumes 100vw (1440px)
- B. The 400w source, because the image is only 400px wide
- C. The 800w source, as a reasonable middle ground

Correct answer: A

Without `sizes`, the browser assumes `100vw`. On a 1440px viewport that means it looks for a source at least 1440px wide, picks the 1600w file, and downloads far more than it needs. Adding `sizes="400px"` would let it pick the 400w source instead.

- Why not B: Without sizes, the browser cannot know the image is in a 400px sidebar. It assumes 100vw and picks the source that covers 1440px, which is the 1600w file.
- Why not C: The browser does not guess a middle ground. Without sizes it assumes 100vw for the viewport in use and picks the source closest to that width.

### Question 2

When should you use <picture> instead of img srcset?

- A. When you need a different crop or a different format at different breakpoints
- B. Whenever you have more than two image sizes
- C. <picture> is always preferred over srcset for responsive images

Correct answer: A

`<picture>` is the right tool for art direction (portrait crop on mobile, landscape crop on desktop) or format switching (AVIF with WebP fallback, WebP with JPEG fallback). For same-image multiple-size serving, `srcset` with `w` descriptors and `sizes` is cleaner.

- Why not B: Multiple sizes is exactly what srcset with w descriptors handles. <picture> is for art direction (different crops) or format switching (WebP vs JPEG), not just more size variants.
- Why not C: For the common case of same image at different sizes, srcset + sizes on <img> is simpler and equally effective. <picture> adds power only when you need different images or formats.

### Question 3

A phone displays an image at 390 CSS pixels and has a 3x device pixel ratio. What is the minimum source width the browser will look for?

- A. 1170px (390 x 3)
- B. 390px (the CSS display width)
- C. 780px (390 x 2, always assuming 2x)

Correct answer: A

The browser multiplies the display width from `sizes` by the device pixel ratio to get the actual pixel need. On a 3x display showing a 390px image, the browser looks for the closest source to 1170px or larger.

- Why not B: The browser multiplies the CSS display width by the device pixel ratio to get the pixel need. A 3x screen needs three physical pixels per CSS pixel, so 390 CSS pixels requires a 1170px image for sharp rendering.
- Why not C: The browser uses the actual device pixel ratio, not an assumed 2x. A 3x device needs 390 x 3 = 1170 physical pixels.

## Remember this

- `srcset` with `w` descriptors lists source files by intrinsic width; the browser picks the best fit based on display need and DPR.
- `sizes` tells the browser the CSS display width of the image at each viewport; without it, the browser assumes `100vw`.
- Always include a plain `src` fallback for browsers that do not support `srcset`.
- Use `<picture>` for art direction (different crops) or format switching (WebP/AVIF fallbacks), not just more size variants.
