CSS Grid simplifies web layout significantly. While there's a learning curve, its intuitive nature makes it enjoyable to use, especially for managing headers and footers. This tutorial demonstrates creating both fixed and sticky footers using CSS Grid. Rachel Andrew's "The New CSS Layout" is highly recommended for a deeper dive into Grid and Flexbox.
Project Overview
We'll build a classic HTML layout with a header, main content, and footer. We'll implement two footer variations: a fixed footer that remains at the viewport bottom regardless of content length, and a traditional sticky footer that adheres to the bottom but is pushed down by longer content. The main content area will be adaptable, spanning the full viewport width or centering within a defined width.
Fixed footers, while less common than sticky footers, are used by sites like Charles Schwab. Their implementation often involves hard-coded sizes and paddings. Our approach avoids these limitations, leveraging CSS Grid's flexibility.
Before proceeding, examine the Charles Schwab homepage's fixed footer. Inspecting its code in DevTools reveals hard-coded height and positioning.
Let's achieve a similar effect without these constraints.
Initial Styles
We'll start with a minimal UI, gradually enhancing it with CSS Grid. A CodeSandbox (and subsequent iterations) provides interactive examples.
First, we ensure the viewport's full height is utilized:
body { margin: 0; /* prevents scrollbars */ } #app { height: 100vh; }
Next, we define the header, main, and footer sections and their grid structure. This initial structure won't function as intended yet; it serves as a foundation:
body { margin: 0; } #app { height: 100vh; /* grid container settings */ display: grid; grid-template-columns: 1fr; grid-template-rows: auto 1fr auto; grid-template-areas: 'header' 'main' 'footer'; } #app > header { grid-area: header; } #app > main { grid-area: main; padding: 15px 5px 10px 5px; } #app > footer { grid-area: footer; }
This creates a single-column layout. 1fr
means "take the remaining space," resulting in full-width columns. The rows are sized using auto
(for header and footer) and 1fr
(for main content to fill remaining space). No hard-coded heights are necessary.
Content Integration
The provided CodeSandbox uses React for demonstration purposes, but the CSS Grid principles remain independent of the framework. Header, Main, and Footer components render the respective HTML elements. Billing and Settings sections provide sample content, demonstrating the layout's adaptability.
The initial layout shows a functional "Billing" section, but "Settings" pushes the footer off-screen. Scrolling affects the entire page, causing the header to disappear. Let's address this.
Fixed Header and Footer
The 100vh
height and auto
/1fr
row sizing initially cause issues when content exceeds the viewport. Adding overflow: auto
to the <main></main>
element enables scrolling within the main content area, keeping the header and footer fixed:
#app > main { grid-area: main; overflow: auto; padding: 15px 5px 10px 5px; }
This updated demo resolves the scrolling and visibility issues.
Adjustable Main Section Width
To center the main content within a 600px width or allow it to span the full viewport, we nest a grid inside <main></main>
. This uses three columns (1fr 600px 1fr
), effectively centering the 600px section.
#app > main { display: grid; grid-template-rows: 1fr; grid-template-columns: 1fr 600px 1fr; }
Content is positioned using grid coordinates. A .full
class allows content to span the entire width:
#app > section { grid-area: 1 / 2 / 1 / 3; } #app > section.full { grid-area: 1 / 1 / 1 / 4; }
Note the use of grid lines (four lines for three columns). Shrinking the viewport reveals a truncation issue with the fixed 600px width. minmax(0, 600px)
solves this, allowing the width to adjust up to 600px:
main { display: grid; grid-template-rows: 1fr; grid-template-columns: 1fr minmax(0, 600px) 1fr; }
The final demo showcases this responsive behavior.
Traditional Sticky Footer
To create a traditional sticky footer, we modify the grid structure. The <main></main>
element now contains the footer within its grid:
<div> <header></header> <main> <section></section> <footer></footer> </main> </div>
The #app
grid is updated to have two rows:
#app { /* same as before */ grid-template-columns: 1fr; grid-template-rows: auto 1fr; grid-template-areas: 'header' 'main'; }
The <main></main>
grid is adjusted to include the footer:
#app > main { display: grid; grid-template-rows: 1fr auto; grid-template-columns: 1fr minmax(0, 600px) 1fr; }
Footer positioning within this grid:
#app > footer { grid-area: 2 / 1 / 3 / 4; }
This setup allows the footer to be pushed down by content within <main></main>
, creating the desired sticky footer effect. Minor padding adjustments were made to ensure proper alignment. The final demo illustrates this behavior. Additional styling was applied to the section element to allow horizontal scrolling within the content area while maintaining the vertical layout.
Conclusion
While the techniques demonstrated are achievable without CSS Grid, Grid provides a unified and elegant solution. Its flexibility and ease of use, as demonstrated by the simple transition from a fixed to a sticky footer, make it a powerful tool for complex layouts. The examples presented are relatively simple, showcasing the potential of CSS Grid for more ambitious projects.
The above is the detailed content of How to Use CSS Grid for Sticky Headers and Footers. For more information, please follow other related articles on the PHP Chinese website!

The React ecosystem offers us a lot of libraries that all are focused on the interaction of drag and drop. We have react-dnd, react-beautiful-dnd,

There have been some wonderfully interconnected things about fast software lately.

I can't say I use background-clip all that often. I'd wager it's hardly ever used in day-to-day CSS work. But I was reminded of it in a post by Stefan Judis,

Animating with requestAnimationFrame should be easy, but if you haven’t read React’s documentation thoroughly then you will probably run into a few things

Perhaps the easiest way to offer that to the user is a link that targets an ID on the element. So like...

Listen, I am no GraphQL expert but I do enjoy working with it. The way it exposes data to me as a front-end developer is pretty cool. It's like a menu of

In this week's roundup, a handy bookmarklet for inspecting typography, using await to tinker with how JavaScript modules import one another, plus Facebook's

I've recently noticed an interesting change on CodePen: on hovering the pens on the homepage, there's a rectangle with rounded corners expanding in the back.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version