CSS - grid-template Property



CSS grid-template property specifies the grid columns, grid rows and grid areas for the grid layout. The property is a shorthand for the individual grid-related properties: grid-template-areas, grid-template-columns and grid-template-rows

Syntax

grid-template: none | grid-template-rows / grid-template-columns | grid-template-areas | initial | inherit;

Property Values

Value Description
none No specific size for the columns and rows is set. Default.
grid-template-rows / grid-template-columns It specifies the size(s) of the rows and columns.
grid-template-area It defines the grid layout using named grid items.
initial This sets the property to its default value.
inherit This inherits the property from the parent element.

Examples of CSS Grid Template Property

The following examples explain the grid-template property with different values.

Grid Template Property with None Value

To not have any predefined rows, columns, or areas, we use the none value which indicates that no explicit grid template is defined. The grid will automatically adjust based on the content and layout of the grid items. This is shown in the following example.

Example

<!DOCTYPE html>
<html>

<head>
   <style>
      .container {
         display: grid;
         background-color: lightgrey;
         grid-gap: 10px;
         padding: 20px;
         grid-template: none;
      }

      .container>div {
         padding: 10px;
         text-align: center;
         color: white;
         border: 3px solid blue;
         background-color: #4775d1;

      }
   </style>
</head>

<body>
   <h2>
      CSS grid-template property
   </h2>
   <h4>
      grid-template: none
   </h4>
   <div class=" container container1">
      <div>
         Item-1
      </div>
      <div>
         Item-2
      </div>
      <div>
         Item-3
      </div>
      <div>
         Item-4
      </div>
   </div>

</body>

</html>

Grid Template Property for Rows and Columns

To define the sizes of the rows and columns in the grid layout, we specify the height of the rows and the width of the columns separated by a '/' to the grid-template property. The number of sizes specified before the '/' indicate the number of rows and the number of sizes specified after the '/' indicate the number of columns. These are shown in the following example.

Example

<!DOCTYPE html>
<html>

<head>
   <style>
      .container {
         display: grid;
         height: 200px;
         background-color: lightgrey;
         grid-gap: 20px;
         padding: 10px;
      }

      .container1 {
         grid-template: 120px 60px / auto auto auto;
      }

      .container2 {
         grid-template: 100px 60px / 160px 130px 160px;
      }

      .container>div {
         padding: 10px;
         text-align: center;
         color: white;
         border: 3px solid blue;
         background-color: #4775d1;

      }
   </style>
</head>

<body>
   <h2>
      CSS grid-template property
   </h2>
   <p>
      <strong>
         grid-template: 120px 60px / auto
         auto auto (2 rows and three columns)
      </strong>
      ; the first row is 120px high and second row
      is 60px high, the width of the columns has
      been set to auto and thus depends on the
      content.
   </p>
   <div class=" container container1">
      <div>
         Item-1
      </div>
      <div>
         Item-2
      </div>
      <div>
         Item-3
      </div>
      <div>
         Item-4
      </div>
      <div>
         Item-5
      </div>
      <div>
         Item-6
      </div>
   </div>
   <p>
      <strong>
         grid-template: 100px 60px / 160px
         130px 160px (2 rows and three columns)
      </strong>
      ; the first row is 100px high and second row
      is 60px high, the first column is 160px wide,
      second column is 130px wide, third column is
      160px wide.
   </p>
   <div class="container container2">
      <div>
         Item-1
      </div>
      <div>
         Item-2
      </div>
      <div>
         Item-3
      </div>
      <div>
         Item-4
      </div>
      <div>
         Item-5
      </div>
      <div>
         Item-6
      </div>
   </div>
</body>

</html>

Grid Template Property with Named Grid Items

To define named grid areas within the grid container, we specify the rows using apostrophes '' and the columns with the number of names inside them to the grid-template property. An element needs to use the name inside the apostrophes to refer to the portion of the layout. This is shown in the following example.

Example

<!DOCTYPE html>
<html>

<head>
   <style>
      .container {
         display: grid;
         background-color: lightgrey;
         grid-gap: 10px;
         padding: 20px;
      }

      .container>div {
         padding: 20px;
         text-align: center;
         color: white;
         border: 3px solid blue;
         background-color: #4775d1;
      }

      .container1 {
         grid-template: 
         'header header header header'
         'sidebar content content content'
         'footer footer footer footer';
      }

      .container2 {
         grid-template: 
         'item item ..'
         'item item..';
      }

      .cont1-item1 {
         grid-area: header;
      }

      .cont1-item2 {
         grid-area: sidebar;
      }

      .cont1-item3 {
         grid-area: content;
      }

      .cont1-item4 {
         grid-area: footer;
      }

      .cont2-item1 {
         grid-area: item;
      }
   </style>
</head>

<body>
   <h2>
      CSS grid-template property
   </h2>
   <p>
      <strong>
         grid-template: 'header header header header'
         'sidebar content content content' 
         'footer footer footer footer'
      </strong>
      ; The layout has three rows indicated by three different
      apostrophes' ' and four columns indicated by the number
      of items inside apostrophes ' '
   </p>
   <div class=" container container1">
      <div class="cont1-item1">
         This is header
      </div>
      <div class="cont1-item2">
         This is sidebar
      </div>
      <div class="cont1-item3">
         This is content
      </div>
      <div class="cont1-item4">
         This is footer
      </div>
   </div>
   <p>
      <strong>
         grid-template: 'item item . .''item item . .'
      </strong>
      ; item1 takes two rows indicated by two separate
      apostrophes ' ' and 4 columns indicated by the 
      number of items inside apostrophes ' 
   '</p>
   <div class=" container container2">
      <div class="cont2-item1">
         Item-1
      </div>
      <div>
         Item-2
      </div>
      <div>
         Item-3
      </div>
      <div>
         Item-4
      </div>
      <div>
         Item-5
      </div>
      <div>
         Item-6
      </div>
   </div>

</body>

</html>

Supported Browsers

Property Chrome Edge Firefox Safari Opera
grid-template 57 16 52 10 44
css_reference.htm
Advertisements