CSS grid is a css module used to make stuffs like these:
It is used for css layouts and positioning.
To a beginner, css grids comprises of unclear terms that makes it seem so confusing at first glance.
The grid property comprises of Rows and Columns.
From the picture above,
The Students;
Elvis, Charles, and James are in the first column.
Steve, Joe and Peter in the second column.
Dan, Jake and Paul in the third column.
The fourth column consists of Clair, Martha and Jane.
Which amounts to 4 columns.
Also,
Elvis, Steve, Dan and Clair occupy the first row.
Charles, Joe, Jake and Martha in the second row, and
James, Peter, Paul and Jane in the third row.
Thus, 4 columns and 3 rows.
In the pictures below, we have 3 rows on the first column and 2 rows on the second column.
Now that we have established what Rows and Columns are; We can apply these knowledge to css grid layouts.
The arrangement below is a block of 4 columns and 2 rows.
Let's read on and find out how this magic was done!
In the HTML code, we have:
Now, the styling:
Firstly, our blocks need to be placed in a container which is the section tag in our HTML code.
Next,
We set the section tag's display to grid to let our browser know that everything in the section tag should be displayed in a grid layout.
.container{display:grid;}
Try running these code and you'd see no changes. At least not yet.
Moving on,
We need to tell our browser the number of Rows and Columns we want our blocks to file in.
To do these;
Type,
.container { display: grid; grid-template-rows: repeat ; grid-template-columns: repeat ; }
Don't run anything yet.
Let's explain these.
Since we're creating 2 rows and 4 columns;
Our styling goes thus:
.container { display: grid; grid-template-rows:repeat(2, ); grid-template-columns: repeat(4, ); }
Wait! Wait!!
Still incomplete;
we need to state a specific width and height for each block.
We could style each of the blocks individually but that'll take time and also will result in ambiguous codes.
So, Way to go about it?
Very Easy.
Add this to your css style:
.container { display: grid; grid-template-rows: repeat(2,60px); grid-template-columns: repeat(4,1fr); }
Here,
60px represents the height and 1fr represents the width for each blocks.
Of course,
You can use any values (or even set to auto) and any units (px, em, fr, %, etc).
Try playing around with the Rows and Columns until you gain adequate mastery of CSS grids.
Yeah,
Looks nice but there's a teeny-weeny problem, which is spacing.
To space CSS grids we use grid-gap.
The grid-gap property is very much similar to css margin and they work the same way around blocks/elements.
The same way,
Margin: 50px is interpreted as:
margin-top: 50px margin-bottom: 50px margin-right: 50px margin-left: 50pxAnd Margin: 20px 30px is interpreted as:
margin-top: 20px margin-bottom: 20px margin-right: 30px margin-left: 30px;The same principle applies to grid-gap such that; Grid-gap: 50px is interpreted as:
grid-row-gap: 50px grid-column-gap: 50pxAnd Grid-gap: 20px 30px is interpreted as:
grid-row-gap: 20px grid-column-gap: 30px;Now, Let's update our CSS code:
.container { display: grid; grid-template-rows: repeat(2,60px); grid-template-columns: repeat(4,1fr); grid-gap: 10px; }
Voila!
Note: Code snippets posted here includes the grid styling only and as such cannot output exact color and other stylings as in display pictures.
To get the full code,
You can fork this pen:
https://codepen.io/devhalimah/pen/eYzYKLa/
Top comments (12)
Overall your article is good enough. I have learnt much more about CSS grids but now thing stucked me as your written lines are....
"The vertical arrangements of James, Peter, Paul and Jane is a COLUMN.
i.e we have 4 columns.
The horizontal arrangement of James, Charles, and Elvis;"
As I know about vertical vs horizontal views are like below picture,
Thanks for the observation Fahad.
Please go through that section again,
I just edited the post for better understanding.
By the way, I'm glad you learnt more on css grid 😊.
Thankful to u
Nice article Halimah!
Just wanted to add a pretty cool property. As you did on the example, your 2 first rows are going to be with a height of 60px. But what is going to be the height for the upcoming rows?
For that comes into the game grid-auto-rows. With this property you can set the height of the upcoming rows in general. This will allow you to control your layout with more data that could be entered.
So, if we want all rows to be 60px height we can just remove the grid-template-rows and add grid-auto-rows: 60px;
Or if we want the 2 first with 60px and the rest to be 40px for example, we can add grid-auto-rows: 40px;
Congrats on the post :)
Wow
Nice one Amir 👍
Thanks for this!
I learnt from it too 😊
Thanks Halimah!
You're welcome John Peters.
Thanks Halimah. The post is really educative
You're welcome Hamza.
I'm glad it helped 😊
Thank you Halimah!
I learnt something today.
You're welcome Ibby!
I'm glad you did.
Thanks for sharing Halimah