Complete CSS Grid Tutorial With Cheat Sheet ?
Complete CSS Grid Tutorial With Cheat Sheet ?
grid-template-columns
grid-template-rows
grid-template-areas
grid-column : start/end
grid-row : start/end
grid-area Forum Donate
justify-self || align-self
Learn to code — free 3,000-hour curriculum
Conclusion
Desktop View
Mobile View
Forum Donate
grid architecture
By the way, you can join multiple rows and columns, just like in
Excel software, which gives you more flexibility and options than
Flexbox.
This chart contains every possible property you can use when
using grid. Grid properties are divided into:
Parent properties Forum Donate
Child Properties
Learn to code — free 3,000-hour curriculum
For this project, you need to know little bit of HTML, CSS, and
how to work with VS code. Follow along with me as we complete
the following tasks:
At the end of this tutorial, you will be able to make accurate and
beautiful website layouts.
HTML
Create three boxes inside the body tag, like this 👇Forum Donate
<div class="container">
<div class="box-1"> A </div>
<div class="box-2"> B </div>
<div class="box-3"> C </div>
</div>
CSS
Step 1
Let's clear out our default browser styles. Follow me 👇
*{
margin: 0px;
padding: 0px;
box-sizing: border-box;
}
Step 2
Inside the body selector, do these adjustments:
body {
font-family: sans-serif;
font-size: 40px;
width: 100%;
min-height: 100vh;
}
Step 3
Now, let's style our boxes by selecting all of them together ->
[class^="box-"] {
Forum Donate
background-color: skyblue;
Learn to code — free 3,000-hour curriculum
/* To place the letter at the center */
display: grid;
place-items: center;
}
Step 4
Now, place some gaps between our boxes like this 👇
.container {
display: grid;
gap: 20px;
}
But Wait....
For the Grid parent property, we will write inside the .container
class. For the Grid child property, we will write in the .box-*
classes.
The grid-template-columns
property
You use this property to define the number and width of columns.
You can either individually set the width of each column, or set a
uniform width for all columns using the repeat() function.
Forum Donate
grid-template-columns
.container {
display: grid;
gap: 20px;
Note:
grid-template-rows
To test these results, write the following CSS code Forum
-> Donate
.container {
display: grid;
gap: 20px;
height: 100vh;
The blueprint
grid-template-areas:
"A A A A A A A A A A A A"
"B B B B B B B B B B C C"
"B B B B B B B B B B C C";
}
.box-1{
grid-area: A;
}
.box-2{
grid-area: B;
}
.box-3{
grid-area: C;
}
Note: I'll discuss the grid-area property again in the grid child
property section.
row gap
justify-items
If you want to test this, then add 1 more box inside the HTML ->
<div class="container">
.container {
display: grid;
gap: 50px;
height: 100vh;
Learn to code
You use this property — free 3,000-hour
to position grid-itemscurriculum
(children) inside the
grid container along the Y-Axis [Cross Axis]. The 4 values are 👇
align-items
Don't change anything in the HTML, but in the CSS write ->
.container {
display: grid;
gap: 50px;
height: 100vh;
justify-content
Don't change anything in the HTML, but in the CSS write ->
Forum Donate
.container {
Learn to code — free 3,000-hour curriculum
display: grid;
gap: 50px;
height: 100vh;
align-content
Don't change anything in the HTML, but in the CSS write ->
.container {
display: grid;
gap: 50px;
height: 100vh;
The illustration below 👇 shows the start and end points of rows
and columns of a single cell.
Forum Donate
HTML
To experiment with the grid scale and understand the following
properties, make 4 boxes inside body tag:
<div class="container">
<div class="box-1"> A </div>
<div class="box-2"> B </div>
<div class="box-3"> C </div>
<div class="box-4"> D </div>
</div>
A Quick Note
When we use the fr [ Fraction ] unit, we are dividingForum
the screen Donate
area into equal proportions.
Learn to code — free 3,000-hour curriculum
grid-column-start
grid-column-end
.container {
display: grid;
gap: 20px;
height: 100vh;
Here, we are dividing our screen [ both width and height ] into 12
equal parts. 1 box is occupying 1 part, or you can call it 1fr [ 1
fraction ]. The remaining 8 fractions along the width are empty. Donate
Forum
Learn to code — free 3,000-hour curriculum
As we are dealing with child properties, we need to target our
.box-* classes like this:
.box-1{}
.box-2{}
.box-3{}
.box-4{}
You can experiment with any or all of these boxes, I'll stick with
.box-1 .
Let's bring our grid scale. We are dealing with columns – just
focus on the columns, not rows.
grid-column-start : 1;
grid-column-end : 2;
.box-1{
grid-column : 1/10
}
A quick note
.box-1{
grid-column : span 9;
}
grid-row-start
grid-row-end
Let's experiment with it! Here, I'll stick with .box-1, and here's our
grid guide. Now, only focus on the row scale, not the column.
The calculation looks like this -> .box-1 holds 1 box, and you add
9 more boxes, plus a mandatory 1 at the last position to indicate
the end which gives you 9+1+1=11.
grid-template-areas:
"A A A A A A A A A A A A"
"B B B B B B B B B B C C"
"B B B B B B B B B B C C";
}
Forum Donate
Justify-self
Remember! This only works on the child classes. So, target any
.box-* class. I'll target the first box:
.box-1 {
/* Change values 👇 to experiment */
justify-self : start;
}
align-self
.box-1 {
/* Change values 👇
to experiment */
align-self : start;
}
place-content
place-items
place-self
grid-template
gap / grid-gap
place-content
place-content
align-content
justify-content
An example Forum Donate
/* The shorthand */
place-content : center / end ;
place-items
place-items
align-items
justify-items
An example
align-items : end;
justify-items : center;
/* The shorthand */
place-items : end / center ;
place-self
place-self
An example
align-self : start ;
justify-self : end ;
/* The shorthand */
place-self : start / end ;
grid-template
grid-template
grid-template-rows
grid-template-columns
An example
/* The shorthand */
grid-template : 100px 100px / 200px 200px;
gap/grid-gap
Gap and grid-gap are the same thing and do the same work. You
can use either of them.
gap
Forum Donate
row-gap
column-gap
An example
row-gap : 20px ;
column-gap : 30px ;
/* The shorthand */
gap : 20px 30px;
Credits
Unicorns
Corgis, kat
Conclusion
Now, you can confidently make responsive website layouts using
your grid knowledge!
Instagram / JoyShaheb
Joy Shaheb
Joy is an experienced Senior Frontend Developer and a mentor based in Dhaka,
Bangladesh. He is highly enthusiastic about javascript & He is on a mission to
create the best web dev tutorials online
If you read this far, tweet to the author to show them you care.
Tweet a thanks
Our mission: to help people learn to code for free. We accomplish this by creating
thousands of videos, articles, and interactive coding lessons - all freely available to the
public. We also have thousands of freeCodeCamp study groups around
Forumthe world. Donate
Trending Guides
JS Substring Tutorial UX vs UI
Copyright Policy