Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Load Images Without Layout Jumps

In this lesson, you watched Rosa’s home page jump as the photo of plot 7 loaded, and kept the photo’s room with width and height. You also let the browser wait to fetch photos far down a page with lazy loading.

Key Points

  • The browser lays out a page as soon as it has the HTML. An image is a separate file that arrives later, so an <img> without a size starts as a box 0 pixels wide and 0 high.
  • The image’s size is written near the start of its file. As soon as those first bytes arrive, the browser learns the size and moves everything below the image, while the rest of the file is still downloading. On the home page, the Next Work Day heading jumped 586 pixels down, below the window.
  • To see this on your own machine, open DevTools’ Network panel, which lists every file the page asks for. Tick its Disable cache box, or the browser reuses the image it already has and nothing jumps. Then pick 3G from its throttling menu and reload.
  • Give every <img> a width and a height, set to the image’s real size in pixels as plain numbers with no unit. The browser then keeps an empty box of that size from the start, and nothing moves when the image arrives. To find the size, open the image file in Chrome. The tab’s title shows it, for example plot-7.jpg (1200×896) for the course’s photo of Mei’s plot. The video’s copy of the photo was 800 by 600, so use the numbers of the file you have.
  • With no CSS yet, width and height also set the size the image shows at. Later, CSS will scale images to fit the screen, and the two numbers then give only the image’s shape, so the browser still keeps the right room for it.
  • loading="lazy" lets the browser wait to fetch an image until the visitor scrolls near it. On a page of twelve photos, Chrome asked for all twelve at once without it. With it, Chrome asked for the six within about 3000 pixels below the window at first, then for each of the rest as scrolling brought it that close. Chrome picks that distance, and other browsers pick their own.
  • Leave lazy loading off images near the top of a page, like Mei’s photo on the home page. Visitors need those straight away. The browser asks for an ordinary image as soon as it reads its tag, but a lazy one waits until the page is laid out, so it can start later.

Final Code

The photo on Rosa’s home page, in index.html, now has its size:

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

00:00 In the last lesson, you put Mei’s photo of plot 7 on Rosa’s home page. In this video, you’ll watch what the page does while that photo loads, and stop it from jumping.

00:11 Your live preview sends the photo almost instantly, so slow things down. DevTools has a Network panel, which lists every file the page asks for. Tick its Disable cache box, so the photo isn’t reused from the last load.

00:25 Then, in its throttling menu, pick 3G, a slow connection like a weak phone signal. Reload. The text arrives first, and the whole page fits in the window. The Next Work Day heading sits partway down.

00:40 Two seconds later, the photo starts to arrive. Everything below it jumps almost 600 pixels down, and the work day heading drops below the window. A visitor who was reading it loses their place.

00:54 Why does it jump? The browser lays out the page as soon as it has the HTML. The photo is a separate file, still on its way. Nothing in the image element says how big the photo is.

01:07 So the browser keeps no room for it, a box zero pixels wide and zero high.

01:13 The photo’s size is written near the start of its file. As soon as those first bytes arrive, the browser learns it, 800 by 600, and moves everything below to make room.

01:23 The rest of the photo fills in afterwards.

01:28 So tell the browser the size up front. Add a width attribute and a height attribute to the image. Each takes the photo’s real size in pixels, as a plain number with no unit.

01:39 To find a photo’s size, open its file in Chrome. The tab’s title shows it, 800 by 600 for Mei’s photo. Use your own photo’s numbers.

01:50 Save. Your turn: pause the video and predict. When you reload on the slow connection, where’s the Next Work Day heading before any of the photo arrives?

02:05 Reload. The heading’s already where it ends up, below the window. The browser keeps an empty 800 by 600 box from the start. When the photo arrives, it fills the box, and nothing moves.

02:19 You might worry that these numbers pin the photo at 800 pixels, even on a narrow phone. With no CSS yet, they do set the size it shows at. Later, CSS will scale images to fit the screen, and the numbers then give only the photo’s shape, so the browser still keeps the right room.

02:37 That’s all you need about sizing for now.

02:41 Some images sit far down a page. Say Rosa adds a photos page, with twelve plot photos, one under another. Each has its width and height.

02:51 Load it, and the Network panel shows the browser asking for all twelve right away, even ones a visitor might never scroll to.

03:00 Now add a loading attribute, set to lazy, to each image. It lets the browser wait to fetch an image until the visitor scrolls near it. Reload, and the list shows the six photos within about 3000 pixels below the window.

03:15 Chrome picks that distance, and other browsers pick their own. Scroll down, and the rest join the list one at a time. Leave lazy loading off images near the top, like Mei’s photo on the home page.

03:27 Visitors need those straight away. The browser asks for an ordinary image as soon as it reads its tag. A lazy one waits until the page is laid out.

03:38 An image’s file arrives after the page. Give every image its width and height, so the browser keeps its space and the page doesn’t jump. Add lazy loading to images far down a page, so the browser fetches them as they’re needed.

03:53 Rosa’s home page now has a heading, a photo, text and links, but nothing marks out its parts yet. Next, in Divide a Page Into Landmarks, you’ll mark out the page’s regions.

Become a Member to join the conversation.