← All posts

CSS Sticky Footer: Keep the Footer at the Bottom When Content Is Short (Grid, Flexbox and the Traps)

Contact page, 404 page or empty cart: the footer floats halfway up the screen. The two modern CSS fixes, and the traps that stop them working (wrappers, 100vh on mobile, the WordPress admin bar).

CSS Sticky Footer: Keep the Footer at the Bottom When Content Is Short (Grid, Flexbox and the Traps)

Key points

  • The footer floats up because the page is only as tall as its content: the container needs to be at least as tall as the viewport.
  • Three lines of CSS Grid on the body handle most cases; Flexbox does the same with flex-grow and copes better with extra elements.
  • The real traps: wrapper elements, 100vh on mobile, a leftover height: 100% and the WordPress admin bar.

A short contact page, a 404, an empty cart: the content doesn’t fill the screen, and the footer ends up in the middle with an empty band below it. It’s one of the layout bugs clients report most often after launch, because it never shows on the content-rich pages everyone tests during development. What you want is usually called a “sticky footer”, which isn’t the same as a footer fixed to the screen: it should sit at the bottom of the window when the page is short, and simply follow the content when the page is long. The modern fix is a few lines of CSS, but it fails in specific situations worth knowing about.

Why the footer floats up

By default, a block element is as tall as its content, and body is no exception: it doesn’t fill the window. If the content takes half the screen, the footer appears right after it, and the bottom half is just the page background. The background colour adds to the illusion, since it paints the whole window even when body is shorter.

To see it, temporarily add outline: 2px solid red to body, or hover it in your browser’s inspector: it stops right under the footer.

The old fixes positioned the footer absolute at the bottom of body, with a padding-bottom equal to its height. They break as soon as the footer’s height changes: a menu wrapping onto two lines on mobile, a longer translation, an extra partner logo. A position: fixed footer is something else entirely: it stays on screen while scrolling and covers the bottom of the content, which is almost never what you want for a full site footer.

The CSS Grid solution

Given a typical structure with three direct children of body:

<body>
  <header class="site-header">…</header>
  <main class="site-main">…</main>
  <footer class="site-footer">…</footer>
</body>
*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;  /* fallback for older browsers */
  min-height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
}

The grid defines three rows: header and footer take their natural height (auto), and the middle row takes all the remaining space (1fr). The key is min-height rather than height: the page is at least as tall as the screen and grows normally when content is long. With height, long pages would overflow underneath the footer.

The weak spot: it assumes exactly three children. A “Skip to content” link, a cookie banner or a modal injected straight into body becomes an extra row, the header gets the free space, and the layout shifts. Script tags are fine, since they aren’t rendered. For everything else, take it out of the flow:

/* Skip link and cookie banner taken out of the flow:
   they no longer create an extra grid row */
.skip-link,
.cookie-banner {
  position: absolute;
}

In our experience this is the most common “it worked in development” case: the cookie banner only exists in production, and it breaks the grid.

The Flexbox solution, more forgiving

Flexbox gives the same result without depending on the number of children, which makes it our default on sites where plugins inject markup into the page:

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.site-main {
  flex-grow: 1;
}

/* Alternative: content keeps its height, the footer is pushed down */
/* .site-footer { margin-top: auto; } */

flex-grow: 1 on the main content gives it all the free space, while any extra elements keep their natural height. The margin-top: auto alternative on the footer produces the same placement, but the gap sits between content and footer instead of inside main. Go with flex-grow if the main area has a background or border that should reach the footer.

One difference to be aware of: inside a flex container, children’s vertical margins no longer collapse. If your header has a bottom margin and your content a top margin, they now add up. Check your spacing after the change; it’s a classic source of small visual regressions.

The traps that stop it working

When the rule is in place and the footer still won’t move down, the cause is almost always one of these:

  • A wrapper element. Grid and Flexbox only affect direct children. If the theme wraps everything in a div#page, that wrapper needs the rule, and the minimum height.
  • A leftover height: 100%. Plenty of older themes declare html, body { height: 100%; }. The body is then capped at the screen height, and on long pages content overflows under the footer. Replace it with min-height.
  • 100vh on mobile. In mobile browsers, 100vh matches the viewport with the address bar hidden, so the page is slightly too tall and a short page gets a scrollbar. The dvh unit tracks the height actually visible; keep 100vh just before it as a fallback.
  • Margins and box-sizing. The default body margin, or padding without box-sizing: border-box, adds to the minimum height and causes a few pixels of scroll.
  • The WordPress admin bar. When you’re logged in, WordPress adds a top margin to the html element to make room for its toolbar, so the page overflows by that amount. Recent WordPress versions expose the bar’s height in the --wp-admin--admin-bar--height custom property, which you can simply subtract.

Here’s the version for a classic WordPress theme, with its wrapper and the admin bar:

/* Classic theme: header, content and footer sit inside a #page wrapper */
#page {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
#page > .site-content {
  flex-grow: 1;
}

/* WordPress admin bar: it adds a top margin to <html> */
body.admin-bar #page {
  min-height: calc(100dvh - var(--wp-admin--admin-bar--height, 0px));
}

The 0px fallback inside var() keeps the calculation valid if the property isn’t defined. Check in the inspector, on the html element, that your WordPress version sets it.

In a WordPress block theme

In a block theme (full site editing), the generated structure is always the same: body contains a .wp-site-blocks wrapper holding the header template part, the content and the footer template part:

.wp-site-blocks {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
.admin-bar .wp-site-blocks {
  min-height: calc(100dvh - var(--wp-admin--admin-bar--height, 0px));
}
.wp-site-blocks > main {
  flex-grow: 1;
}

This can go in the styles.css field of theme.json, in a child theme’s stylesheet, or in the Site Editor’s Additional CSS. A trap specific to block themes: not every template uses an actual main element. In the Site Editor, select the group wrapping the content and check under “Advanced” that its HTML element is <main>. It’s better for accessibility too.

Then test the pages nobody remembers: the 404 template, a search with no results, an empty cart, a page with just a title, a phone in landscape mode, and a logged-in session with the admin bar showing. Those rarely-viewed pages are exactly where this bug shows up.

FAQ

What’s the difference between a sticky footer and a fixed footer?

A sticky footer sits at the bottom of the window only when the page is short, and follows the content otherwise. A fixed footer (position: fixed) stays visible the whole time you scroll. The latter suits a small action bar, not a full site footer.

Should I use Grid or Flexbox?

Both work. Grid is more concise when the structure is strict (exactly header, content, footer). Flexbox is more forgiving when plugins or scripts add elements to body, which happens a lot on WordPress.

Why does a short page still get a scrollbar?

Usually one of these: 100vh on mobile instead of 100dvh, the default body margin, padding without box-sizing: border-box, or the WordPress admin bar when you’re logged in.

Layout glitches that only appear on certain pages or screens? We fix the CSS in your WordPress theme and test your templates against every real-world case, including the ones everyone forgets.

Share LinkedIn Facebook X WhatsApp
</>

webtestudio

Web agency: custom WordPress sites, premium themes and SaaS software.

Discuss your project →