The Complete CSS Grid Guide: From Basics to Advanced Layouts
This comprehensive guide will teach you everything you need to know about CSS Grid Layout, from basic concepts to advanced techniques. Use our interactive Grid Generator tool above to experiment with the concepts as you learn!
Table of Contents
- Introduction to CSS Grid
- Key Grid Concepts
- The Grid Container
- Grid Items
- Grid Lines and Tracks
- Grid Template Properties
- Grid Areas
- Item Placement
- Responsive Grid Layouts
- Common Grid Patterns
- Grid vs. Flexbox
- Browser Support
- Best Practices
- Additional Resources
Introduction to CSS Grid
CSS Grid Layout (also known as "Grid") is a two-dimensional layout system designed specifically for the web. It allows you to organize content into rows and columns and has many features that make building complex layouts straightforward.
Before CSS Grid, web developers relied on various layout methods like tables, floats, positioning, and inline-block, each with significant limitations. CSS Grid was created to solve these problems by providing a native CSS solution for creating grid-based layouts.
Unlike Flexbox, which is one-dimensional and deals with either rows OR columns, Grid is two-dimensional and can handle both rows AND columns simultaneously, making it perfect for page layouts and complex interface components.
Why Use CSS Grid?
- Create complex layouts with minimal HTML
- Control both rows and columns simultaneously
- Design layouts that adapt to different screen sizes
- Precisely control the placement of elements
- Simplify responsive design implementation
- Reduce the need for media queries in many cases
Key Grid Concepts
Before diving into the code, it's important to understand the fundamental concepts of CSS Grid:
Grid Container
The element on which display: grid
is applied. It's the direct parent of all the grid items.
Grid Item
The direct children of the grid container.
Grid Line
The dividing lines that make up the structure of the grid. They can be vertical ("column grid lines") or horizontal ("row grid lines").
Grid Track
The space between two adjacent grid lines. You can think of them as the columns or rows of the grid.
Grid Cell
The space between two adjacent row and two adjacent column grid lines. It's a single "unit" of the grid.
Grid Area
The total space surrounded by four grid lines. A grid area may be composed of any number of grid cells.
The Grid Container
To create a grid container, you set the display property of an element to grid
or inline-grid
.
.container { display: grid; }
Once you've established a grid container, you can set up the columns and rows using the grid-template-columns
and grid-template-rows
properties.
.container { display: grid; grid-template-columns: 200px 1fr 1fr; grid-template-rows: auto 1fr auto; gap: 10px; }
This creates a grid with:
- Three columns: the first is 200px wide, and the other two share the remaining space equally (1fr each)
- Three rows: the first and last adjust to their content (auto), and the middle one takes up the remaining space (1fr)
- A 10px gap between all rows and columns
Grid Items
Grid items are the direct children of a grid container. By default, each item takes up exactly one grid cell, but you can make items span multiple rows and/or columns.
.item { grid-column: 1 / 3; /* Start at line 1, end at line 3 */ grid-row: 2 / 4; /* Start at line 2, end at line 4 */ }
Grid Areas
Grid areas are a powerful feature that allows you to name regions of your grid and place items in those regions. This makes your CSS more readable and maintainable.
.container { display: grid; grid-template-columns: 200px 1fr 1fr; grid-template-rows: auto 1fr auto; grid-template-areas: "header header header" "sidebar content content" "footer footer footer"; } .header { grid-area: header; } .sidebar { grid-area: sidebar; } .content { grid-area: content; } .footer { grid-area: footer; }
Responsive Grid Layouts
CSS Grid makes it easy to create responsive layouts that adapt to different screen sizes. Here are some techniques:
Using minmax() for Responsive Columns
.container { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 20px; }
Redefining Grid Areas with Media Queries
.container { display: grid; grid-template-columns: 1fr; grid-template-areas: "header" "nav" "content" "sidebar" "footer"; } @media (min-width: 768px) { .container { grid-template-columns: 200px 1fr; grid-template-areas: "header header" "nav nav" "sidebar content" "footer footer"; } }
Common Grid Patterns
Here are some common layout patterns you can create with CSS Grid:
Holy Grail Layout
.holy-grail { display: grid; grid-template-columns: 200px 1fr 200px; grid-template-rows: auto 1fr auto; grid-template-areas: "header header header" "left-sidebar content right-sidebar" "footer footer footer"; min-height: 100vh; }
Card Grid
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 20px; }
Additional Resources
Here are some excellent resources to learn more about CSS Grid:
- MDN CSS Grid Layout - Comprehensive documentation and guides
- CSS-Tricks: A Complete Guide to Grid - Comprehensive visual guide
- Grid by Example - Examples and patterns by Rachel Andrew
- CSS Grid Garden - Learn Grid through a fun game
Ready to Create Your Own Grid Layout?
Now that you understand CSS Grid, try our interactive Grid Generator tool at the top of this page to create your own custom layouts!
Last updated: April 19, 2025
If you found this guide helpful, please share it with others who might benefit from learning about CSS Grid!