If you've ever tried to build a complex webpage layout using only div, flex, margins, and a lot of CSS adjustments, you probably know how quickly things can become messy.
You might start with something simple:
.container {
display: flex;
}
Then you need multiple rows and columns.
Suddenly, you're adding widths, margins, wrapping, and media queries just to make everything line up.
This is where CSS Grid becomes extremely useful.
CSS Grid is a CSS layout system designed specifically for creating layouts with rows and columns.
Whether you're building a dashboard, blog page, product listing, admin panel, portfolio, or responsive website, CSS Grid can make the layout much easier to manage.
In this article, we'll start with the basics and gradually move toward practical, real-world examples.
What is CSS Grid?
CSS Grid Layout is a two-dimensional CSS layout system.
The phrase two-dimensional is important.
With Grid, you can control both:
-
Rows
-
Columns
For example:
+---------+---------+---------+
| | | |
| A | B | C |
| | | |
+---------+---------+---------+
| | | |
| D | E | F |
| | | |
+---------+---------+---------+
You can control where each item appears within this structure.
This makes Grid particularly useful for layouts that need control over both horizontal and vertical directions.
Creating Your First Grid
Let's start with a simple example.
HTML:
<div class="container">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
</div>
CSS:
.container {
display: grid;
}
That's enough to turn the container into a grid.
But we haven't told CSS how many columns we want yet.
Let's do that.
grid-template-columns
The grid-template-columns property defines the columns in your grid.
For example:
.container {
display: grid;
grid-template-columns: 200px 200px 200px;
}
This creates three columns:
+---------+---------+---------+
| Item 1 | Item 2 | Item 3 |
+---------+---------+---------+
| Item 4 | | |
+---------+---------+---------+
Each column is 200px wide.
You can also use different sizes:
.container {
display: grid;
grid-template-columns: 100px 200px 300px;
}
Now the columns have different widths.
Using Fractional Units
One of the most useful Grid features is the fr unit.
fr means fraction of the available space.
For example:
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
This creates three equal columns.
+----------+----------+----------+
| Item 1 | Item 2 | Item 3 |
+----------+----------+----------+
You don't have to calculate percentages or fixed widths.
You can also create different proportions:
.container {
display: grid;
grid-template-columns: 1fr 2fr;
}
The second column gets twice as much available space as the first.
+--------+----------------+
| | |
| 1fr | 2fr |
| | |
+--------+----------------+
This is very useful for layouts such as:
Sidebar | Main Content
For example:
.layout {
display: grid;
grid-template-columns: 250px 1fr;
}
The sidebar gets 250px, while the main content takes the remaining space.
grid-template-rows
Just as grid-template-columns controls columns, grid-template-rows controls rows.
.container {
display: grid;
grid-template-rows: 100px 200px;
}
This creates:
Row 1 → 100px
Row 2 → 200px
You can combine both:
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 100px 200px;
}
Now you have control over both dimensions.
The gap Property
Just like Flexbox, CSS Grid supports the gap property.
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 20px;
}
This creates consistent spacing between rows and columns.
Without gap, you might be tempted to add margins to every card.
Instead of:
.card {
margin-right: 20px;
margin-bottom: 20px;
}
you can simply use:
.container {
gap: 20px;
}
This usually results in cleaner CSS.
row-gap and column-gap
You can also control row and column spacing separately.
.container {
row-gap: 30px;
column-gap: 20px;
}
Or using the shorthand:
.container {
gap: 30px 20px;
}
The first value is the row gap.
The second value is the column gap.
repeat()
Writing this:
grid-template-columns: 1fr 1fr 1fr 1fr;
works perfectly fine.
But when you have many identical columns, repeat() makes the CSS cleaner.
grid-template-columns: repeat(4, 1fr);
This means:
Create four columns, each taking one fraction of the available space.
For example:
.container {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 20px;
}
You get:
+------+------ +------+------+
| 1 | 2 | 3 | 4 |
+------+-------+------+------+
repeat() with Fixed Sizes
You can also use:
grid-template-columns: repeat(3, 200px);
This creates:
200px 200px 200px
repeat() with Different Patterns
You can combine fixed values and fractions:
grid-template-columns: 200px repeat(2, 1fr);
This creates:
200px 1fr 1fr
This is useful when you have a fixed sidebar and flexible content areas.
minmax()
One of the most useful CSS Grid functions is minmax().
It allows you to specify a minimum and maximum size.
For example:
grid-template-columns: repeat(3, minmax(200px, 1fr));
This means:
Each column should be at least
200px, but it can grow up to1fr.
This becomes very useful when building responsive layouts.
Responsive Grid with auto-fit
Here's where CSS Grid becomes really powerful.
Suppose you have product cards:
<div class="products">
<div class="card">Product 1</div>
<div class="card">Product 2</div>
<div class="card">Product 3</div>
<div class="card">Product 4</div>
<div class="card">Product 5</div>
<div class="card">Product 6</div>
</div>
You could write:
.products {
display: grid;
grid-template-columns: repeat(
auto-fit,
minmax(250px, 1fr)
);
gap: 20px;
}
Now the browser automatically decides how many columns can fit.
On a large screen:
+--------+--------+--------+--------+
| Card 1 | Card 2 | Card 3 | Card 4 |
+--------+--------+--------+--------+
On a smaller screen:
+--------+--------+
| Card 1 | Card 2 |
+--------+--------+
| Card 3 | Card 4 |
+--------+--------+
And on mobile:
+--------+
| Card 1 |
+--------+
| Card 2 |
+--------+
| Card 3 |
+--------+
You may not even need a media query for this particular layout.
auto-fit vs auto-fill
You may have seen both:
auto-fit
and:
auto-fill
They are similar, but they behave slightly differently when there is extra space.
auto-fit
auto-fit tries to fit as many columns as possible and allows the existing columns to expand when there is extra space.
auto-fill
auto-fill tries to preserve the potential grid tracks even when there aren't enough items to fill them.
For many card-based responsive layouts, you'll commonly see:
repeat(auto-fit, minmax(250px, 1fr))
It's a great pattern to remember.
Grid Lines
CSS Grid creates invisible lines around the rows and columns.
For example:
1 2 3 4
| | | |
+-------+-------+-------+
| | | |
+-------+-------+-------+
| | | |
+-------+-------+-------+
The numbers represent grid lines.
You can use these lines to position elements.
grid-column
Suppose we have:
.item {
grid-column: 1 / 3;
}
This means:
Start at column line 1 and end at column line 3.
So the item occupies two columns.
+-------------------+-------+
| | |
| Item | |
| | |
+-------------------+-------+
You can also use:
grid-column: 2 / 4;
This starts at line 2 and ends at line 4.
grid-row
Similarly:
.item {
grid-row: 1 / 3;
}
means the item spans two rows.
This becomes extremely useful when creating dashboard layouts.
span
Instead of specifying exact line numbers, you can use span.
For example:
.item {
grid-column: span 2;
}
This means:
Make this item occupy two columns.
Similarly:
.item {
grid-row: span 2;
}
makes the item occupy two rows.
Creating a Dashboard Layout
Let's create a simple dashboard.
HTML:
<div class="dashboard">
<div class="sidebar">Sidebar</div>
<div class="header">Header</div>
<div class="content">Content</div>
</div>
CSS:
.dashboard {
display: grid;
grid-template-columns: 250px 1fr;
grid-template-rows: 70px 1fr;
min-height: 100vh;
}
The layout becomes:
+-------------+----------------------+
| | Header |
| Sidebar +----------------------+
| | |
| | Content |
| | |
+-------------+----------------------+
We can position the elements:
.sidebar {
grid-row: 1 / 3;
}
.header {
grid-column: 2;
}
.content {
grid-column: 2;
}
Now the sidebar spans both rows.
CSS Grid Areas
For larger layouts, grid-template-areas can make your CSS much easier to understand.
Let's create a layout:
+---------------------------+
| Header |
+---------+-----------------+
| Sidebar | Content |
+---------+-----------------+
| Footer |
+---------------------------+
HTML:
<div class="layout">
<header>Header</header>
<aside>Sidebar</aside>
<main>Content</main>
<footer>Footer</footer>
</div>
CSS:
.layout {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-areas:
"header header"
"sidebar content"
"footer footer";
}
header {
grid-area: header;
}
aside {
grid-area: sidebar;
}
main {
grid-area: content;
}
footer {
grid-area: footer;
}
This is much easier to visualize because the CSS itself describes the layout.
Grid Area Names
The grid-template-areas approach becomes especially useful for application layouts.
For example:
.layout {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-areas:
"sidebar header"
"sidebar main";
}
Then:
.sidebar {
grid-area: sidebar;
}
.header {
grid-area: header;
}
.main {
grid-area: main;
}
This is a clean approach for dashboards and admin applications.
Aligning Items in Grid
CSS Grid also provides alignment properties.
justify-items
Controls horizontal alignment of items inside their grid cells.
.container {
justify-items: center;
}
align-items
Controls vertical alignment of items inside their grid cells.
.container {
align-items: center;
}
place-items
A shorthand for both.
.container {
place-items: center;
}
This is another useful shortcut for centering content.
Centering an Element with Grid
Just like Flexbox, Grid makes centering easy.
.container {
display: grid;
place-items: center;
min-height: 100vh;
}
HTML:
<div class="container">
<h1>Hello World</h1>
</div>
The heading is centered horizontally and vertically.
Pretty convenient.
justify-content and align-content
These properties control the entire grid inside the container when there is extra space.
For example:
.container {
display: grid;
justify-content: center;
align-content: center;
}
This is different from:
justify-items
align-items
A useful distinction is:
justify-content → positions the entire grid
align-content → positions the entire grid
justify-items → positions items inside their cells
align-items → positions items inside their cells
A Real-World Blog Card Layout
Let's say you're building a blog website.
You have:
<div class="posts">
<article class="post">Post 1</article>
<article class="post">Post 2</article>
<article class="post">Post 3</article>
<article class="post">Post 4</article>
<article class="post">Post 5</article>
<article class="post">Post 6</article>
</div>
A responsive layout can be created with:
.posts {
display: grid;
grid-template-columns: repeat(
auto-fit,
minmax(280px, 1fr)
);
gap: 24px;
}
This gives you a responsive blog card grid without manually defining the number of columns for every screen size.
This pattern is particularly useful for:
-
Blog websites
-
E-commerce products
-
Portfolio projects
-
News websites
-
Course cards
-
Dashboard widgets
CSS Grid vs Flexbox
This is one of the most common questions.
Should you use Grid or Flexbox?
The answer depends on the problem.
Flexbox
Flexbox is primarily a one-dimensional layout system.
It works with either:
Row
or:
Column
For example:
Logo | Navigation | Button
Flexbox is excellent for:
-
Navigation bars
-
Buttons
-
Toolbars
-
Form elements
-
Aligning items
-
Component-level layouts
CSS Grid
Grid is a two-dimensional layout system.
It works with:
Rows + Columns
For example:
+------+------+
| | |
| A | B |
| | |
+------+------+
| | |
| C | D |
| | |
+------+------+
Grid is excellent for:
-
Dashboards
-
Page layouts
-
Card grids
-
Complex layouts
-
Sidebar + content layouts
-
Two-dimensional positioning
Do You Have to Choose One?
No.
In real-world applications, Flexbox and Grid often work together.
For example:
CSS Grid
↓
Page Layout
↓
+-------------+----------------+
| Sidebar | Main Content |
+-------------+----------------+
↓
Flexbox
↓
Card / Toolbar
You might use Grid for the overall page structure and Flexbox inside individual components.
That's completely normal.
Common CSS Grid Mistakes
1. Forgetting display: grid
This won't work:
.container {
grid-template-columns: repeat(3, 1fr);
}
You need:
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
2. Using too many fixed widths
You might write:
grid-template-columns: 300px 300px 300px;
This can cause problems on smaller screens.
Instead, consider:
grid-template-columns: repeat(3, 1fr);
or for responsive cards:
grid-template-columns:
repeat(auto-fit, minmax(250px, 1fr));
3. Forgetting gap
Instead of manually adding margins:
.card {
margin: 10px;
}
consider:
.container {
gap: 20px;
}
It makes the relationship between grid items much clearer.
4. Confusing grid lines with grid items
If you write:
grid-column: 1 / 3;
the numbers refer to grid lines, not the number of columns.
This is an important distinction.
A Practical CSS Grid Example
Let's put several concepts together.
HTML:
<div class="products">
<div class="product">Product 1</div>
<div class="product">Product 2</div>
<div class="product">Product 3</div>
<div class="product">Product 4</div>
<div class="product">Product 5</div>
<div class="product">Product 6</div>
</div>
CSS:
.products {
display: grid;
grid-template-columns:
repeat(auto-fit, minmax(220px, 1fr));
gap: 20px;
padding: 20px;
}
.product {
padding: 30px;
border: 1px solid #ddd;
}
That's a complete responsive grid with just a few lines of CSS.
No complicated width calculations.
No JavaScript.
No need to manually change the number of columns at every breakpoint.
Useful CSS Grid Properties to Remember
You don't need to memorize every Grid property immediately.
Start with these:
Container properties
display: grid;
grid-template-columns;
grid-template-rows;
gap;
row-gap;
column-gap;
grid-template-areas;
justify-items;
align-items;
place-items;
Item properties
grid-column;
grid-row;
grid-area;
justify-self;
align-self;
Useful functions
repeat();
minmax();
Once you're comfortable with these, you'll be able to build most common Grid layouts.
When Should You Use CSS Grid?
A simple rule is:
Use CSS Grid when you need control over rows and columns.
For example, if you're building:
-
A dashboard
-
An admin panel
-
A product grid
-
A blog layout
-
A photo gallery
-
A page with sidebar and main content
-
A complex responsive layout
Grid is likely worth considering.
If you're simply trying to align:
Icon + Text + Button
Flexbox might be simpler.
Final Thoughts
CSS Grid changed the way we build layouts with CSS.
Before modern layout systems became widely available, developers often had to rely on floats, positioning, complicated margins, and other workarounds.
Today, Grid gives us a clean way to describe layouts using rows and columns.
The most important concepts to understand are:
-
display: gridcreates the grid container. -
grid-template-columnsdefines columns. -
grid-template-rowsdefines rows. -
gapcontrols spacing. -
frdistributes available space. -
repeat()makes repeated tracks easier to define. -
minmax()is extremely useful for responsive layouts. -
grid-columnandgrid-rowcontrol item placement. -
grid-template-areasmakes complex layouts easier to visualize. -
Grid and Flexbox are complementary, not competing technologies.
You don't need to memorize everything before using CSS Grid.
Start with a simple layout.
Create a few columns.
Add some gaps.
Try fr.
Then experiment with repeat(), minmax(), and auto-fit.
Once you start using Grid in real projects, the syntax will quickly become familiar.
And you'll probably find yourself asking a very different question when building a layout:
"Can CSS Grid handle this?"
In most cases, the answer is yes.