This tutorial aims to introduce you to the world of CSS Grid, a powerful layout system in CSS. It provides a simple, efficient method to design your web layout in both rows and columns, perfect for complex web layouts.
By the end of this tutorial, you will be able to understand how the CSS Grid works and be capable of using it to design your own web layouts.
To get the most out of this tutorial, you should have a basic understanding of HTML and CSS.
CSS Grid Layout is a 2-dimensional system, meaning it can handle both columns and rows. We define a grid container with display: grid
and then we place child elements into the grid.
For a basic grid layout, you need to follow these steps:
display: grid
.grid-template-rows
and grid-template-columns
.grid-column
and grid-row
.Let's create a simple 3x3 grid:
.container {
display: grid;
grid-template-columns: 100px 100px 100px;
grid-template-rows: 100px 100px 100px;
}
.item {
grid-column: 1 / 3;
grid-row: 1 / 3;
}
In this example, .container
is the grid container, and .item
is a grid item. The grid-template-columns
and grid-template-rows
properties define the number and sizes of the rows and columns. The grid-column
and grid-row
properties on the item specify where the item should be placed in the grid.
In this tutorial, we have covered the basics of CSS Grid. You have learned how to create a grid container with display:grid
and how to define and place grid items using grid-template-columns
, grid-template-rows
, grid-column
, and grid-row
.
For further learning, you can explore more about grid properties such as grid-gap
, grid-auto-rows
, grid-auto-columns
, and grid-template-areas
.
Now let's apply what we've learned with some exercises:
.container {
display: grid;
grid-template-columns: 50px 50px;
grid-template-rows: 50px 50px;
}
.item {
grid-row: 1 / span 1;
grid-column: 1 / span 2;
}
.container {
display: grid;
grid-template-areas:
"header header"
"sidebar content"
"footer footer";
}
.header {
grid-area: header;
}
.sidebar {
grid-area: sidebar;
}
.content {
grid-area: content;
}
.footer {
grid-area: footer;
}
Keep practicing and exploring more about CSS Grid. Happy coding!