CSS Grid: Divide the Grid Into an Area Template

You can group cells of your grid together into an area and give the area a custom name. Do this by using grid-template-areas on the container like this:
grid-template-areas:
"header header header"
"advert content content"
"footer footer footer";
The code above merges the top three cells together into an area named header, the bottom three cells into a footer area, and it makes two areas in the middle row; advert and content.


Note
Every word in the code represents a cell and every pair of quotation marks represent a row.
In addition to custom labels, you can use a period (.) to designate an empty cell in the grid.

here is our grid
1
2
3
4
5
html code to name that grid

<style>
  .item1{background:LightSkyBlue;}
  .item2{background:LightSalmon;}
  .item3{background:PaleTurquoise;}
  .item4{background:LightPink;}  
  .item5 {background: PaleGreen;}
  
  .container {
    font-size: 40px;
    min-height: 300px;
    width: 100%;
    background: LightGray;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    grid-gap: 10px;
    grid-template-areas: 
      "header header header"
      "advert content content"
      "footer footer footer";
  }
</style>
  
<div class="container">
  <div class="item1">1</div>
  <div class="item2">2</div>
  <div class="item3">3</div>
  <div class="item4">4</div>
  <div class="item5">5</div>
</div>

Place Items in Grid Areas Using the grid-area Property

After creating an areas template for your grid container, 
as shown you can place an item in your custom area by referencing the name you gave it. 
To do this, you use the grid-area property on an item like this:
.item1 { grid-area: header; }
item1 is grid cell inside grid container div
This lets the grid know that you want the item1 class to go in the area named header. In this case, the item will use the entire top row because that whole row is named as the header area.

1
2
3
4
5


here .item1 take all space in the container as it take grid-area:header
html code

<style>
  .item1{
   background:LightSkyBlue;
   grid-area:header;
 }
  .item2{background:LightSalmon;}
  .item3{background:PaleTurquoise;}
  .item4{background:LightPink;}
  .item5 {background: PaleGreen; }
  
  .container {
    font-size: 40px;
    min-height: 300px;
    width: 100%;
    background: LightGray;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr 1fr;
    grid-gap: 10px;
    grid-template-areas: 
      "header header header"
      "advert content content"
      "footer footer footer";
  }
</style>
  
<div class="container">
  <div class="item1">1</div>
  <div class="item2">2</div>
  <div class="item3">3</div>
  <div class="item4">4</div>
  <div class="item5">5</div>
</div>

another replace to header is item1 { grid-area: 1/1/2/4; } it will take
the first horizontal lines and between the second and fourth vertical lines.

grid-area explanation
grid-area: horizontal line to start at / vertical line to start at / horizontal line to end at / vertical line to end at.
source

Comments

Popular posts from this blog

ES6 : 5 Prevent Object Mutation

ES6: 1 Explore Differences Between the var and let Keywords

CSS Grid: Reduce Repetition Using the repeat Function