Course contentsShow
← Web Development
Lesson 1214 of 2,98222. Responsive Images and MediaFree lesson

How Browsers Choose from srcset

Understanding the browser's selection algorithm based on viewport width and device pixel ratio.

How Browsers Choose from srcset

What you'll learn: How browsers automatically pick the best image from your srcset based on the user's screen and connection.

The Browser's Decision Process

When you provide a srcset with width descriptors (like image-800w.jpg 800w), you're not commanding the browser which image to use—you're giving it options. The browser then acts like a smart assistant, considering multiple factors to choose the best fit:

What the Browser Considers

1. Viewport Width
The browser knows how wide the user's screen is. If someone's on a 375px phone, loading a 2000px image would be wasteful.

2. Device Pixel Ratio (DPR)
High-density "Retina" displays have a DPR of 2 or 3, meaning they need more pixels to look sharp. A 400px-wide image slot on a 2x display actually needs an 800px image file.

3. The Layout Width
The browser calculates how much screen space your image will actually occupy (from CSS or the sizes attribute, which you'll learn soon).

The Selection Formula

Think of it like ordering pizza: the browser calculates how many pixels it needs (viewport width × DPR × layout percentage), then picks the smallest image that's still large enough from your srcset options. It might even choose a slightly larger one if bandwidth is good and it wants extra quality.

The User-Agent Mystery

Browsers also factor in network conditions, user preferences (like "data saver" mode), and cached images. You can't control or predict the exact choice—and that's intentional! You provide the menu; the browser orders wisely.

Key Takeaway: The browser selects from srcset using a smart algorithm that weighs viewport width, device pixel ratio, and current conditions—you provide options, the browser optimizes automatically.