Home 〉 CSS Tutorials 〉 CSS Grid Layout Tutorial for Beginners | Complete Guide with Examples
CSS Grid Layout Tutorial for Beginners | Complete Guide with Examples !
Need a powerful tool to build multi-column layouts? CSS Grid Layout lets you design complex layouts with rows and columns using simple properties like `grid-template-rows` and `grid-template-columns`. This tutorial walks you through auto-placement, grid areas, and responsive designs. It works well alongside Flexbox for modern layouts.
CSS Grid is a powerful layout system in CSS that allows you to create responsive, structured, and complex web layouts easily. In our earlier post, we explained CSS Display Property in detail. Here, we will focus specifically on 'display: grid'.
CSS Grid is a 2-dimensional layout system, meaning you can control both rows and columns. It gives you full control over placement and spacing.
To activate grid, you set:
Example 📄
display: grid;
on a container.
<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
gap: 10px;
}
.grid-item {
background-color: lightblue;
padding: 20px;
text-align: center;
}
</style>
</head>
<body>
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
</div>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Defines the number and size of columns and rows.
Example 📄
grid-template-columns: 1fr 2fr;
grid-template-rows: 100px 200px;
'fr' is a flexible unit. '1fr 2fr' means the second column is twice as wide.
Adds spacing between columns and rows.
Example 📄
gap: 20px; /* or */
row-gap: 10px;
column-gap: 15px;
Controls item placement.
Example 📄
grid-column: 1 / 3; /* spans across column 1 to 2 */
grid-row: 1 / 2;
Used with named areas or shorthand placement.
When used with 'grid-template-areas'.
Assign names to grid areas.
Example 📄
grid-area: header;
.grid-container {
display: grid;
grid-template-areas:
"header header"
"sidebar content";
grid-template-columns: 1fr 3fr;
}
.header {
grid-area: header;
}
.sidebar {
grid-area: sidebar;
}
.content {
grid-area: content;
Aligns items inside grid cells.
Example 📄
justify-items: center;
align-items: center;
Aligns the whole grid inside the container.
Example 📄
justify-content: space-between;
align-content: center;
Sets size for automatically created rows or columns.
Example 📄
grid-auto-rows: 100px;
Example 📄
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
CSS Grid is a clean and modern way to build complex layouts without using floats or positioning. With the grid system, you can control alignment, spacing, and structure in a powerful and responsive way.
Take time to practice, and combine grid with media queries for a full responsive experience.
Yes, you can use Grid for the main layout and Flexbox inside grid items for alignment.
Yes, it is well supported in all modern browsers (Chrome, Firefox, Edge, Safari).