Set areas within the grid layout in CSS

To set the areas within the grid layout, use the grid-template-areas Property. With that, first set the display property as grid to create a grid layout. You need to work on the grid-area, grid-gap, and grid-template-areas property. Let us understand them one by one and set areas withing the grid layout using HTML and CSS.

The grid-template-areas property

Use the grid-template-areas property to set areas within the grid layout −

grid-template-areas:
   'newArea newArea . . .'
   'newArea newArea . . .';

The grid-gap property

To set the gap between the rows and columns in a grid layout, use the grid-gap property. Use it as a shorthand or use the grid-row-gap and grid-column-gap separately −

grid-gap: 5px;

The grid-area property

Use the grid-area property to set the grid item's size and location in a grid layout. Use it as a shorthand or use the grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties directly.

Assign a name to a grid-item

To assign a name to a grid item, use the grid-area property. For example −

grid-area: newArea;

Set the grid container

Set a container for the grid. Within that, all the child divs gets added −

<div class="grid-container">
   <div class="demo">250</div>
   <div class="item2">120</div>
   <div class="item3">333</div>
   <div class="item4">298</div> 
   <div class="item5">645</div>
   <div class="item6">543</div>
   <div class="item7">555</div>
</div>

Set the Grid Area

The grid name is set using the grid-area property −

.demo {
   grid-area: newArea;
}

Create a grid

A grid gets created when the display property is set to grid. An element becomes a grid container when its display is set to grid −

.grid-container {
   display: grid;
   grid-template-areas:
   'newArea newArea . . .'
   'newArea newArea . . .';
   grid-gap: 5px;
   background-color: blue;
   padding: 5px;
}

Set areas within the grid layout

The areas within the grid layout is set using the grid-template-areas property −

grid-template-areas:
   'newArea newArea . . .'
   'newArea newArea . . .';

Example

Let us now see an example −

<!DOCTYPE html>
<html>
<head>
   <style>
      .demo {
         grid-area: newArea;
      }
      .grid-container {
         display: grid;
         grid-template-areas:
         'newArea newArea . . .'
         'newArea newArea . . .';
         grid-gap: 5px;
         background-color: blue;
         padding: 5px;
      }
      .grid-container > div {
         background-color: orange;
         text-align: center;
         padding: 5px 0;
         font-size: 30px;
      }
   </style>
</head>
<body>
   <h1>Heading One</h1>
   <p>Set some random numbers</p>
   <div class="grid-container">
      <div class="demo">250</div>
      <div class="item2">120</div>
      <div class="item3">333</div>
      <div class="item4">298</div> 
      <div class="item5">645</div>
      <div class="item6">543</div>
      <div class="item7">555</div>
   </div>
</body>
</html>
Updated on: 2023-12-26T15:44:43+05:30

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements