When it comes to creating two-dimensional layouts, CSS Grid is one of the most powerful tools available. Unlike Flexbox (which is mainly one-dimensional), Grid lets you arrange items in rows and columns simultaneously, making it ideal for complex layouts like web pages, dashboards, or galleries.
What is CSS Grid?
CSS Grid is a layout system that divides a container into rows and columns. Each child element (called a grid item) can be placed anywhere inside the grid.
To enable Grid, simply set the containerβs display
property to grid
:
.container {
display: grid;
}
Defining Rows and Columns
You can define how many rows and columns your grid should have using grid-template-rows
and grid-template-columns
.
Example:
.container {
display: grid;
grid-template-columns: 200px 200px 200px;
grid-template-rows: 100px 100px;
}
π This creates a grid with 3 columns (200px each) and 2 rows (100px each).
Using the fr Unit
The fr
(fractional unit) distributes available space automatically.
.container {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
}
π This makes 3 columns where the middle one is twice as wide as the others.
Gap Between Grid Items
Instead of margins, use gap
to add spacing between grid cells.
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 20px;
}
Placing Items in the Grid
You can place items in specific rows and columns using grid-column
and grid-row
.
.item1 {
grid-column: 1 / 3; /* spans across column 1 and 2 */
grid-row: 1 / 2; /* occupies row 1 */
}
Repeat Function
The repeat()
function reduces code repetition.
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
π This creates 3 equal columns without writing 1fr 1fr 1fr
.
Auto-fit and Auto-fill
Useful for responsive designs where columns should adjust to available space.
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
π This creates flexible columns that shrink no smaller than 200px
but expand as needed.
Common Mistakes Beginners Make
- Forgetting to use
display: grid;
on the parent container. - Mixing up
grid-column
vsgrid-row
. - Not using
fr
units and ending up with rigid layouts. - Ignoring
auto-fit
orauto-fill
β makes layouts non-responsive.
Quick Reference Summary
Property | Example | Description |
---|---|---|
display | display: grid; | Enables Grid layout |
grid-template-columns | 200px 1fr | Defines column sizes |
grid-template-rows | 100px auto | Defines row sizes |
fr | 1fr 2fr | Flexible fractional units |
gap | gap: 10px; | Space between items |
grid-column | 1 / 3 | Spans across columns |
grid-row | 2 / 4 | Spans across rows |
repeat() | repeat(3, 1fr) | Repeats columns/rows |
auto-fit / auto-fill | repeat(auto-fit, minmax()) | Responsive grids |
Practice Challenge
- Create a 3Γ2 grid with equal columns and rows.
- Add
gap: 15px;
between items. - Make the first item span across the entire first row.
- Make the grid responsive using
auto-fit
andminmax(150px, 1fr)
.
Conclusion
CSS Grid gives you unmatched control for creating two-dimensional layouts. While Flexbox is great for one-dimensional alignment, Grid is the best choice for full page layouts, dashboards, and galleries.