# Bad value for attribute “srcset” on element “img”: Expected width descriptor but found X at Y. (When the “sizes” attribute is present, all image candidate strings must specify a width.)

> Canonical HTML version: https://rocketvalidator.com/html-validation/bad-value-for-attribute-srcset-on-element-img-expected-width-descriptor-but-found-x-at-y-when-the-sizes-attribute-is-present-all-image-candidate-strings-must-specify-a-width
> Attribution: Rocket Validator (https://rocketvalidator.com)
> License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

The `srcset` attribute allows you to provide multiple image sources so the browser can choose the most appropriate one based on the user's device and viewport. There are two distinct modes for `srcset`:

1. **Width descriptor mode** — Each candidate specifies the image's intrinsic width using a `w` descriptor (e.g., `640w`). This mode **requires** the `sizes` attribute so the browser knows how much space the image will occupy in the layout, enabling it to pick the best candidate.
2. **Density descriptor mode** — Each candidate specifies a pixel density using an `x` descriptor (e.g., `2x`). This mode does **not** use the `sizes` attribute; the browser simply matches candidates to the device's pixel density.

These two modes are mutually exclusive. You cannot mix `w` and `x` descriptors in the same `srcset`, and you cannot use `x` descriptors (or bare URLs with no descriptor) when `sizes` is present. The HTML specification is explicit about this: if `sizes` is specified, all image candidate strings must include a width descriptor.

## Why this matters

- **Standards compliance**: The WHATWG HTML Living Standard defines strict parsing rules for `srcset`. When `sizes` is present, the browser's source selection algorithm expects width descriptors. Providing density descriptors or bare candidates in this context violates the spec and produces undefined behavior.
- **Broken image selection**: Browsers rely on the `sizes` attribute to calculate which `w`-described image best fits the current layout width. If you provide `x` descriptors alongside `sizes`, the browser may ignore the `srcset` entirely or fall back to the `src` attribute, defeating the purpose of responsive images.
- **Accessibility and performance**: Responsive images exist to serve appropriately sized files to different devices. An invalid `srcset`/`sizes` combination can result in oversized images being downloaded on small screens (wasting bandwidth) or undersized images on large screens (reducing visual quality).

## How to fix it

You have two options:

1. **Keep `sizes` and switch to width descriptors** — Replace every `x` descriptor (or missing descriptor) in `srcset` with the actual intrinsic pixel width of each image file, expressed with a `w` suffix.
2. **Remove `sizes` and keep density descriptors** — If you only need to serve different resolutions for high-DPI screens at a fixed layout size, drop the `sizes` attribute and use `x` descriptors.

When using width descriptors, the value must match the image file's actual intrinsic width in pixels. For example, if `photo-640.jpg` is 640 pixels wide, its descriptor should be `640w`.

## Examples

### Invalid: `sizes` present with density descriptors

This triggers the error because `1x` and `2x` are density descriptors, but `sizes` requires width descriptors.

```html
<img
  src="photo-640.jpg"
  srcset="photo-640.jpg 1x, photo-1280.jpg 2x"
  sizes="(max-width: 600px) 100vw, 600px"
  alt="A mountain landscape">
```

### Invalid: `sizes` present with a bare candidate (no descriptor)

A candidate with no descriptor defaults to `1x`, which is a density descriptor — still invalid when `sizes` is present.

```html
<img
  src="photo-640.jpg"
  srcset="photo-640.jpg, photo-1280.jpg 2x"
  sizes="(max-width: 600px) 100vw, 600px"
  alt="A mountain landscape">
```

### Fix: use width descriptors with `sizes`

Each candidate now specifies the intrinsic width of the image file. The browser uses the `sizes` value to determine which image to download.

```html
<img
  src="photo-640.jpg"
  srcset="photo-320.jpg 320w, photo-640.jpg 640w, photo-1280.jpg 1280w"
  sizes="(max-width: 600px) 100vw, 600px"
  alt="A mountain landscape">
```

### Alternative fix: remove `sizes` and use density descriptors

If you don't need the browser to choose images based on layout width — for example, the image always renders at a fixed CSS size — drop `sizes` and use `x` descriptors.

```html
<img
  src="photo-640.jpg"
  srcset="photo-640.jpg 1x, photo-1280.jpg 2x"
  alt="A mountain landscape">
```

### Using width descriptors with `source` inside `picture`

The same rule applies to `source` elements inside a `picture`. If `sizes` is present, every candidate must use a `w` descriptor.

```html
<picture>
  <source
    srcset="hero-480.webp 480w, hero-960.webp 960w, hero-1920.webp 1920w"
    sizes="(max-width: 768px) 100vw, 50vw"
    type="image/webp">
  <img
    src="hero-960.jpg"
    srcset="hero-480.jpg 480w, hero-960.jpg 960w, hero-1920.jpg 1920w"
    sizes="(max-width: 768px) 100vw, 50vw"
    alt="A hero banner image">
</picture>
```
