×

html · level 3 · unit 1 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

w and sizes let the browser fit the file to the space.

A picture might fill a whole phone screen but only half a laptop screen. Instead of 1x and 2x, give each file in srcset its real width with a w: 400w means the file is 400 pixels wide.

Then sizes says how wide the picture will appear. Each entry starts with a , a question about the screen like (min-width: 800px). The unit is one hundredth of the viewport's width, so 50vw is half.

The browser checks the entries in order and uses the first one that is true. It works out the width it needs and downloads one file. The last entry has no condition: it is the answer when nothing else matches.

<h1>Adopt a pet</h1>
<img src="cat.svg"
     srcset="cat.svg 400w, dog.svg 800w"
     sizes="(min-width: 800px) 50vw, 100vw"
     alt="A pet looking for a new home" width="800" height="600">

Press show my page to see it here.

The browser counts pixel density too, so a sharp phone may still choose the bigger file.