← Back to all snippets
CSS

CSS Grid: Build a Responsive Full-Page Layout

Construct a comprehensive responsive page layout with distinct header, main content, sidebar, and footer regions using CSS Grid's explicit row and column definitions.

<div class="grid-page-layout">
  <header>Header</header>
  <nav>Navigation</nav>
  <main>Main Content</main>
  <aside>Sidebar</aside>
  <footer>Footer</footer>
</div>

.grid-page-layout {
  display: grid;
  grid-template-columns: 1fr 4fr; /* Two columns: 1 part for nav/aside, 4 for main */
  grid-template-rows: auto 1fr auto; /* Rows: header (auto), main (1fr), footer (auto) */
  min-height: 100vh;
  gap: 15px; /* Spacing between grid items */
}

header {
  grid-column: 1 / -1; /* Header spans all columns */
  background-color: #f0f0f0;
  padding: 1rem;
}

nav {
  grid-column: 1 / 2; /* Nav in the first column */
  background-color: #e6e6e6;
  padding: 1rem;
}

main {
  grid-column: 2 / -1; /* Main content in the second column */
  background-color: #e0e0e0;
  padding: 1rem;
}

aside {
  grid-column: 1 / 2; /* Sidebar below nav, in the first column */
  background-color: #e6e6e6;
  padding: 1rem;
}

footer {
  grid-column: 1 / -1; /* Footer spans all columns */
  background-color: #f0f0f0;
  padding: 1rem;
}

/* Basic Responsive Adjustment */
@media (max-width: 768px) {
  .grid-page-layout {
    grid-template-columns: 1fr; /* Single column on smaller screens */
    grid-template-rows: auto auto 1fr auto auto; /* Re-order rows for mobile */
  }

  nav, main, aside, footer {
    grid-column: 1 / -1; /* All items span full width */
  }

  /* Re-order sidebar to appear below main content on mobile */
  aside {
    grid-row: 4; /* Place aside after main (which is row 3) */
  }
}
How it works: This snippet illustrates creating a full-page layout using CSS Grid with a header, navigation, main content, sidebar, and footer. It explicitly defines `grid-template-columns` and `grid-template-rows` and then places each element using `grid-column` and `grid-row`. A basic media query is included to demonstrate how to adapt the layout for smaller screens by collapsing to a single column and reordering elements using `grid-row` properties, providing a responsive design without `grid-template-areas`.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs