Prompt
How do I fix broken responsive image srcset output?
Latest observation
To fix broken responsive image srcset output, the key is to ensure the generated HTML is valid and the image URLs, widths, and sizes attribute all match what the browser expects.
Common problems and fixes
1) Wrong srcset syntax
Each candidate in srcset must be:
image-320.jpg 320w, image-640.jpg 640w, image-1280.jpg 1280w
or for pixel-density descriptors:
image.jpg 1x, image@2x.jpg 2x
Common mistakes:
- Missing commas
- Missing width descriptor (
320w) - Using
pxinstead ofw - Extra spaces or malformed URLs
2) Missing or incorrect sizes
If you use w descriptors, you usually need sizes. Without it, browsers may choose the wrong file.
Example:
<img
src="image-640.jpg"
srcset="image-320.jpg 320w, image-640.jpg 640w, image-1280.jpg 1280w"
sizes="(max-width: 600px) 100vw, 600px"
alt="Example"
/>
What this means:
- On screens up to 600px wide, image takes full viewport width.
- Otherwise it renders at 600px.
If sizes doesn’t match your CSS layout, responsive selection breaks.
3) src doesn’t match a valid fallback
Always include a valid src as a fallback:
<img src="image-640.jpg" ...>
If src is broken, some browsers or edge cases may show no image.
4) Generated URLs are incorrect
If your CMS/build tool generates bad URLs, check:
- base path
- CDN prefixes
- query strings
- escaping/encoding
- file existence
Example of a broken output:
srcset="https://cdn.example.com/image.jpg?width=320w"
That is wrong because 320w must be a descriptor, not a query parameter.
Correct:
srcset="https://cdn.example.com/image-320.jpg 320w"
5) Mismatched image dimensions
If the image files don’t actually match the widths in srcset, the browser can still load them, but the output may look blurry or oversized.
Make sure:
image-320.jpgis actually ~320px wideimage-640.jpgis ~640px wide- etc.
6) CSS is overriding the responsive behavior
The browser uses the rendered size of the image to determine which candidate to pick.
Make sure your CSS is sensible:
img {
max-width: 100%;
height: auto;
}
Avoid fixed widths that contradict your sizes attribute.
7) Art direction needs <picture>, not just srcset
If you need different crops for different layouts, use <picture>:
<picture>
<source media="(max-width: 600px)" srcset="image-mobile.jpg">
<source media="(min-width: 601px)" srcset="image-desktop.jpg">
<img src="image-desktop.jpg" alt="Example">
</picture>
Use srcset on <img> for size-based selection, and <picture> for crop/format changes.
A known-good example
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1200.jpg 1200w"
sizes="(max-width: 768px) 100vw, 768px"
alt="Landscape photo"
style="max-width: 100%; height: auto;"
/>
Quick debugging checklist
- Is
srcsetvalid and comma-separated? - Are descriptors
320w,640w, etc. correct? - Is
sizespresent when usingwdescriptors? - Do the files actually exist?
- Do CSS widths match the intended display size?
- Is
srca usable fallback? - Do you need
<picture>instead?
If you want, paste your current img tag or generated HTML and I can pinpoint exactly what’s broken.