CSS Grid Layout lets you create complex responsive web design layouts quickly and consistently across browsers.
Let's take a complete look π§΅ππ»
Grid is used for making complex web design layouts more easily as it's not so hard to master
Using Flex you can make only 1D layout but Grid gives you the full power of creating a 2D layout
Let's start
{ 02 / 28 }
First things first, start with giving the display property "grid" to the container element or parent element.
{ 03 / 28 }
Nothing will change after adding display: flex; in the parent container because we need to define the width of columns. In order to set that column's width we have gird-template-columns property.
{ 04 / 28 }
Let's start with defining the width of our columns.
For example, let's say I need two columns of width 60% and 40% respectively
grid-template-columns: 60% 40%;
{ 05 / 28 }
Ahh!! My grid items look ugly as there is no spacing between them.
Here "grid-gap" property comes into play. For example, I need 10px spacing along column and row
grid-gap: 10px;
{ 06 / 28 }
Similarly we have grid-template-rows.
It is used to define the number of rows and height of rows.
grid-template-rows: 200px 400px;
{ 07 / 28 }
As you can see there is a lot of repeated code in
grid-template-columns: 200px 200px 200px 200px 200px;
Instead of this, we can use the repeat function ππ»
grid-template-columns: repeat(5, 200px);
{ 08 / 28 }
You might run into some responsiveness issues if you pass pixel unit or percentage in your grid-template-columns
In order to prevent this, it is recommended to use fraction values
For example:
{ 09 / 28 }
You can use repeat function for fr as well
repeat(2, 1fr 2fr);
It will repeat 1fr 2fr two times.
{ 10 / 28 }
Alright moving forward, you can set the height of the grid element using grid-auto-rows
For ex, grid-auto-rows: 200px;
{ 11 / 28 }
Though there is a problem. By doing this, we are setting the fixed height so content inside items can be overflow.
For example:
{ 12 / 28 }
In order to prevent this kind of issues we have minmax function
grid-auto-rows: minmax(200px, auto);
It's pretty intuitive that the height of gird items will be 200px minimum and "auto" maximum(according to content)
{ 13 / 28 }
Well, all that we have covered so far we can do that using flexbox also.
Let's understand the 2 dimensions of grid layout
{ 14 / 28 }
We can change the position of a particular item in accordance of row and column
For example, I want my first item to take up entire row, that is from the first column to the last column
grid-column: 1 / 4;
{ 15 / 28 }
Alright moving forward, The next property we have is `grid-template-areas` which specifies the areas within the grid layout.
- Each row is defined by apostrophes (' ')
- Hence only one row in this case as there is only one pair of apostrophes
- Five "areaName" hence five columns
CONT...
{ 17 / 28 }
Now let's apply this area (areaName) to the first grid item
.one {
grid-area: areaName;
}
As you can see item1 takes one entire row and 5 columns. As simple as that
React is a JavaScript library for building UI components. The ecosystem of React is really immense which eventually makes it one of the best front-end libraries.
π§΅ππ»
Why React?
1. Reusable components 2. Fast due to virtual DOM 3. Huge ecosystem
A typical React app contain many components. They are reusable and can interact with each other.
What is a component?
- Component as a simple function that you can call with some input and they render some output