So, what’s up dude! We have come again with a new master class that is CSS grid layout tutorial.
Here, we will guide you to use CSS grid for responsive web designs. So, stay with us for the next 7 minutes.
First of all, the grid means 2-dimensional layout which can handle both rows and columns simultaneously. So, CSS grid layout is two-dimensional layout whereas flexbox used single-dimensional layout.
By the way, we have published a complete guide about CSS flexbox layout, here.
You can read about browser support on caniuse.com, it provides the browser support information like below,
OK, let’s understand some basic terminology of CSS grids. These terms are very common and going to be used again and again during this CSS grids tutorial.
CSS grid rows are the same as in the table or spreadsheet if you are familiar with that. Basically, rows are the horizontal lines in the grid, left-to-right.
CSS grid columns are the vertical lines in the grid, top-to-bottom. As in the below example, vertical lines are grid columns.
With CSS grid, most of the time we want some space between rows and columns to manage the clean look of the layout. It is known as grid-gap or gutter.
.container { display: grid; grid-template-columns: 1fr 1fr; grid-gap: 10px; }
CSS grid lines are those lines who draw the basic layout of the page. Like, row lines which draws horizontally between the grid rows and column lines which draws vertically between grid columns.
CSS grid cell is a basic block of grid. We can understand it as a single unit of grid. Like rows and columns joint together to create a cell and collection of cells is known as grid.
CSS grid area is a collection of one or more than one cell. We will basically use grid area to define the portion of one template like header, sidebar, footer etc. We will understand it in detail later.
CSS grid tracks are simply rows and columns. Like total cells of one column is a single track and total cells of one row is also a track. We will use tracks to define the width and height of rows and columns.
The fr is a fractional unit which means a flexible unit. It is basically used in CSS grids. Actually, fr uses the part of available space, like if there are three columns with 1fr unit then all three columns will take equal space out of available free space.
.grid { display: grid; grid-gap: 10px; grid-template-columns: 1fr 2fr; }
Let’s assume about the grid which is 500px wide and 10px of grid-gap or gutter. So, we have 490px for width. As we have defined the grid-template-columns 1fr+2fr = 3fr. Now divide the 490px into 3 parts.
Now 1fr = 163.333px because 163.333 X 3 = 490 that’s it. It means 2fr = 326.667px.
Hope you got the idea about the fr unit.
When we define the CSS grid rows and columns via grid-template-column and grid-template-rows, it is known as the explicit grid.
So, what is implicit grid?
Let’s understand with the following example,
.grid-container { display: grid; grid-gap: 10px; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 2fr; } .item { border: 1px solid #008000; padding: 20px; }
Here, we have defined eight grid items where we have used two columns with grid-template-column and two rows with grid-template-rows. Other than this we have two more rows which are automatically created because of extra grid items. These are called the implicit grid.
Implicit grid items take the size according to their content, means these are auto-sized.
Firefox grid inspector is a very useful tool to work with CSS grids because here we can inspect all aspect of the grid with visuals like grid lines, line numbers, area names etc.
There are two main HTML elements used to work with CSS grid, grid container element (parent) and grid item element (child).
It is a wrapper element which wrap its items into a single box. To create a container element, we need to defined the property display to grid.
.container { display: grid; }
Note: A column controls the width of the cell and A row controls the height of the cell.
Grid items are direct child of grid container not nested children, only direct children element. When we defined an element with display grid, it becomes a grid container and its direct child immediately become grid items.
.container { display: grid; } .item { padding: 20px; border: 1px solid; }
<div class="container"> <div class="item">item 1</div> <div class="item">item 2</div> <div class="item">item 3</div> <div class="item">item 4</div> </div>
The first property of CSS grid container is a display property that convert an element into the grid container. We can use grid or inline-grid for display property.
CSS Grid is a block-level element and inline-grid is an inline-level element, which allows other elements to take place next to it.
This property defines the number of columns in the grid. With the help of grid-template-columns, we defined the width of the columns. For example,
.container { display: grid; grid-template-columns: 1fr 2fr; }
As we can see in the above example, the space-separated list of values is used to define the number of columns and the width of each column.
Here we can use any kind of CSS unit like %, rem, em, px, etc. to define the value of this property. Even we can use the fr unit which is enough flexible to adapt the window size or device size.
The grid-template-rows defines the number of rows in the grid. Here we use this property to define the height of the rows and a total number of rows. For example,
.container { display: grid; grid-template-rows: 1fr 2fr; }
The above example shows the space-separated list of values. These values are used to defined the number of rows and height of individual row.
We can use fr unit, auto, %, rem, em, px, etc. to define the height of each row.
This function can help us to define the grid-template-columns when all columns need to be same width. Same way, we can also use this function for grid-template-rows when all rows need to be same height.
To understand it more clearly, let’s jump over the example,
.container{ display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(2, 1fr); }
In this example we have defined the 3 columns with width 1fr and 2 rows with height 1fr.
Justify-content is used to align the grid items, as per available space inside the grid container.
justify-content: start | end | center | space-around | space-evenly | space-between
Align-content property is used to align the all grid items vertically inside the grid container. Following are the possible values of align-content property,
align-content: start | end | center | space-around | space-evenly | space-between
As we have discussed, grid items are direct child elements of the grid container. By default, it creates a single column and single or multiple rows as per the total number of grid items.
As we know this is grid-item based property. So, we can use it to place the item in the single or multiple columns.
The grid-column property is a shorthand property of grid-column-start and grid-column-end.
For example,
.item1 { grid-column: 1/4; }
.item1 { grid-column: 1/ span 4; }
As the above example, we have defined the start column line and end column line to place the item.
It is a shorthand property of grid-row-start and grid-row-end. It also behaves as grid-column does. The grid-row property is used to place the item inside the row. For example,
.item2 { grid-row: 1/ 4; }
.item2 { grid-row: 1/ span 4; }
Here we have defined the start row line and end row line to place the item.
When we use grid-row and grid-column property together it becomes a grid-area. So, grid-area is also a shorthand property for grid-row and grid-column property.
This property is used to placing the grid items as per their area name inside the grid layout.
.grid { display: grid; grid-template-rows: 1fr 0.5fr 2fr 1fr; grid-template-areas: 'header header' 'navbar navbar' 'sidebar content' 'sidebar content' 'footer footer'; } .item { border: 1px solid #008000; padding: 20px; } .item1 { grid-area: header; } .item2 { grid-area: navbar; } .item3 { grid-area: content; } .item4 { grid-area: sidebar; } .item5 { grid-area: footer; }
<div class="grid"> <div class="item item1"> item 1</div> <div class="item item2"> item 2</div> <div class="item item3"> item 3</div> <div class="item item4"> item 4</div> <div class="item item5"> item 5</div> </div>
If you like this CSS grids tutorial, please share it with your friends.
We have found some interesting learning material on the web, So you can check-out that also.
If you have understood the basics of CSS grid layout then this is for you a detailed and complete CSS grid layout guide by CSS-tricks.
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…