visit
CSS Grid Layout is a two-dimensional system, meaning it can handle both columns and rows, unlike flexbox which is largely a one-dimensional system (either in a column or a row). A core difference between CSS Grid and Flexbox is that — CSS Grid’s approach is layout-first while Flexbox’s approach is content-first.Enough of speaking about why and what is a Grid system. And let’s see how to work with the CSS grid System.
From my point of view, the best and easiest way to work with the CSS grid is to use the magical grid property “grid-template-areas”. but before jumping into that, we need to understand first how the CSS grid is structured and get familiar with some technical words. Grid systems usually have a grid container and grid items. A grid container is the HTML element that has a display property of grid “display: grid”. and a grid item is every element that a direct descendant of that element.
let’s return back to our magical property that I have mentioned before. so to start with the CSS grid. we use “display: grid”, and we define our columns and rows, then after that, we use that magical property to draw our areas as shown in the code below :
.container {
display: grid;
gird-template-columns: repeat(6, 1fr);
grid-template-rows: repeat(3, 1fr) 100px;
grid-template-areas:
“header header header header header header”
“main main main . sidebar sidebar”
“main main main . sidebar sidebar”
“ . footer footer footer footer . “;
}
so
repeat()
is a CSS grid function, that allows us to instead of repeating many values of columns and rows sizes. to just type in repeat n times that value. in other terms, it makes sure that we don’t repeat our self’s, so instead of typing “300px 300px 300px” we can type “repeat(3, 300px)”.There is a new unit of measure in CSS introduced with CSS grid called fraction ( fr ), this unit allows us to share space based on the extra space. Here is a YouTube video that explains it.Let’s continue, so in grid-template-areas we kind of have defined some areas that we will be used later by the grid items in order to align there self’s in them. Now let’s see how a grid item should look like..header {
gird-area: header;
}
.main {
grid-area: main;
}
...
@media only screen and (max-width: 700px) {
.container {
gird-template-columns: 1fr;
grid-template-areas:
“header”
“sidebar”
“main
“footer“;
}
That’s all friends.
I hope you’ve enjoyed this article and learned from my experience.
If you can like this post and share it, I will appreciate applause and sharing :-)
You can contact or follow me: