How to Use CSS Grid for Sticky Headers and Footers
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!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The CSS box-shadow and outline properties gained theme.json support in WordPress 6.1. Let's look at a few examples of how it works in real themes, and what options we have to apply these styles to WordPress blocks and elements.

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

In this article we will be diving into the world of scrollbars. I know, it doesn’t sound too glamorous, but trust me, a well-designed page goes hand-in-hand

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

npm commands run various tasks for you, either as a one-off or a continuously running process for things like starting a server or compiling code.

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and
