AD JB Toolbox 01 EN

HTML Tutorial / Responsive Images: srcset, sizes, and picture

Responsive images offer a browser files with different resolutions or compositions so it can choose one suited to the display. Use srcset and sizes for resolution choices of the same image, and picture when a narrower screen needs a different crop.

Get the example image files

These examples use the same lake photo at three widths and a square crop that keeps the tree prominent. Download the files into the same folder as your HTML file to try the relative URLs unchanged.

The first three preserve the scene and aspect ratio. The square file has a different composition for a narrow display.

Choose image resolution with srcset and sizes

The 480w, 960w, and 1600w descriptors in srcset give each file's actual pixel width. sizes describes the image's expected layout width under different viewport conditions.

<img class="example-fluid"
  src="html-image-sample-lake-tree-960-v1.webp"
  srcset="html-image-sample-lake-tree-480-v1.webp 480w,
          html-image-sample-lake-tree-960-v1.webp 960w,
          html-image-sample-lake-tree-1600-v1.webp 1600w"
  sizes="(max-width: 800px) 100vw, 800px"
  alt="A tree on a small island in a lake, with mountains beyond"
  width="1600" height="900">

This assumes the image is about as wide as the viewport up to 800 CSS pixels, and no wider than 800 CSS pixels above that. If your layout differs, change sizes to match it. src supplies a fallback for environments without srcset support.

.example-fluid {
  display: block;
  width: 100%;
  max-width: 800px;
  height: auto;
}

This CSS applies only to the first example, which has the example-fluid class. The browser can also consider device pixel density and zoom. A narrow viewport therefore does not guarantee that the 480-pixel file will be chosen. sizes describes displayed CSS pixels, not the width of the downloaded file.

Use density descriptors for a fixed display width

If an image is always displayed at 480 CSS pixels wide, you can offer 1x and 2x candidates. Do not use sizes with this form.

<img class="example-fixed"
  src="html-image-sample-lake-tree-480-v1.webp"
  srcset="html-image-sample-lake-tree-480-v1.webp 1x,
          html-image-sample-lake-tree-960-v1.webp 2x"
  alt="A tree on a small island in a lake, with mountains beyond"
  width="480" height="270">
.example-fixed {
  display: block;
  width: 480px;
  height: auto;
}

The first example's CSS does not apply to this image. This CSS fixes the displayed width at 480 pixels, so the image may overflow a narrower container. If the display width needs to change with the layout, use the earlier w and sizes approach. Do not mix w and x descriptors in one srcset.

Change the crop with picture

picture can offer a different composition instead of merely a different resolution. This example uses the square crop at viewport widths up to 600 pixels and a landscape image otherwise.

<picture>
  <source media="(max-width: 600px)"
          srcset="html-image-sample-lake-tree-square-800-v1.webp"
          width="800" height="800">
  <img src="html-image-sample-lake-tree-960-v1.webp"
       alt="A tree on a small island in a lake, with mountains beyond"
       width="960" height="540">
</picture>

Square crop of a lake photo centered on the tree

The photo above shows the square-crop file used on narrow screens; this photo in the article does not itself switch with viewport width. picture still needs an img element with fallback content and alt. Each source supplies a condition and file candidate; the image's alternative text belongs on img.

Check which image the browser selected

Save all four image files, keeping their filenames, next to index.html. Put an HTML example inside body and its CSS in a style element in head or in a separate stylesheet. To test against the actual viewport width on mobile, add <meta name="viewport" content="width=device-width, initial-scale=1"> to head.

  1. Open index.html in a browser and open the developer tools' Network panel.
  2. Change the viewport width, disable the cache, and reload the page. Check the image request filenames to see which candidates loaded.
  3. For the picture example, reload once at a viewport width of 600 pixels or less and once above 600 pixels. Check for the square crop at the narrower width and the landscape file at the wider width.

You can also check these values in the console. currentSrc shows the image URL currently selected by the browser. The first command targets the srcset example; the second targets the picture example.

document.querySelector('.example-fluid').currentSrc
document.querySelector('picture img').currentSrc

Even at the same viewport width, pixel density and zoom may change which srcset file is selected. If a larger file is already loaded, narrowing the window may not immediately replace it with a smaller one. Change the conditions, disable the cache, and reload before comparing.

If the wrong image is selected

  • Check that each w descriptor matches the file's actual pixel width.
  • Compare sizes with the CSS width at each viewport size.
  • Use same-composition files for resolution switching and separate crops with picture.
  • Keep a meaningful alt on the img inside picture.

For the detailed selection rules, see MDN's responsive images guide and the WHATWG HTML image specification.

More in This Category
HTML Tutorial / Syntax

HTML Tutorial / Syntax

Learn the essential rules of HTML syntax, including elements, tags, attributes, void elements, comments, nesting, and the structure of a complete HTML document.

HTML Tutorial / Lists: ul, ol, dl, and Nested Lists

HTML Tutorial / Lists: ul, ol, dl, and Nested Lists

Choose the right HTML list for ordered items, unordered items, and name–description pairs. Try a complete example, custom numbering, and correctly nested lists.

HTML Tutorial / Responsive Images: srcset, sizes, and picture

HTML Tutorial / Responsive Images: srcset, sizes, and picture

Offer image resolutions with srcset and sizes, and switch crops with picture. Downloadable files let you test the examples in a browser.

HTML Tutorial / Links: Paths, Page Fragments, and Email

HTML Tutorial / Links: Paths, Page Fragments, and Email

Create HTML links with absolute or relative URLs, jump to a document fragment, and add email, new-tab, or download links. Learn what to verify before publishing.

HTML Tutorial / Basic Structure of an HTML Document

HTML Tutorial / Basic Structure of an HTML Document

Learn the roles of doctype, html, head, and body, then save your first HTML file and open it in a browser. Includes a complete example, a diagram, and fixes for common filename, refresh, and encoding problems.

HTML Tutorial / Images: img, Alt Text, and Dimensions

HTML Tutorial / Images: img, Alt Text, and Dimensions

Add an image with HTML img, write useful alternative text, and set width and height correctly. A downloadable example helps you test file paths and sizing.

HTML Tutorial / Text Semantics: Emphasis, Quotations, Abbreviations, and Code

HTML Tutorial / Text Semantics: Emphasis, Quotations, Abbreviations, and Code

Learn when to use HTML em, strong, q, blockquote, abbr, code, and kbd. Examples show how each element conveys meaning rather than just changing the appearance of text.

HTML Tutorial / Headings and Paragraphs

HTML Tutorial / Headings and Paragraphs

Learn when to use h1–h6 headings, p paragraphs, and br line breaks in HTML. Examples show document hierarchy, browser output, and common mistakes.

HTML Tutorial / What Is HTML and What Does It Do on a Web Page?

HTML Tutorial / What Is HTML and What Does It Do on a Web Page?

HTML gives web content structure and meaning. Learn how it differs from CSS and JavaScript, see how basic markup appears in a browser, and make your first HTML file.