CSS Grid Layout is one of the most powerful layout systems available in CSS. It allows you to create complex, responsive layouts with ease. In this comprehensive guide, we'll explore everything you need to know about CSS Grid, from the basics to advanced techniques.
What is CSS Grid?
CSS Grid is a two-dimensional layout system that allows you to create grid-based layouts with rows and columns. Unlike Flexbox, which is primarily one-dimensional, Grid enables you to control both rows and columns simultaneously, making it perfect for complex layouts.
Basic Grid Setup
To create a grid, you simply need to set the display property to grid on a container element:
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: auto;
gap: 20px;
}
CSS Grid allows flexible column and row definitions
Grid Template Columns and Rows
The grid-template-columns and grid-template-rows properties define the structure of your grid. You can use various units:
- fr (fractional unit): Divides available space proportionally
- px, em, rem: Fixed sizes
- auto: Sizes based on content
- minmax(): Sets minimum and maximum sizes
/* Example: Three equal columns */
grid-template-columns: 1fr 1fr 1fr;
/* Example: Mixed units */
grid-template-columns: 200px 1fr 2fr;
/* Example: Responsive with minmax */
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
Grid Gap
The gap property (or grid-gap for older browsers) adds spacing between grid items:
.container {
gap: 20px; /* Same gap for rows and columns */
/* Or separately: */
row-gap: 20px;
column-gap: 30px;
}
Grid Item Placement
You can control where items are placed in the grid using line numbers or named lines:
.item {
grid-column-start: 1;
grid-column-end: 3;
grid-row-start: 1;
grid-row-end: 2;
/* Shorthand */
grid-column: 1 / 3;
grid-row: 1 / 2;
/* Span syntax */
grid-column: span 2;
}
Grid Areas
Named grid areas make layouts more readable and maintainable:
.container {
display: grid;
grid-template-areas:
"header header header"
"sidebar main main"
"footer footer footer";
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: auto 1fr auto;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main { grid-area: main; }
.footer { grid-area: footer; }
Responsive Grid Layouts
CSS Grid excels at creating responsive layouts. Here are some common patterns:
Auto-Fit and Auto-Fill
/* Auto-fit: collapses empty tracks */
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
/* Auto-fill: keeps empty tracks */
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
Media Query Breakpoints
/* Mobile first */
.container {
grid-template-columns: 1fr;
}
/* Tablet */
@media (min-width: 768px) {
.container {
grid-template-columns: repeat(2, 1fr);
}
}
/* Desktop */
@media (min-width: 1024px) {
.container {
grid-template-columns: repeat(3, 1fr);
}
}
Alignment in Grid
Grid provides powerful alignment properties:
.container {
/* Align items within their grid area */
align-items: start | end | center | stretch;
justify-items: start | end | center | stretch;
/* Align the entire grid */
align-content: start | end | center | stretch | space-around | space-between;
justify-content: start | end | center | stretch | space-around | space-between;
}
.item {
/* Align individual items */
align-self: start | end | center | stretch;
justify-self: start | end | center | stretch;
}
Practical Examples
Example 1: Card Grid
Responsive card grid layout using CSS Grid
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 2rem;
padding: 2rem;
}
Example 2: Holy Grail Layout
.layout {
display: grid;
grid-template-areas:
"header header header"
"nav main aside"
"footer footer footer";
grid-template-columns: 200px 1fr 200px;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}
Example 3: Magazine Layout
.magazine {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(3, 200px);
gap: 1rem;
}
.featured {
grid-column: 1 / 3;
grid-row: 1 / 3;
}
Best Practices
- Use Grid for 2D layouts: When you need to control both rows and columns
- Combine with Flexbox: Use Grid for page layout, Flexbox for component layout
- Use semantic HTML: Grid doesn't change document structure
- Test responsiveness: Always test your grid layouts on different screen sizes
- Use named areas: Makes code more maintainable and readable
Browser Support
CSS Grid is supported in all modern browsers. For older browsers, you can use feature queries:
@supports (display: grid) {
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
}
@supports not (display: grid) {
.container {
display: flex;
flex-wrap: wrap;
}
}
Conclusion
CSS Grid Layout is a powerful tool that simplifies complex layout challenges. With its two-dimensional capabilities, it's perfect for creating modern, responsive web layouts. Start with the basics, practice with real projects, and you'll soon master this essential CSS feature.