Offer Images in Several Widths

The garden’s layout now fits every screen, but the hero photo is still one file. Every visitor downloads the same plot-7.jpg, 1200 pixels wide and about 286 kB, whether the photo fills a 272-pixel column on a small phone or a 778-pixel column on a laptop. On the phone, most of those pixels are wasted. On a laptop with a sharp screen, there aren’t enough of them, so the photo looks soft.

In this lesson, you’ll make the photo in four widths, describe them to the browser, and predict which one it picks for a given screen. You’ll also give the volunteer cards’ small photos a sharper version for sharp screens. Start with how many pixels a photo actually needs.

Make the Photo in Four Widths

How wide a photo needs to be depends on the width of its slot in CSS pixels and on the screen’s pixel density. You met CSS pixels when you tested the site at 320 pixels. A phone packs two or three of the screen’s dots into each CSS pixel. That ratio is the screen’s pixel density, or device pixel ratio.

Type window.devicePixelRatio into the Console to see your own screen’s. It’s often 1 on a laptop’s screen, 2 on most Macs, and 2 or 3 on phones.

For a sharp photo, the file needs one image pixel for every dot. So its width is the slot’s width times the density. Rosa’s phone is 390 pixels wide with a density of 3, and the hero’s slot there is 342 pixels wide, so a sharp hero requires 342 × 3 = 1026 image pixels. A 1x laptop with a 778-pixel slot needs 778.

No single file is right for both, so you’ll make four: 480, 800, 1200, and 1600 pixels wide. You make them from the full-size original, which is 2752 pixels wide, because shrinking a photo keeps it sharp and enlarging one doesn’t. Here are the steps, using Squoosh, a free image tool that runs in the browser, so there’s nothing to install:

  1. Download html-css-garden-photos-full from the course’s supporting materials and unzip it outside garden-practice/. The originals are too large to serve, so they don’t belong in the site.
  2. Go to squoosh.app in Chrome or Edge, and drag plot-7.jpg from the full-size folder onto the page.
  3. In the Edit panel on the right, turn on Resize, and type 480 into Width. Leave Maintain aspect ratio checked, so the height follows, and leave Compress on MozJPEG.
  4. Click the round download button at the bottom right. Squoosh saves the file as plot-7.jpg, so rename it plot-7-480.jpg and move it into garden-practice/images/.
  5. Change Width to 800, download again, and rename the file plot-7-800.jpg. Do the same for 1200 and 1600.

The four files come to about 41 kB, 102 kB, 205 kB, and 334 kB. A phone that only needs the 480-pixel file saves about 245 kB compared with today’s plot-7.jpg.

Describe the Files With srcset and sizes

Now the browser needs to know the files exist and how wide each one is. The <img> element’s srcset attribute lists them. Each entry is a file’s path, a space, and a width descriptor, the file’s real width in image pixels followed by w. So images/plot-7-480.jpg 480w says that this file is 480 pixels wide.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.