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

Keep the Footer at the Bottom

In this lesson, you turned the seed swap page into a page shell: <body> as a grid of three rows that keeps the footer at the bottom of the window when the page is short, and lets it move down when the page is long.

Key Points

  • With the banner’s min-height unticked in the Styles pane, the seed swap page is shorter than the window, and the footer sits right under the seed list with an empty band of page below it. A footer floating halfway up the window makes a page look unfinished, and any page with little on it, or a tall screen, shows it.
  • <body> has three children, the header, <main> and the footer. With display: grid on <body>, each child gets a row of its own. One column sounds like flexbox, but here the sizes come from the layout in, from the window’s height, and that’s grid’s side.
  • grid-template-rows: auto 1fr auto lists the rows from top to bottom. 1fr takes whatever space is left over, and that goes to <main>. An auto row can stretch into spare space too, as the banner’s row did, but the 1fr row takes all of it first, so the header and footer rows stay as tall as their content.
  • min-height: 100vh makes the body at least as tall as the window. Without it, the body is only as tall as its content, and there’s nothing left over for the row for <main>.
  • On the short page, the footer now sits at the bottom of the window. DevTools’ grid overlay on <body> numbers four row lines, three rows, with the row for <main> filling the space between.
  • In normal flow, a block with no width set takes all the width it can, up to its max-width of 800 pixels, and auto side margins center it. In a grid, it’s the grid that stretches an item across its cell, and auto side margins switch that off. The page’s column gives the header, <main> and the footer margin-inline: auto, so each one shrinks to fit its content, and the margins share out the space on either side: the header shrank to 292.3 pixels and the footer to 382.09.
  • <main> follows the same rule, but its seed tags, all on one line, would run wider than the column, so fitting its content still fills it. On the upcoming events page, <main> shrinks too, to 698.52 pixels.
  • width: 100% on the three boxes makes each fill its cell again. The column’s max-width still stops it at 800 pixels, and the auto margins center it. The reset’s box-sizing: border-box keeps the column’s side padding inside that width, so no box spills past the column.
  • On a page taller than the window, the footer goes below the seed list, past the bottom of the window: the row for <main> has no space left over to share, and the body’s height is only a minimum, so it grows with the content.

Final Code

The two rules at the end of events/layout.css:

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 The seed swap banner now fills the window, so this page runs well past the bottom of the screen. To see the page when it’s short, untick the banner’s min-height in the Styles pane.

00:10 The banner shrinks to its heading, and the whole page fits in the window.

00:15 Now look at the footer. It sits right under the seed list, with an empty band of page below it. A footer floating above the window’s bottom makes the page look unfinished, and any page with little on it, or a tall screen, shows it.

00:28 In this video, you’ll give the page a shell that works whatever its length.

00:35 The body has three children: the header, main and the footer. Make the body a grid, and each child gets a row of its own. One column sounds like flexbox, but here the sizes come from the layout in, and that’s grid’s job.

00:49 In layout.css, add a rule for the body, with its display set to grid.

00:55 Next, list the rows from top to bottom: auto, 1fr and auto, one for each child. One fr takes whatever space is left over, and that goes to main. An auto row can stretch into spare space too, as the banner’s row did, but the 1fr row takes all of it first.

01:13 So the header and footer rows stay as tall as their content. Last, add a min-height of 100vh. It makes the body at least as tall as the window. Without it, the body is only as tall as its content, and there’s nothing left over for main’s row.

01:30 Save. The reload makes the banner full height again, so untick the banner’s min-height once more. The footer drops to the bottom of the window. Turn on the body’s grid overlay: four row lines, three rows, and main’s row fills the space between.

01:47 Something else moved. The site name and the nav now sit in the middle of the window. Hover over the header in the Elements panel. Its box is only as wide as its content, and the footer has shrunk the same way.

02:00 In normal flow, a block with no width set takes all the width it can, up to its max-width of 800 pixels, and auto side margins center it. In a grid, it’s the grid that stretches an item across its cell, and auto side margins switch that off.

02:16 So each box shrinks to fit its content, and the margins share out the space on either side.

02:22 Main follows the same rule. But its seed tags, all on one line, would run wider than the column, so main still fills the column. On the upcoming events page, where nothing runs that wide, main shrinks too.

02:37 To fill the column again, add one more rule under the body rule. Give the header, main and footer a width of 100%, and save. Each box fills its cell, the max-width still stops it at 800 pixels, and the auto margins center it.

02:53 The reset’s border-box keeps the column’s side padding inside that width, so no box spills past the column.

03:01 That save brought the banner’s min-height back, so the page is taller than the window again. Your turn: pause the video and predict where the footer goes now.

03:12 Below the seed list, past the bottom of the window. Main’s row has no space left over to share, and the body’s height is only a minimum, so it grows with the content.

03:24 So that’s a page shell: the body as a grid of three rows, auto, 1fr and auto, at least as tall as the window. Where the three boxes have auto side margins, give them a width of 100% so they fill the column.

03:39 Next, in Spread the Header and Add a Sidebar, you’ll lay out the header’s own row and put a sidebar beside main.

Become a Member to join the conversation.