# Bad value X for attribute “sizes” on element “source”: Bad media condition: Parse Error at Y

> Canonical HTML version: https://rocketvalidator.com/html-validation/bad-value-x-for-attribute-sizes-on-element-source-bad-media-condition-parse-error-at-y
> Attribution: Rocket Validator (https://rocketvalidator.com)
> License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

The `sizes` attribute tells the browser how wide an image will be displayed at different viewport sizes, so it can choose the best candidate from `srcset`. It only applies when `srcset` uses **width descriptors** (e.g., `480w`, `800w`). The value is a comma-separated list where:

1. Each entry (except the last) is a **media condition** in parentheses followed by a **CSS length** — for example, `(min-width: 800px) 50vw`.
2. The **final entry** is a fallback length with no media condition — for example, `100vw`.

The browser evaluates the media conditions from left to right and uses the length from the first one that matches. If none match, it uses the fallback.

The media condition syntax mirrors CSS media queries but **without** the `@media` keyword. You can use logical operators like `and`, `or`, and `not`, and features like `min-width`, `max-width`, `orientation`, etc.

## Why This Error Occurs

The validator parses the `sizes` value according to the [HTML living standard](https://html.spec.whatwg.org/multipage/images.html#sizes-attributes) and CSS Media Queries specification. A parse error is triggered when the value doesn't conform to the expected grammar. Common causes include:

- **Missing parentheses** around the media condition: `min-width: 600px 50vw` instead of `(min-width: 600px) 50vw`.
- **Missing length** after a media condition: `(min-width: 800px)` with no length following it.
- **Invalid or missing units** on the length: `50` instead of `50vw`, `50px`, or `50rem`.
- **Trailing commas** or extra separators: `(min-width: 600px) 50vw, , 100vw`.
- **Invalid media feature syntax**: typos like `(minwidth: 600px)` or using unsupported tokens.
- **Using `sizes` with pixel-density descriptors**: when `srcset` uses `1x`, `2x` instead of width descriptors, the `sizes` attribute is meaningless and can confuse validators.

## Why It Matters

- **Browser image selection**: Browsers rely on a correctly parsed `sizes` value to pick the optimal image from `srcset`. A malformed value causes the browser to fall back to a default size (typically `100vw`), which can result in downloading unnecessarily large images on small screens or blurry images on large screens.
- **Standards compliance**: Invalid `sizes` values violate the HTML specification and may behave unpredictably across different browsers.
- **Performance**: Correct `sizes` values are essential for responsive image optimization. Without them, you lose the bandwidth savings that `srcset` with width descriptors is designed to provide.

## How to Fix It

1. **Wrap every media condition in parentheses**: `(min-width: 600px)`, not `min-width: 600px`.
2. **Always include a valid CSS length after each condition**: `(min-width: 600px) 50vw`, not just `(min-width: 600px)`.
3. **End with a fallback length** that has no condition: `100vw`.
4. **Use valid CSS length units**: `vw`, `px`, `em`, `rem`, or `calc()` expressions.
5. **Remove trailing or duplicate commas**.
6. **Omit `sizes` entirely** if your `srcset` uses pixel-density descriptors (`1x`, `2x`).

## Examples

### Incorrect: missing parentheses around the media condition

```html
<img
  src="photo-800.jpg"
  srcset="photo-400.jpg 400w, photo-800.jpg 800w"
  sizes="min-width: 600px 50vw, 100vw"
  alt="A landscape photo">
```

### Correct: parentheses added

```html
<img
  src="photo-800.jpg"
  srcset="photo-400.jpg 400w, photo-800.jpg 800w"
  sizes="(min-width: 600px) 50vw, 100vw"
  alt="A landscape photo">
```

### Incorrect: missing length after the media condition

```html
<img
  src="banner-800.jpg"
  srcset="banner-400.jpg 400w, banner-800.jpg 800w"
  sizes="(min-width: 700px), 100vw"
  alt="A promotional banner">
```

The first entry `(min-width: 700px)` has no length value — the comma makes it look like a separate entry, but it's incomplete.

### Correct: length added after the condition

```html
<img
  src="banner-800.jpg"
  srcset="banner-400.jpg 400w, banner-800.jpg 800w"
  sizes="(min-width: 700px) 60vw, 100vw"
  alt="A promotional banner">
```

### Incorrect: using `sizes` with pixel-density descriptors

```html
<img
  src="avatar.jpg"
  srcset="avatar@1x.jpg 1x, avatar@2x.jpg 2x"
  sizes="(min-width: 600px) 80px, 40px"
  alt="User avatar">
```

The `sizes` attribute is only meaningful with width descriptors (`w`). When `srcset` uses density descriptors (`1x`, `2x`), omit `sizes`.

### Correct: `sizes` removed for density descriptors

```html
<img
  src="avatar.jpg"
  srcset="avatar@1x.jpg 1x, avatar@2x.jpg 2x"
  alt="User avatar">
```

### Incorrect: trailing comma

```html
<img
  src="hero-800.jpg"
  srcset="hero-400.jpg 400w, hero-800.jpg 800w"
  sizes="(min-width: 800px) 800px, 100vw,"
  alt="Hero image">
```

### Correct: trailing comma removed

```html
<img
  src="hero-800.jpg"
  srcset="hero-400.jpg 400w, hero-800.jpg 800w"
  sizes="(min-width: 800px) 800px, 100vw"
  alt="Hero image">
```

### Using `sizes` on a `<source>` inside `<picture>`

```html
<picture>
  <source
    type="image/webp"
    srcset="hero-480.webp 480w, hero-800.webp 800w, hero-1200.webp 1200w"
    sizes="(min-width: 1200px) 1200px, (min-width: 800px) 800px, 100vw">
  <img
    src="hero-800.jpg"
    srcset="hero-480.jpg 480w, hero-800.jpg 800w, hero-1200.jpg 1200w"
    sizes="(min-width: 1200px) 1200px, (min-width: 800px) 800px, 100vw"
    alt="Hero image">
</picture>
```

### Using compound media conditions

You can combine conditions with `and`:

```html
<img
  src="photo-800.jpg"
  srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1200.jpg 1200w"
  sizes="(min-width: 900px) and (orientation: landscape) 50vw, 100vw"
  alt="A sample photo">
```

### Full valid document

```html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Responsive Image Example</title>
  </head>
  <body>
    <img
      src="pic-800.jpg"
      srcset="pic-400.jpg 400w, pic-800.jpg 800w"
      sizes="(min-width: 600px) 50vw, 100vw"
      alt="A picture demonstrating valid sizes usage">
  </body>
</html>
```
