Width Descriptors (w) in srcset
What you'll learn: How to tell the browser exactly how wide each image file is so it can pick the perfect one for any screen.
The Problem with x Descriptors
In the last lesson, you learned about srcset with pixel density descriptors (1x, 2x). But those assume all screens at a given density need the same image size. What if a mobile user with a high-density screen has a small viewport? They'd download a massive file they don't need.
What Are Width Descriptors?
Width descriptors (w) let you declare the actual pixel width of each image file. Instead of saying "this is for 2x screens," you say "this image is 800 pixels wide."
<img
src="photo.jpg"
srcset="photo-400.jpg 400w,
photo-800.jpg 800w,
photo-1200.jpg 1200w"
alt="Landscape">
Here, 400w means "this file is 400 pixels wide," 800w means 800 pixels, and so on.
How the Browser Decides
The browser combines three pieces of information:
- Viewport width (how wide is the screen?)
- Device pixel ratio (standard or high-density display?)
- Image widths (from your
wdescriptors)
It picks the smallest file that will still look sharp—saving bandwidth on small screens while delivering quality on large displays.
Think of It Like Shopping
It's like a clothing store where you tell the salesperson "I have sizes Small (400), Medium (800), and Large (1200)." The customer (browser) looks at their measurements (viewport + density) and picks the best fit. You're giving options, not forcing a choice.
Key Takeaway: Width descriptors (w) tell browsers the actual pixel width of each image file, letting them intelligently choose based on both viewport size and screen density—more efficient than density descriptors alone.