CHAPTER 21 · HTML

Responsive images: offer the right picture for the available space

A phone and a wide monitor do not always need the same image file. Learn how srcset, sizes and picture help the browser choose, while keeping a normal img as the foundation.

Download example files (ZIP)

Two different problems

Imagine printing the same poster for a small noticeboard and a large wall. You need different amounts of detail, but the message stays the same. On the web, resolution switching offers different-sized files of the same picture.

Sometimes a wide group photograph becomes unreadable when reduced. You may need a closer crop, meaning a version that shows a smaller part of the scene. Choosing a different composition for a smaller space is called art direction.

Neither task is the same as making the displayed image fit its container. CSS controls that layout. HTML image candidates help choose the resource to fetch. We will learn CSS in its own lessons; the examples here keep a fixed, modest display width to isolate the HTML choice.

Start with src and alt

Keep a useful img with src, alt, width and height. Add responsive choices around that working starting point. All candidate files must actually exist; a list of filenames does not generate resized images.

The ZIP includes images/desk-360.png and images/desk-720.png. They show the same original drawing at actual widths of 360 and 720 pixels. Both have the same proportions. The larger file can supply more detail when the display needs it.

Srcset with density descriptors

Srcset supplies candidate image addresses and descriptors: short labels explaining their size or density. A density descriptor such as 2x means the candidate supplies twice as many image pixels per CSS pixel at the intended display size.

HTML fragment — part of a document
<img src="images/desk-360.png"
     srcset="images/desk-360.png 1x, images/desk-720.png 2x"
     alt="A notebook and pencil on a study desk" width="360" height="220">

At 360 CSS pixels wide, the 720-pixel candidate can serve a high-density display. The browser makes the final choice, considering factors such as density and its own resource policy. Do not promise that every device always downloads the exact same candidate. With x descriptors, sizes is not needed.

Complete program: density-desk.html

density-desk.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Study Desk Image Candidates</title>
</head>
<body>
  <h1>One drawing, two detail levels</h1>
  <p>The browser can choose between two versions of the same drawing.</p>
  <img src="images/desk-360.png" srcset="images/desk-360.png 1x, images/desk-720.png 2x" alt="A notebook and pencil on a study desk" width="360" height="220">
</body>
</html>
Browser previewDocument title: Study Desk Image Candidates

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: one drawing appears, not two pictures side by side. A sharper candidate does not change the intended display width to 720. To inspect the selected resource, open browser developer tools, inspect the img and look at its currentSrc property or the Network panel. This is an inspection task, not a JavaScript requirement for the page.

Width descriptors and sizes

A width descriptor gives a candidate's actual intrinsic width, meaning the width of the resource itself. Write 360w for the 360-pixel file, not 360px and not an invented preferred display size.

HTML fragment — part of a document
<img src="images/desk-360.png"
     srcset="images/desk-360.png 360w, images/desk-720.png 720w"
     sizes="360px"
     alt="A notebook and pencil on a study desk" width="360" height="220">

Here, sizes says that the intended slot is 360 CSS pixels wide. On a higher-density screen, that slot may still need the 720-pixel file. Do not mix w and x descriptors in the same srcset. When using w candidates, provide an accurate sizes value; otherwise the browser generally assumes a viewport-wide slot.

A conditional example is sizes="(max-width: 600px) 90vw, 540px". It means a slot of 90 percent of the viewport width at viewport widths up to 600 pixels, otherwise 540 pixels. Viewport means the area where the browser displays the page, and vw measures a percentage of its width. This hint must match the real CSS layout. Sizes does not create that layout itself, and a percentage such as 90% is not a valid slot-width substitute here.

Picture for a different crop

Picture lets you put source choices before the final img. A source's media condition says when it applies. The browser considers suitable sources in order and uses the first applicable supported choice. The img remains the displayed image element and supplies alt.

Match the image choice to the problem
  1. 1Same picture, extra detail: srcset
  2. 2Width candidates: describe the slot with sizes
  3. 3Different crop: picture and source media
  4. 4Always keep img with useful alt

Resource selection does not create CSS layout. Test the actual slot and selected file.

Complete program: picture-desk.html

picture-desk.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Choose a Closer Study View</title>
</head>
<body>
  <h1>Keep the notebook easy to see</h1>
  <p>Narrow viewports use a closer view; wider viewports use the full desk.</p>
  <picture>
    <source media="(max-width: 600px)" srcset="images/notebook-close.png">
    <img src="images/desk-360.png" alt="The notebook prepared for a writing task" width="360" height="220">
  </picture>
</body>
</html>
Browser previewDocument title: Choose a Closer Study View

This preview shows the example in a browser. Save the complete code in local files to test links between pages and downloads. Same-page section links work here.

Expected result: one image. In your saved page, resize the browser across 600 pixels to compare the close view and full drawing. The embedded preview has its own viewport; a narrow tutorial column can select the close view even on a large monitor. Both versions express the same task, so one truthful alt can serve them.

Format alternatives

Source can also supply type, such as image/webp, when offering different encodings of an image. Put supported alternatives first and an ordinary img fallback last. The type value is a media-type label, not a file converter. Merely renaming a PNG to .webp does not change its format.

Use source srcset inside picture. For audio and video in the next lessons, source uses src instead. Source is a void element in both cases; it has no closing source tag.

Problem and solution: the large file is always chosen

Problem: a student lists 360w and 720w candidates but leaves out sizes, despite displaying the image in a narrow column.

Solution: provide a slot-size hint matching the layout. Recheck in developer tools. A previously cached larger resource can still be reused, so resizing alone is not proof of a wrong rule. Compare the condition, slot and selected resource rather than guessing from appearance.

Common mistakes and useful tips

  • Thinking srcset displays every candidate at once.
  • Mixing x and w descriptors in one set.
  • Giving a false intrinsic width or forgetting to create the image files.
  • Expecting sizes to style the image width.
  • Omitting the img inside picture or putting alt only on source.
  • Describing only one crop when the other crop needs different information.
  • Using many image variants without checking whether they improve the page.

Practice questions

  1. What is the difference between resolution switching and art direction?
  2. What does 720w describe?
  3. Is sizes required for a 1x/2x candidate set?
  4. Does sizes create a responsive CSS layout?
  5. Where does alt belong in a picture example?
  6. Save both complete examples with their image folder. Compare the close and full views at different viewport widths, then explain why a browser might reuse a cached larger image.
Check your practice answers
  1. Resolution switching changes resource detail for the same composition. Art direction chooses a different crop or composition suitable for the space.
  2. The candidate file's actual intrinsic width of 720 image pixels.
  3. No. Sizes is used with width descriptors rather than density descriptors.
  4. No. It describes the intended slot to resource selection; the layout must match separately.
  5. On the img element.
  6. Keep all supplied image files together. Check the picture condition in a normal browser, inspect the selected resource and remember that cache and browser policy affect requests.

Interview questions

How do srcset and sizes work together?

Srcset lists resource candidates and their actual widths. Sizes describes the intended display slot under different conditions. The browser combines that information with density and other factors when choosing a resource.

Why does picture still need img?

Img is the image element that renders the selected resource and provides replacement text. The preceding source elements describe choices; they are not separate visible images.

Does a higher-density image necessarily display larger?

No. It can supply extra pixels for the same CSS display dimensions. Display size and resource detail are related but different decisions.

Can HTML choose a closer crop without JavaScript?

Yes. Picture with suitable source media conditions can express art direction. Include a valid img fallback and keep its alt accurate across the alternatives.

Quick revision

  • Offer real candidates with honest descriptors.
  • Use x for density choices, or w with accurate sizes for width choices.
  • Picture can switch crops or formats; img still renders the result.
  • Test resource selection and layout as separate concerns.