About This HTML Issue
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:
-
Each entry (except the last) is a media condition in parentheses followed by a CSS length — for example,
(min-width: 800px) 50vw. -
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 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 50vwinstead 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:
50instead of50vw,50px, or50rem. -
Trailing commas or extra separators:
(min-width: 600px) 50vw, , 100vw. -
Invalid media feature syntax: typos like
(minwidth: 600px)or using unsupported tokens. -
Using
sizeswith pixel-density descriptors: whensrcsetuses1x,2xinstead of width descriptors, thesizesattribute is meaningless and can confuse validators.
Why It Matters
-
Browser image selection: Browsers rely on a correctly parsed
sizesvalue to pick the optimal image fromsrcset. A malformed value causes the browser to fall back to a default size (typically100vw), which can result in downloading unnecessarily large images on small screens or blurry images on large screens. -
Standards compliance: Invalid
sizesvalues violate the HTML specification and may behave unpredictably across different browsers. -
Performance: Correct
sizesvalues are essential for responsive image optimization. Without them, you lose the bandwidth savings thatsrcsetwith width descriptors is designed to provide.
How to Fix It
-
Wrap every media condition in parentheses:
(min-width: 600px), notmin-width: 600px. -
Always include a valid CSS length after each condition:
(min-width: 600px) 50vw, not just(min-width: 600px). -
End with a fallback length that has no condition:
100vw. -
Use valid CSS length units:
vw,px,em,rem, orcalc()expressions. - Remove trailing or duplicate commas.
-
Omit
sizesentirely if yoursrcsetuses pixel-density descriptors (1x,2x).
Examples
Incorrect: missing parentheses around the media condition
<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
<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
<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
<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
<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
<img
src="avatar.jpg"
srcset="avatar@1x.jpg 1x, avatar@2x.jpg 2x"
alt="User avatar">
Incorrect: trailing comma
<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
<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>
<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:
<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
<!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>
Find issues like this automatically
Rocket Validator scans thousands of pages in seconds, detecting HTML issues across your entire site.