Basic srcset with Width Descriptors
You'll learn: How to give the browser multiple image files at different resolutions so it can pick the best one for each user's screen.
The Core Problem
Right now you've learned about the "Resolution Switching Problem" — serving the same large image to both a small phone and a giant desktop wastes bandwidth. The srcset attribute with width descriptors solves this by telling the browser: "Here are three versions of this image, and here's how wide each one is."
How Width Descriptors Work
Instead of forcing one image on everyone, you provide a menu of options:
<img
src="photo-800w.jpg"
srcset="photo-400w.jpg 400w,
photo-800w.jpg 800w,
photo-1200w.jpg 1200w"
alt="A scenic mountain view">
The 400w, 800w, and 1200w are width descriptors — they tell the browser each image's actual pixel width. The w stands for "width" (not "watts" or anything else).
The browser now knows:
- "I have a 400px-wide image file"
- "I have an 800px-wide image file"
- "I have a 1200px-wide image file"
Based on the user's screen size, pixel density, and network speed, the browser picks the most appropriate one. A phone might grab the 400w version, while a high-resolution desktop grabs the 1200w version.
Think of It Like...
Imagine a restaurant offering small, medium, and large pizzas. You tell the waiter the sizes available — they recommend one based on how hungry the customer is. You don't decide; you just provide options.
Key Takeaway: Use srcset with width descriptors (400w, 800w, etc.) to provide multiple image sources, letting the browser intelligently choose the best file for each device.