# How the srcset Attribute Works for Responsive Images

> Learn how the srcset attribute optimizes images for responsive design. Deliver sharp visuals and save bandwidth by letting browsers choose the best image resolution for any device.

- Repository: [H5BP/Front-end-Developer-Interview-Questions](https://github.com/h5bp/Front-end-Developer-Interview-Questions)
- Tags: deep-dive
- Published: 2026-03-05

---

**The `srcset` attribute enables browsers to automatically select optimal image resolutions based on device pixel density and viewport width, delivering sharp visuals while conserving bandwidth.**

The `srcset` attribute is a cornerstone of modern responsive image delivery, enabling browsers to choose optimal assets dynamically. As documented in the `h5bp/Front-end-Developer-Interview-Questions` repository—specifically in [`src/questions/html-questions.md`](https://github.com/h5bp/Front-end-Developer-Interview-Questions/blob/main/src/questions/html-questions.md)—understanding this HTML feature is essential for front-end developers optimizing web performance across devices.

## Parsing srcset Candidates and Descriptors

When the browser encounters an `<img>` element containing `srcset`, it first parses the comma-separated list of image URLs, each optionally followed by a descriptor. According to the HTML specification referenced in the interview questions file, two descriptor types control selection behavior:

### Width Descriptors (w)

The **`w` descriptor** (e.g., `800w`) indicates the intrinsic width of the image in pixels. This allows the browser to calculate which asset best fits the available space when combined with the `sizes` attribute. For example, a candidate listed as `photo-medium.jpg 960w` signals to the browser that this file is 960 pixels wide.

### Pixel Density Descriptors (x)

The **`x` descriptor** (e.g., `2x`) specifies the pixel-density multiplier the image targets. This descriptor is ideal for high-DPI (Retina) displays where physical pixels exceed CSS pixels. A value of `2x` indicates the image contains twice as many pixels per dimension as standard displays, ensuring crisp rendering on dense screens.

## The Browser Selection Algorithm

The selection process occurs entirely client-side, as implemented in the HTML standard logic. The browser follows these sequential steps to determine which resource to fetch:

1. **Parse all candidates** – Extract URLs and their associated `w` or `x` descriptors from the `srcset` string.

2. **Calculate effective width** – If a `sizes` attribute is present, evaluate media conditions to determine the image's display width; otherwise assume 100% of viewport width.

3. **Select optimal candidate** – 
   - With `w` descriptors, calculate required resource width (`viewport-width × device-pixel-ratio`) and pick the smallest candidate that meets or exceeds this value.
   - With `x` descriptors, match the candidate's density value to the device's device-pixel-ratio.

4. **Fetch chosen resource** – Download only the selected image, ignoring other candidates to minimize bandwidth usage.

This algorithm ensures mobile devices receive smaller assets while desktop Retina displays fetch high-resolution variants.

## Implementing srcset in HTML

The `srcset` attribute works within standard `<img>` tags alongside the fallback `src` attribute. Here are practical implementations demonstrating width-based and density-based selection:

### Width-Based Responsive Images

Use `w` descriptors when serving different image sizes based on viewport width:

```html
<img
  src="photo-small.jpg"
  srcset="
    photo-small.jpg 480w,
    photo-medium.jpg 960w,
    photo-large.jpg 1440w
  "
  sizes="(max-width: 600px) 480px, (max-width: 1200px) 960px, 1440px"
  alt="A scenic landscape">

```

### Pixel Density for Retina Displays

Use `x` descriptors when targeting high-DPI screens with identical image dimensions:

```html
<img
  src="icon-1x.png"
  srcset="
    icon-1x.png 1x,
    icon-2x.png 2x,
    icon-3x.png 3x
  "
  alt="App icon">

```

## Advanced Usage with picture and sizes

For complete art direction—serving entirely different image crops rather than just resolutions—combine `srcset` with the `<picture>` element. This approach allows distinct images for mobile versus desktop layouts:

```html
<picture>
  <source
    media="(max-width: 600px)"
    srcset="hero-mobile.jpg 1x, hero-mobile-2x.jpg 2x">
  <source
    media="(min-width: 601px)"
    srcset="hero-desktop.jpg 1x, hero-desktop-2x.jpg 2x">
  <img src="hero-fallback.jpg" alt="Hero banner">
</picture>

```

In this example from the h5bp interview questions context, the browser evaluates media queries first, then selects the appropriate density from the `srcset` list within each `<source>` element.

## Summary

- **The `srcset` attribute** provides a comma-separated list of image candidates with `w` (width) or `x` (density) descriptors.
- **Browser-side selection** occurs entirely client-side, with no server-side processing required.
- **Bandwidth optimization** results from downloading only the image resource that matches the device's display characteristics.
- **Descriptor types** serve different use cases: `w` for responsive layouts, `x` for high-DPI displays.
- **Integration with `sizes`** allows explicit control over intended display dimensions.
- **Art direction** requires the `<picture>` element combined with `srcset` for viewport-dependent image swaps.

## Frequently Asked Questions

### What is the difference between srcset and the picture element?

The **`srcset` attribute** provides alternative resolutions of the same image, allowing the browser to choose based on technical criteria like pixel density and viewport size. The **`<picture>` element** offers syntactic sugar for art direction, enabling developers to specify entirely different images (different crops, compositions, or formats) based on media queries. While `srcset` solves resolution switching, `<picture>` solves content switching.

### When should I use w descriptors versus x descriptors?

Use **`w` descriptors** when the image will display at different sizes depending on viewport width, such as responsive layouts where an image spans 100% width on mobile but 33% on desktop. Use **`x` descriptors** when the image displays at fixed CSS pixel dimensions across all devices, but you want to provide high-resolution versions for Retina displays (e.g., icons fixed at 64×64px CSS pixels).

### Does srcset work without the sizes attribute?

Yes, `srcset` functions without `sizes`, but the browser assumes a default size of 100% viewport width when calculating which `w` descriptor to select. This often leads to suboptimal choices on desktop viewports where images typically occupy less than full screen width. **Including `sizes`** provides the browser with accurate layout information earlier in the page lifecycle, preventing unnecessary downloads of overly large images.

### How does srcset improve web performance?

The `srcset` attribute improves performance through **intelligent asset delivery**: mobile devices on 3G networks receive small, compressed images while desktop Retina displays receive high-resolution assets. This eliminates the waste of serving 2× or 3× images to standard displays or forcing high-DPI users to view blurry, upscaled low-resolution images. According to the h5bp/Front-end-Developer-Interview-Questions source, this client-side selection mechanism reduces unnecessary data transfer and improves perceived page load speed.