More CSS answers

by Danny
Published 2 min read

Keep a footer at the bottom of short pages without fixing it in place. Use a grid or flex layout with min-height: 100svh so the main content grows to fill the gap.

The short answer

#

Make the page wrapper at least as tall as the viewport, then let the main content take up any spare space. With CSS Grid, three rows of auto 1fr auto push the footer to the bottom on short pages and let it flow normally on long ones.


_10
body {
_10
min-height: 100svh;
_10
margin: 0;
_10
display: grid;
_10
grid-template-rows: auto 1fr auto;
_10
}


_10
<body>
_10
<header>...</header>
_10
<main>...</main>
_10
<footer>...</footer>
_10
</body>

Why it happens

#

By default, a block element is only as tall as its content. If a page has a header, a short paragraph and a footer, the footer sits straight after the paragraph, halfway up the screen, with empty space below it.

What you usually want is a "sticky footer": on short pages it sits at the bottom of the viewport, and on long pages it sits after the content like normal. That is different from position: fixed or position: sticky, which keep the footer on screen while the page scrolls.

To get the sticky footer effect, two things must be true:

  1. The wrapper around header, main and footer is at least the height of the viewport.
  2. Something between the header and footer grows to fill the leftover space.

How to fix it

#

Use grid on the body

The grid version shown above is the shortest. 1fr gives the middle row all the space left after the header and footer take their natural height. If the content is longer than the viewport, the grid just grows and the footer follows the content.

If your framework wraps the page in an extra element (for example a root div in a React app), put the grid on that element instead, and make sure there are exactly three direct children.


_10
#root {
_10
min-height: 100svh;
_10
display: grid;
_10
grid-template-rows: auto 1fr auto;
_10
}

Use flexbox

Flexbox works just as well. Make the wrapper a column and let main grow.


_10
body {
_10
min-height: 100svh;
_10
margin: 0;
_10
display: flex;
_10
flex-direction: column;
_10
}
_10
_10
main {
_10
flex: 1;
_10
}

An alternative is margin-top: auto on the footer, which pushes it down without changing main. That is useful when you cannot add styles to the content area.


_10
footer {
_10
margin-top: auto;
_10
}

Why 100svh and not 100vh

On mobile browsers, 100vh is the height of the viewport with the address bar hidden. When the bar is showing, a 100vh wrapper is taller than the screen, so the footer starts just out of view and the page scrolls for no reason. 100svh (small viewport height) uses the height with the browser UI showing, so short pages fit without scrolling. The svh, lvh and dvh units have been supported in all major browsers since 2022.

If you need to support very old browsers, declare 100vh first as a fallback:


_10
body {
_10
min-height: 100vh;
_10
min-height: 100svh;
_10
}

Common pitfalls

#
  • Using height instead of min-height. A fixed height cuts off long pages or makes content overflow the footer.
  • Forgetting the default margin on body. It adds a few pixels and causes a small scroll bar.
  • Having more than three children in a three-row grid. An extra element such as a skip link or a portal root creates a new row and breaks the layout. Name the rows with grid-template-areas, or move the extra element out of the flow.
  • Reaching for position: fixed or position: absolute; bottom: 0. The footer then covers content on long pages unless you add padding to match its height.
  • Using 100vh on mobile and wondering why short pages scroll.
#

Was this resource helpful?