The Wayback Machine - https://web.archive.org/web/20250319092126/https://www.geeksforgeeks.org/css-grid-template-property/
Open In App

CSS grid-template Property

Last Updated : 27 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The grid-template property in CSS is a shorthand property for defining grid columns, rows, and areas. It allows you to set values for the following longhand properties: grid-template-rows, grid-template-columns, and grid-template-areas.

Syntax

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

Property Values: 

1. none:

Resets the sizing of rows and columns to their default values.

Example:

html
<!DOCTYPE html>
<html>

<head>
    <title>
        CSS | grid-template Property
    </title>
    <style>
        .main {
            display: grid;
            grid-template: none;
            grid-gap: 10px;
            background-color: green;
            padding: 10px;
        }
        
        .main > div {
            background-color: white;
            text-align: center;
            padding: 15px 0;
            font-size: 30px;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="item1">G</div>
        <div class="item2">E</div>
        <div class="item3">E</div>
        <div class="item4">K</div>
    </div>
</body>

</html>


Output: 
 

2. grid-template-rows/grid-template-columns:

This property value is used to specify size of rows and columns measured in px, cm etc. If the user wants the row or column size to remain default then set that row or column to “auto”.

Example: 

html
<!DOCTYPE html>
<html>

<head>
    <title>
        CSS | grid-template Property
    </title>
    <style>
        .main {
            display: grid;
            grid-template: 50px 100px/150px auto;
            grid-gap: 10px;
            background-color: green;
            padding: 10px;
        }
        
        .main > div {
            background-color: white;
            text-align: center;
            padding: 15px 0;
            font-size: 30px;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="item1">G</div>
        <div class="item2">E</div>
        <div class="item3">E</div>
        <div class="item4">K</div>
    </div>
</body>

</html>


Output: 

3. grid-template-areas:

This property value specifies areas within the grid layout. grid-area property is used to name the grid items and then reference them using grid-template-areas.

Example: 

html
<!DOCTYPE html>
<html>

<head>
    <title>
        CSS | grid-template Property
    </title>
    <style>
        .item1 {
            grid-area: item1;
        }
        
        .item2 {
            grid-area: item2;
        }
        
        .item3 {
            grid-area: item3;
        }
        
        .item4 {
            grid-area: item4;
        }
        
        .main {
            display: grid;
            grid-template: 'item1 item1 item1' 
                           'item2 item3 item4';
            grid-gap: 10px;
            background-color: green;
            padding: 10px;
        }
        
        .main > div {
            background-color: white;
            text-align: center;
            padding: 15px 0;
            font-size: 30px;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="item1">G</div>
        <div class="item2">E</div>
        <div class="item3">E</div>
        <div class="item4">K</div>
    </div>
</body>

</html>


Output: 

4. initial:

This property value will set the property to its default value.

Example: 

html
<!DOCTYPE html>
<html>

<head>
    <title>
        CSS | grid-template Property
    </title>
    <style>
        .main {
            display: grid;
            grid-template: initial;
            alignnone grid-gap: 10px;
            background-color: green;
            padding: 10px;
        }
        
        .main > div {
            background-color: white;
            text-align: center;
            padding: 15px 0;
            font-size: 30px;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="item1">G</div>
        <div class="item2">E</div>
        <div class="item3">E</div>
        <div class="item4">K</div>
    </div>
</body>

</html>

Output: 
 

5. inherit:

This will inherit this property from its parent element.

Example: 

html
<!DOCTYPE html>
<html>

<head>
    <title>
        CSS | grid-template Property
    </title>
    <style>
        .main {
            display: grid;
            grid-template: inherit;
            grid-gap: 10px;
            background-color: green;
            padding: 10px;
        }
        
        .main > div {
            background-color: white;
            text-align: center;
            padding: 15px 0;
            font-size: 30px;
        }
        
        alignnone
    </style>
</head>

<body>
    <div class="main">
        <div class="item1">G</div>
        <div class="item2">E</div>
        <div class="item3">E</div>
        <div class="item4">K</div>
    </div>
</body>

</html>

Output: 
 

The grid-template property is a powerful shorthand tool for defining the structure and layout of CSS grids. By combining grid-template-rows, grid-template-columns, and grid-template-areas, you can create complex and responsive grid layouts with ease.

Supported Browsers: The browser supported by CSS grid-template Property are listed below: 

  • Google Chrome 57.0
  • Edge 16
  • Firefox 52.0
  • Safari 10.1
  • Opera 44.0

CSS grid-template Property – FAQs

What does the grid-template property do in CSS?

The grid-template property is a shorthand property that allows you to define both the grid-template-rows and grid-template-columns, and optionally the grid-template-areas. It helps define the structure and layout of the grid container.

How do I use grid-template to define both rows and columns?

You can define rows and columns using grid-template like this: grid-template: 100px 200px / 1fr 2fr 1fr; which creates a grid with two rows and three columns.

What’s the difference between grid-template and grid-template-areas?

Grid-template is a broader property that can include both row and column definitions, as well as areas, while grid-template-areas specifically defines named areas for item placement.

Can grid-template be used for responsive design?

Yes, grid-template supports responsive units like percentages and fr units, making it adaptable for different screen sizes and layouts.

How does grid-template work with implicit grid tracks?

If the content exceeds the explicitly defined rows and columns, implicit grid tracks will be automatically generated based on the settings of grid-auto-rows and grid-auto-columns.



Next Article

Similar Reads

three90RightbarBannerImg