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

Stop the Header From Jumping

The video picks up with the full-height header at the top of Rosa’s home page, set to height: 100vh. On a model of her phone, 360 pixels wide, she sees 640 pixels of the page while the browser’s toolbar is shown, and 720 once she scrolls and the toolbar slides away. These are round numbers for a model: real phones differ.

In this video, you made the header fit her screen and keep its size while she scrolls, and you pinned a Book a lane bar to the bottom of the screen. Here are the main points and the final code.

Key Points

  • 100vh is 720 pixels in the model, whatever the toolbar does. So with the toolbar shown, the header’s last 80 pixels are hidden under the toolbar. The header centers its text in all 720 pixels, so on Rosa’s screen that text sits 40 pixels below the middle. Her links are still in view, but the header doesn’t fit. Watch out: 100vh is measured with the toolbar hidden, so it isn’t the height you see.
  • A common tip says to swap vh for dvh. With height: 100dvh, the header is 640 pixels tall while the toolbar is shown, and it fits. But as the toolbar hides, the header grows to 720 and pushes the Today panel below it 80 pixels down the page. When the toolbar returns, it all moves back up. (The model holds the scroll still, so these shifts are the toolbar’s alone.) That tip is a detour: dvh fits, but the page shifts while Rosa reads it.
  • A phone has three viewports:
  • The small viewport is the area you see with the toolbar shown, 640 pixels in the model. 1svh is 1 percent of it.
  • The large viewport is the area with the toolbar hidden, 720 pixels. Both lvh and vh measure it.
  • The dynamic viewport is the current one, anywhere from 640 to 720. dvh changes as the toolbar moves, though browsers may update it in steps.
  • A full-height header like this is often called a hero. For it, min-height: 100svh fits with the toolbar shown and then keeps its size: when the toolbar slides away, the header stays 640 pixels tall, the Today panel keeps its place in the page, and Rosa sees 80 more pixels instead.
  • Use min-height rather than height, so the header can still grow if its text needs more room.
  • Keep dvh for something that must fill the visible area, such as a full-screen closure notice. It has nothing below it to push, so its resizing doesn’t move anything Rosa reads.
  • The Book a lane bar is a fixed link, 3 rems tall. Placed at 100vh minus its height, its top is 672 pixels, under the toolbar. Set its inset-block-end to 0 instead: a fixed element is placed against the area you see, so its bottom follows the visible bottom. It sits right above the toolbar and slides down with the toolbar as it hides. Pad the end of the page by 3rem too, so the bar never covers the last card.
  • The rule: size a full-height header with min-height and svh, so it fits and keeps its size. Use dvh only for something that must fill the visible area, with nothing below it to push. Pin a bottom bar with inset-block-end, so it follows the toolbar.
  • DevTools’ device mode has no toolbar that hides, so svh, lvh and dvh all give the same length there. To see the difference, scroll the page on a real phone.

The Final Code

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 Rosa’s session card now fits wherever it’s placed. Her phone’s full-height header doesn’t. You’ll make it fit her screen and keep its size while she scrolls.

00:10 Here’s a model of her phone, 360 pixels wide. With the toolbar shown, she sees 640 pixels of the page. When she scrolls down, the toolbar slides away, and she sees 720.

00:23 Real phones differ, so these are round numbers.

00:27 The header’s height is 100vh, so it’s 720 pixels tall, whatever the toolbar does. With the toolbar shown, its last 80 pixels are hidden under the toolbar. It centers its text in all 720, so on her screen that text sits 40 pixels below the middle.

00:46 Her links are still in view, but the header doesn’t fit. Watch out: 100vh is measured with the toolbar hidden, so it isn’t the height you see.

00:57 A common tip says to swap vh for dvh, the dynamic viewport unit, which follows what’s visible. So you make 100dvh the header’s height. With the toolbar shown, the header is 640 pixels tall, and it fits.

01:14 Now the toolbar slides away. The model holds the scroll still. Your turn: pause the video and predict what happens to the Today panel below the header.

01:25 It’s pushed 80 pixels down the page. As the toolbar hides, the header grows to 720 and pushes everything below it down. When the toolbar returns, it all moves back up.

01:37 So the tip is a detour: dvh fits, but the page shifts while Rosa reads it.

01:45 That shift comes from the phone’s three viewports.

01:50 The small viewport is the area you see with the toolbar shown, 640 pixels here. One svh is 1 percent of it. The large viewport is the area with the toolbar hidden, 720 pixels.

02:03 Both lvh and vh measure it. The dynamic viewport is the current one, anywhere from 640 to 720. So dvh changes as the toolbar moves, though browsers may update it in steps.

02:19 A full-height header like this is often called a hero. You want it to fit with the toolbar shown and then keep its size. That’s the small viewport. So 100svh becomes the header’s minimum height.

02:33 With the toolbar shown, the header is 640 pixels, and it fits. The toolbar slides away, the header stays 640, and the Today panel keeps its place in the page.

02:43 She sees 80 more pixels instead.

02:47 Use min-height rather than height, so the header can still grow if its text needs more room. Keep dvh for something that must fill the visible area, such as a full-screen closure notice.

02:59 It has nothing below it to push, so its resizing doesn’t move anything Rosa reads.

03:06 Rosa also wants a Book a lane bar at the bottom, a fixed link 3 rems tall. Placed at 100vh minus its height, its top is 672 pixels, under the toolbar. So set its inset-block-end to 0.

03:23 A fixed element is placed against the area you see, so its bottom follows the visible bottom. It sits right above the toolbar and slides down with the toolbar as it hides.

03:34 Pad the end of the page by 3 rems too, so the bar never covers the last card.

03:40 Here’s the rule. Size a full-height header with min-height and svh, so it fits and keeps its size. Use dvh only for something that must fill the visible area, with nothing below it to push.

03:53 Pin a bottom bar with inset-block-end, so it follows the toolbar.

03:58 DevTools’ device mode has no toolbar that hides, so all three units give the same length there. To see the difference, scroll the page on a real phone. Rosa’s header now fits her phone and keeps its size while she scrolls.

04:13 Next, in Exercise: Choose Viewport Units, you’ll size a welcome hero, a full-screen closure notice and the Book a lane bar.

Become a Member to join the conversation.