OSHTML.007: Responsive Images — srcset, sizes, picture and Accessible Image Selection

Desktop, tablet and phone displaying landscape imagery at different sizes.

A webpage may display the same photograph on a 360-pixel-wide phone, a tablet, or a desktop monitor. Sending the largest file to every visitor wastes bandwidth, while sending only a small image can produce blurry results. HTML provides native resource selection through img, srcset, sizes and picture. Continue from OSHTML.006: Semantic HTML and apply accessible markup to responsive media.

Prerequisites and learning objectives

Use a modern browser, a plain-text editor and a local folder. Review document structure, HTML attributes and responsive CSS sizing. By completion, learners can write accessible img markup; distinguish intrinsic image width, CSS width and device-pixel ratio; implement srcset and sizes; select art direction or alternative formats with picture; and diagnose the selected resource using browser developer tools.

1. Accessible images and intrinsic dimensions

The src attribute names the fallback image resource. The alt attribute supplies equivalent text for informative images, such as “A network switch with four installed optical transceivers.” Decorative imagery that contributes no information uses alt=””. A linked image acting as a button needs an accessible name describing its purpose. Avoid repeating an adjacent caption verbatim. The width and height attributes can reserve space at the image’s aspect ratio before downloading, reducing layout shifts. For a 1200 by 630 image, use <img src="devices.png" alt="Desktop, tablet and phone displaying landscape imagery" width="1200" height="630">. CSS can make the image fit its container with img { max-width: 100%; height: auto; }.

2. CSS pixels, physical pixels and resolution selection

Intrinsic width is the width of the source file in image pixels. CSS width describes the space occupied in layout. Device-pixel ratio (DPR) relates device pixels to CSS pixels. An image displayed at 400 CSS pixels on a DPR 2 screen may benefit from a source approximately 800 image pixels wide. Browser selection also depends on caching, zoom, available candidates and implementation policy, so the arithmetic is a target rather than a guarantee.

The width-descriptor syntax lists actual candidate widths: <img src="landscape-800.jpg" srcset="landscape-400.jpg 400w, landscape-800.jpg 800w, landscape-1200.jpg 1200w" sizes="(max-width: 600px) 100vw, 600px" alt="Mountain landscape" width="1200" height="630">. The first matching sizes condition describes the anticipated display width; the last value is the fallback. For a viewport of 400 CSS pixels the declared slot is 400 CSS pixels. At 1000 CSS pixels the slot is 600 CSS pixels. The CSS layout must independently match these hints: img { display: block; width: 100%; max-width: 600px; height: auto; }. Width descriptors must equal actual source widths. Density descriptors such as 1x and 2x instead describe alternative pixel densities for a fixed-size image; do not mix descriptor types in one srcset.

Web Dev Simplified — Make Your Site Lightning Fast With Responsive Images. Watch on YouTube.

3. Art direction and format negotiation

Resolution switching preserves the composition. Art direction changes the crop or composition to keep essential detail visible. The picture element lets authors declare alternatives in priority order. For example: <picture><source media="(max-width: 600px)" srcset="portrait.jpg"><img src="wide.jpg" alt="Technician inspecting a rack"></picture>. Use an img fallback inside every picture. Place alt on img rather than source. When switching aspect ratios, supply appropriate dimensions on source where supported, and test layout stability.

Format alternatives use a type hint. For example: <picture><source type="image/avif" srcset="landscape.avif"><source type="image/webp" srcset="landscape.webp"><img src="landscape.jpg" alt="Mountain landscape"></picture>. The browser can choose a supported format. Keep the visual meaning equivalent across alternatives. Avoid marking the main above-the-fold image loading=”lazy” if it delays important content; lazy loading is useful for below-the-fold imagery.

Advanced HTML Image Techniques — srcset, picture, WebP and AVIF, preload, figure and figcaption. Watch on YouTube.

4. Hands-on browser lab

Open the MDN live responsive-images demonstration and its source markup. The demonstration includes an art-directed picture and a width-based image selection example. Open DevTools, enable the device toolbar, set a narrow viewport and reload with cache disabled in the Network panel. Filter requests to images. Record the selected filenames and their transferred sizes. Repeat at a desktop viewport and at another DPR. In the console, document.querySelector('picture img').currentSrc reports the resolved picture image URL; document.querySelectorAll('img') lets you locate the resolution-switching image. Compare window.devicePixelRatio and the image’s getBoundingClientRect().width with the source width. A cached larger resource may be reused, so do not assume resizing always causes a new request.

For an original local exercise, export the same owned or openly licensed landscape at 400, 800 and 1200 pixels wide, preserving aspect ratio. Save them as landscape-400.jpg, landscape-800.jpg and landscape-1200.jpg. Create index.html using the document skeleton from OSHTML.001, put the markup from section 2 in main, and add the CSS in style within head. Include <meta name="viewport" content="width=device-width, initial-scale=1"> in head. Open index.html locally. No accounts, server credentials or production website changes are needed. Confirm all candidate files exist, alt text describes the actual picture and layout remains within the viewport.

5. Practice and assessment

Exercise A: a 500 CSS-pixel image at DPR 2 has a nominal target of how many source pixels? Exercise B: for the sizes expression in section 2, determine the slot at viewport widths of 360 and 900 CSS pixels. Exercise C: explain which element carries alt when picture is used. Exercise D: if a portrait crop is required on phones, choose between CSS shrinking, srcset alone and picture. Exercise E: rename one local candidate to create a broken path, observe the failed request, restore the filename and reload. Never alter a production page for the experiment.

Answer key

A: approximately 1000 image pixels, subject to browser resource selection. B: 360 and 600 CSS pixels, respectively. C: img. D: picture supports a media-dependent composition; srcset alone primarily offers alternative resolutions. E: the Network panel identifies the failed resource; a valid path restores access. Do not assume every browser automatically retries a different candidate after a failed request.

Glossary and authoritative references

Intrinsic width: source image width in pixels. DPR: device pixels per CSS pixel. Slot size: anticipated rendered width described by sizes. Art direction: selection of alternative compositions. currentSrc: resolved URL of the selected image resource. For normative requirements, see the WHATWG HTML image standard, MDN responsive images guide, img reference and picture reference.

We volunteer daily to ensure the credibility of the information on our platform is Verifiably True.

One response to “OSHTML.007: Responsive Images — srcset, sizes, picture and Accessible Image Selection”

  1. […] min-width: 0 in a row layout and appropriate text wrapping. Use responsive image techniques from OSHTML.007 when adding images. Keep reading and keyboard focus order consistent with the HTML source; do not […]

    Like

Leave a Reply