Grid Layout in CSS: Building Advanced Web Layouts

Mark HaverbekeMark Haverbeke
22 May, 2025
Grid Layout in CSS: Building Advanced Web Layouts

TABLE OF CONTENTS

Introduction

1 . Difference Between Flexbox and Grid

2 . Basic Setup

3 . Positioning Items with Grid

4 . Nested Grids and Responsive Design

5 . CSS Grid Examples

6 . Combining Grid and Flexbox

Conclusion

Grid Layout in CSS: Building Advanced Web Layouts

Introduction

CSS Grid Layout is a two-dimensional layout system for the web. Unlike Flexbox, which works in one direction at a time (row or column), Grid lets you control both rows and columns simultaneously. This makes it ideal for complex and responsive web layouts like dashboards and magazine-style designs. In this guide, we’ll dive deep into CSS Grid, its core properties, use cases, and how to combine it effectively with Flexbox.

1 . Difference Between Flexbox and Grid

Feature Flexbox Grid Layout
Axis One-dimensional Two-dimensional
Layout Direction Row or Column Rows and Columns
Use Case UI components Full page layouts

Flexbox is great for aligning items in a single direction (like navbars or cards), while Grid excels at defining large-scale layouts with rows and columns.


2 . Basic Setup

display: grid

  • Defines a grid container.

grid-template-columns & grid-template-rows

  • Set the number and size of rows/columns.
.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr; /* 3 equal columns */
  grid-template-rows: auto auto;     /* 2 rows */
  gap: 20px;
}

gap

  • Adds spacing between rows and columns.

3 . Positioning Items with Grid

grid-column and grid-row Control where items start and end.

.item {
  grid-column: 1 / 3; /* spans column 1 to 2 */
  grid-row: 1 / 2;
}

grid-area

  • Names a region and places an item in that named area.
.container {
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
}

.header {
  grid-area: header;
}

4 . Nested Grids and Responsive Design

Grid elements can contain other grid containers (nested grids). Combine this with media queries for responsive behavior.

@media screen and (max-width: 768px) {
  .container {
    grid-template-columns: 1fr;
  }
}

Nested grids are useful for creating modular and scalable designs.

5 . CSS Grid Examples

Dashboard Layout

.dashboard {
  display: grid;
  grid-template-areas:
    "header header"
    "sidebar main"
    "footer footer";
  grid-template-columns: 200px 1fr;
  grid-template-rows: 60px 1fr 40px;
}

Magazine Layout

.magazine {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: auto;
  gap: 10px;
}

.featured-article {
  grid-column: span 2;
  grid-row: span 2;
}

6 . Combining Grid and Flexbox

Use Grid for overall layout and Flexbox for component-level alignment inside grid items.

.card {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

This hybrid approach allows for more precise and scalable designs.

Conclusion

CSS Grid is a game changer for creating powerful, responsive, and flexible web layouts. When used alongside Flexbox, it offers unmatched control over structure and alignment. Mastering Grid Layout is essential for any front-end developer aiming to build modern and professional interfaces.

Mark Haverbeke

Mark Haverbeke

Senior Frontend Engineer

Mark is a passionate software developer and author with expertise in JavaScript and Python. He enjoys simplifying complex programming concepts and sharing practical coding tips.