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 element as a grid container.
Example π
display: grid;
Values:
Defines the column sizes of the grid.
Example π
grid-template-columns: 1fr 2fr;
Values:
Defines the row sizes of the grid.
Example π
grid-template-rows: 100px 200px;
Values: Same as grid-template-columns.
Defines named grid areas for layout.
Values: String of area names in quotes.
Example π
grid-template-areas:
"header header"
"sidebar content"
"footer footer";
Shorthand for defining rows, columns, and areas.
Values: Combines grid-template-rows, grid-template-columns, and grid-template-areas.
Specifies the size of implicitly created columns.
Values: auto, length, percentage, fr, etc.
Specifies the size of implicitly created rows.
Example π
grid-auto-rows: 100px;
Values: Same as grid-auto-columns.
Controls the direction in which auto-placed items flow.
Values:
Specifies the starting grid line for an itemβs column.
Example π
grid-column-start: 1;
Values:
Specifies the ending grid line for an itemβs column.
Example π
grid-column-end: 3;
Values: Same as grid-column-start.
Specifies the starting grid line for an itemβs row.
Example π
grid-row-end: 1;
Values: Same as grid-column-start.
Specifies the ending grid line for an itemβs row.
Example π
grid-row-end: 3;
Values: Same as grid-column-start.
Shorthand for grid-column-start / grid-column-end.
Example π
grid-column: 1 / 3;
Values:
Shorthand for grid-row-start / grid-row-end.
Example π
grid-row: 1 / 2;
Values:
Defines a grid itemβs location using grid-row-start / grid-column-start / grid-row-end / grid-column-end or by area name.
Example π
grid-area: header;
Values:
Shorthand for align-items and justify-items.
Values: center, start, end, stretch
Shorthand for align-content and justify-content.
Values: center, start, end, stretch, space-between, space-around, space-evenly
Shorthand for align-self and justify-self.
Values: center, start, end, stretch
Aligns items inside their grid area along the inline (row) axis.
Example π
justify-items: center;
Values: start, end, center, stretch
Aligns items inside their grid area along the block (column) axis.
Example π
align-items: center;
Values: start, end, center, stretch
Aligns the whole grid within the container along the row axis.
Example π
justify-content: space-between;
Values: start, end, center, space-between, space-around, space-evenly, stretch
Aligns the grid within the container along the column axis.
Example π
align-content: center;
Values: Same as justify-content
Aligns a single item along the row axis inside its grid area.
Values: start, end, center, stretch
Aligns a single item along the column axis inside its grid area.
Values: Same as justify-self
Defines spacing between rows and columns.
Example π
gap: 20px;
row-gap: 10px;
column-gap: 15px;
Values:
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).