Menu

The sizes attribute: how the browser picks a srcset candidate
📰

The sizes attribute: how the browser picks a srcset candidate

DEV Community: html·Sahadat Husain·13 days ago
#cYCmtW5P
#devto2#devto#dev#sizes#bytes
Reading 0:00
15s threshold

The browser picks a srcset candidate by dividing each width descriptor by the slot width from sizes , then taking a candidate whose density covers the screen's device pixel ratio. Leave sizes out and the slot is the full viewport width. On a 1280 px laptop with a 720 px column, that one omission took the download from 58.4 KB to 170.3 KB in my test.

What does the browser compute?

Three numbers per image:

The slot width. The browser walks the sizes list and takes the first media condition that matches. No match falls to the last entry. No sizes at all means 100vw , which MDN's img reference states as the default.

Each candidate's density. A 1600w candidate in a 720 px slot has a density of 1600 / 720 = 2.22.

The density it needs. That is the device pixel ratio: 1 on most laptops, 2 or 3 on phones.

The browser then picks a candidate with enough density. The spec leaves the final choice to the browser.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.