Table of Contents
Project Overview
Initial Styles
Content Integration
Fixed Header and Footer
Adjustable Main Section Width
Traditional Sticky Footer
Conclusion
Home Web Front-end CSS Tutorial How to Use CSS Grid for Sticky Headers and Footers

How to Use CSS Grid for Sticky Headers and Footers

Apr 02, 2025 pm 06:29 PM

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;
}
Copy after login

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;
}
Copy after login

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.

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

The final demo showcases this responsive behavior.

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>
Copy after login

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';
}
Copy after login

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;
}
Copy after login

Footer positioning within this grid:

#app > footer {
  grid-area: 2 / 1 / 3 / 4;
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Adding Box Shadows to WordPress Blocks and Elements Adding Box Shadows to WordPress Blocks and Elements Mar 09, 2025 pm 12:53 PM

The CSS box-shadow and outline properties gained theme.json support in WordPress 6.1. Let&#039;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.

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

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

Making Your First Custom Svelte Transition Making Your First Custom Svelte Transition Mar 15, 2025 am 11:08 AM

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

Classy and Cool Custom CSS Scrollbars: A Showcase Classy and Cool Custom CSS Scrollbars: A Showcase Mar 10, 2025 am 11:37 AM

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

Show, Don't Tell Show, Don't Tell Mar 16, 2025 am 11:49 AM

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

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

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

What the Heck Are npm Commands? What the Heck Are npm Commands? Mar 15, 2025 am 11:36 AM

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.

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

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

See all articles