CSS

Mastering CSS Grid Layout

Deep dive into CSS Grid and learn how to create complex, responsive layouts with ease.

J John Doe
12 min read
CSS Grid Layout Illustration

Prerequisites

This guide assumes you have basic knowledge of HTML and CSS. If you're new to CSS, we recommend starting with our Introduction to Web Development post.

What is CSS Grid?

CSS Grid Layout is a powerful two-dimensional layout system designed for the web. It lets you lay out items in rows and columns, and has many features that make building complex layouts straightforward.

Unlike Flexbox, which is primarily a one-dimensional layout method, Grid is designed for two-dimensional layouts. This means you can control both rows and columns simultaneously, making it perfect for creating complex page layouts.

Key Concepts

1. Grid Container vs Grid Items

To use CSS Grid, you need to understand two main concepts:

  • Grid Container: The element on which display: grid is applied
  • Grid Items: The direct children of the grid container

Basic Grid Setup

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: auto auto;
  gap: 20px;
}

2. Grid Lines

Grid lines are the horizontal and vertical dividing lines that make up the structure of the grid. They are numbered starting from 1, and you can reference them to position grid items.

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6

Visual representation of a 3x2 grid with grid lines

Grid Properties

Container Properties

display

Defines the element as a grid container:

.container {
  display: grid; /* or display: inline-grid */
}

grid-template-columns

Defines the columns of the grid:

Common Values:
  • grid-template-columns: 1fr 1fr 1fr; - Three equal columns
  • grid-template-columns: 200px 1fr 2fr; - Fixed, flexible, and double flexible
  • grid-template-columns: repeat(3, 1fr); - Three equal columns using repeat()
  • grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); - Responsive columns

grid-template-rows

Defines the rows of the grid:

.container {
  grid-template-rows: 100px auto 200px;
}

gap

Sets the gap between grid items:

.container {
  gap: 20px; /* Both row and column gap */
  /* Or separately: */
  row-gap: 20px;
  column-gap: 30px;
}

Item Properties

grid-column

Specifies which columns an item should span:

1
2 (spans 2 columns)
3
4
.item-2 {
  grid-column: 2 / 4; /* Start at line 2, end at line 4 */
  /* Or using span: */
  grid-column: 2 / span 2;
}

grid-row

Specifies which rows an item should span:

.item {
  grid-row: 1 / 3; /* Span from row 1 to row 3 */
}

Practical Examples

1. Holy Grail Layout

The classic three-column layout with header and footer:

Header
Sidebar
Main Content
Sidebar
Footer
.holy-grail {
  display: grid;
  grid-template-columns: 1fr 3fr 1fr;
  grid-template-rows: auto 1fr auto;
  min-height: 100vh;
  gap: 20px;
}

.header { grid-column: 1 / -1; }
.sidebar-left { grid-column: 1; }
.main { grid-column: 2; }
.sidebar-right { grid-column: 3; }
.footer { grid-column: 1 / -1; }

2. Responsive Card Grid

A responsive grid that adapts to different screen sizes:

Card 1

This is a sample card content.

Card 2

This is a sample card content.

Card 3

This is a sample card content.

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 20px;
}

Advanced Techniques

Named Grid Areas

Use named areas to create more readable and maintainable layouts:

.layout {
  display: grid;
  grid-template-areas: 
    "header header header"
    "sidebar main aside"
    "footer footer footer";
  grid-template-columns: 1fr 3fr 1fr;
  grid-template-rows: auto 1fr auto;
}

.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.aside { grid-area: aside; }
.footer { grid-area: footer; }

Grid Auto-Flow

Control how items flow into the grid:

Dense Packing

1
2 (large)
3
4
.container {
  grid-auto-flow: dense; /* Fills gaps automatically */
}

Browser Support

Excellent Browser Support

CSS Grid is supported in all modern browsers:

  • • Chrome 57+ (2017)
  • • Firefox 52+ (2017)
  • • Safari 10.1+ (2017)
  • • Edge 16+ (2017)

Best Practices

Tips for Success

  • Use fr units for flexible columns instead of percentages
  • Combine Grid with Flexbox for complex layouts
  • Use minmax() for responsive designs
  • Name your grid areas for better maintainability
  • Test your layouts on different screen sizes

Common Mistakes to Avoid

Watch Out For

  • • Forgetting to set display: grid on the container
  • • Confusing Grid with Flexbox use cases
  • • Not considering mobile-first responsive design
  • • Overcomplicating simple layouts with Grid
  • • Ignoring browser support for older browsers

Conclusion

CSS Grid Layout is a powerful tool that revolutionizes how we create web layouts. Its two-dimensional nature makes it perfect for complex page structures, while its flexibility allows for responsive and maintainable designs.

Start with simple grids and gradually work your way up to more complex layouts. Remember that Grid works great alongside Flexbox - use Grid for overall page layout and Flexbox for component-level layouts.

Ready to Practice?

Test your CSS Grid knowledge with our interactive quiz and reinforce what you've learned!

Take CSS Quiz

Resources

Tags: CSS Grid Layout Responsive