Skip to content
Tutorials Center
My learning

Topics

Lesson 33 of 122 ยท Images and Media

Responsive Images

Serve the right image to every screen with srcset, sizes and the picture element. Learn w and x descriptors, format fallbacks and art direction.

A photo that looks crisp on a large desktop monitor might be 2,400 pixels wide and weigh 600 KB. Send that same file to a phone with a 400-pixel-wide screen, and the visitor downloads six times more pixels than they can see. Send a small file to a high-density laptop screen, and it looks blurry.

Responsive images solve this. Instead of one src, you describe several versions of the image and let the browser pick the best one for the current screen size, pixel density and supported formats. You still write ordinary <img> elements; you just add a few attributes and, sometimes, a <picture> wrapper.

There are three separate problems, each with its own tool:

Problem Tool
Same image, different screen densities (1x, 2x, 3x) srcset with x descriptors
Same image, displayed at different widths srcset with w descriptors plus sizes
Different crops or newer formats <picture> with <source> elements

Density switching with x descriptors

Many screens pack two or three physical pixels into every CSS pixel. An icon that is 200 CSS pixels wide needs a 400-pixel file to look sharp on such a display.

If an image always appears at the same CSS size, list one file per density in srcset and tag each with an x descriptor:

HTML
<img src="badge-200.png"
     srcset="badge-200.png 1x, badge-400.png 2x, badge-600.png 3x"
     alt="Gold medal badge for completing the course"
     width="200" height="200">

The browser compares the descriptors with the device's pixel ratio and downloads only the best match. The src stays as the fallback for anything that ignores srcset, and width/height still prevent layout shift.

x descriptors

The label painted inside each sample file tells you which one arrived, and currentSrc reports it in JavaScript. On a standard screen you get the small file; on a high-density laptop or phone, a larger one. If you zoom the page, the ratio changes too.

Width switching with w descriptors and sizes

Most content images are fluid: a blog photo might fill the whole screen on a phone and a third of the screen on a desktop. Density alone cannot handle that. Instead, you tell the browser how wide each file really is, and how wide the image will be displayed. The browser does the math.

HTML
<img src="lake-800.jpg"
     srcset="lake-400.jpg 400w,
             lake-800.jpg 800w,
             lake-1200.jpg 1200w"
     sizes="(min-width: 900px) 33vw, 100vw"
     alt="Mountains above a blue lake"
     width="1200" height="750">
  • 400w means "this file is 400 pixels wide". It describes the file, not the screen.
  • sizes is a list of media conditions and widths, checked from left to right. The first condition that matches wins. Here: on viewports at least 900px wide, the image will occupy about a third of the viewport (33vw); otherwise it fills the full width (100vw).
  • The last entry in sizes has no condition. It is the default.

Suppose the viewport is 600px wide on a 2x screen. sizes says the image is 100vw = 600 CSS pixels, which needs 1,200 device pixels, so the browser picks lake-1200.jpg. On a 1x screen, the same viewport needs only 600 pixels, so lake-800.jpg is the smallest file that is big enough.

Why can't the browser just measure the image? It chooses which file to download very early, while the HTML is still arriving and before the CSS layout is known. sizes is your promise about the layout. It does not have to be perfect, but a rough match saves real bandwidth. If you leave sizes out, the browser assumes 100vw.

sizes accepts any CSS length, including px, em and calc():

HTML
sizes="(min-width: 1200px) 380px, (min-width: 700px) calc(50vw - 32px), calc(100vw - 32px)"
w descriptors and sizes

Try changing sizes="100vw" to sizes="150px" and run again: you are telling the browser the image is tiny, so it picks a smaller file (and it then looks stretched, because the CSS still makes it full width). That shows why sizes should match your real layout.

Browsers may keep the bigger file. Once a large version is in the cache, a browser is allowed to keep using it even if you shrink the window. srcset is a set of options, not commands: the browser may also choose a smaller file on a slow or metered connection.

How many sizes should you make?

There is no magic list. A practical approach is three to five widths that cover your layout, for example 400, 800, 1200 and 1600 pixels. Image CDNs and build tools can generate them automatically. Do not mix w and x descriptors in the same srcset.

A newer option, sizes="auto", lets the browser use the actual layout width for lazy-loaded images, since by the time a lazy image loads, the layout is already known. It only works together with loading="lazy", and not every browser supports it yet, so keep a real sizes value as well: sizes="auto, (min-width: 900px) 33vw, 100vw".

The <picture> element

srcset lets the browser choose between versions of the same picture. Sometimes you want more control. <picture> wraps an <img> and adds one or more <source> elements in front of it:

HTML
<picture>
  <source srcset="..." media="..." type="...">
  <source srcset="...">
  <img src="fallback.jpg" alt="..." width="..." height="...">
</picture>

The browser walks through the <source> elements in order and uses the first one that matches. If none match, it uses the <img>. Some rules to remember:

  • The <img> is required and must be the last child. It is the element that actually displays; <picture> and <source> only feed it a URL.
  • alt, width, height, loading and styling all go on the <img>, never on <source>.
  • Inside <picture>, <source> uses srcset (and optionally sizes), not src.

Format fallbacks with type

The type attribute names a MIME type. A browser that does not understand the format skips that source:

HTML
<picture>
  <source srcset="harbor.avif" type="image/avif">
  <source srcset="harbor.webp" type="image/webp">
  <img src="harbor.jpg" alt="Fishing boats tied up in a small harbor" width="1200" height="800">
</picture>

A browser that supports AVIF downloads only the AVIF file. Others fall through to WebP, then JPEG. Each <source> can also carry its own srcset with w descriptors and a sizes attribute, so you can combine format and width switching.

Art direction with media

Scaling an image down is not always enough. A wide landscape shot with a lighthouse on the far right turns into a tiny sliver of lighthouse on a narrow phone. Art direction means serving a different crop that keeps the subject large.

With media, the browser must follow your choice; unlike srcset candidates, it is not a hint:

Art direction with picture

The media query is checked against the width of the preview pane, not your whole screen. If the pane is narrower than 500px you see the tall crop; otherwise the wide one. Change 500px to 200px or 2000px and run again to flip between them. Notice the width and height on the <source> element. When different crops have different shapes, these attributes let each source reserve the right amount of space, so you still avoid layout shift.

Do not use <picture> for simple resizing. If every version shows the same framing, srcset and sizes on a plain <img> are simpler and give the browser more freedom to choose well. Reach for <picture> when you need different crops or format fallbacks.

Putting it together

Here is a realistic article photo that combines everything: modern formats, several widths, an accurate sizes, a fallback <img> with alt and dimensions, and lazy loading because it sits further down the page.

HTML
<picture>
  <source type="image/avif"
          srcset="market-480.avif 480w, market-960.avif 960w, market-1440.avif 1440w"
          sizes="(min-width: 960px) 720px, 100vw">
  <source type="image/webp"
          srcset="market-480.webp 480w, market-960.webp 960w, market-1440.webp 1440w"
          sizes="(min-width: 960px) 720px, 100vw">
  <img src="market-960.jpg"
       srcset="market-480.jpg 480w, market-960.jpg 960w, market-1440.jpg 1440w"
       sizes="(min-width: 960px) 720px, 100vw"
       alt="Shoppers browsing fruit stalls at an open-air market"
       width="1440" height="960"
       loading="lazy">
</picture>

You will combine these techniques with media queries and fluid layouts in Responsive Web Design.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In srcset="photo-800.jpg 800w", what does 800w describe?

    A w descriptor states the file's real width. The display width comes from the sizes attribute.
  2. 2. You use w descriptors but forget the sizes attribute. What does the browser assume?

    Without sizes, the browser assumes the image fills the viewport (100vw), which often leads to downloading a larger file than needed.
  3. 3. When is <picture> with media the right tool?

    That is art direction. For simple resizing or density switching, srcset on a plain img is enough.
  4. 4. Inside <picture>, where does the alt attribute go?

    The img is the element that is actually displayed, so alt, width, height and loading all belong on it.

Summary

  • srcset with x descriptors offers versions for different pixel densities when the display size is fixed.
  • srcset with w descriptors states each file's width; sizes tells the browser how wide the image will be shown.
  • The browser treats srcset as options and picks the best file for the screen, zoom and network.
  • <picture> with <source type> provides format fallbacks such as AVIF โ†’ WebP โ†’ JPEG.
  • <picture> with <source media> does art direction; the browser must follow those choices.
  • The <img> inside <picture> is required, comes last and carries alt, width and height.

Next up: giving images, code and charts a caption with Figures and Captions.