How Browsers Choose from srcset
You'll learn: How browsers intelligently pick the best image from your srcset list based on screen conditions and your hints.
When you provide a srcset with multiple image options, you might wonder: which one does the browser actually download? The browser uses a sophisticated selection algorithm that runs before the page renders, considering three key factors:
The Selection Algorithm
1. Viewport Width
The browser knows the current width of the viewport in CSS pixels. This is its starting point.
2. Device Pixel Ratio (DPR)
The browser multiplies the viewport width by the device's DPR to determine how many physical pixels it needs to fill. A 400px-wide viewport on a 2x display needs an 800px-wide image for crisp rendering.
3. The sizes Hint
Your sizes attribute tells the browser how much space the image will occupy. If you wrote sizes="50vw", the browser knows the image only needs to fill half the viewport, so it can choose a smaller file.
How It Works Together
The browser calculates: "If this image takes up X% of the viewport, and my DPR is Y, what's the minimum image width I need?" Then it scans your srcset and picks the smallest image that meets or exceeds that calculated width.
Think of it like ordering pizza: you tell the restaurant how many people you're feeding (sizes), they know what size plates you have (viewport + DPR), and they choose the smallest pizza that won't leave anyone hungry.
This all happens during HTML parsing, before CSS loads—that's why sizes uses viewport units, not em or rem.
Key Takeaway: Browsers choose from srcset by calculating needed image width (viewport × DPR × sizes percentage), then selecting the smallest suitable option to balance quality and performance.