CSS - Grid template



Description

The grid-template property is a shorthand property that allows you to define both the rows and columns of the grid in a single declaration.

Example

Here is an example where the value of grid-template property is set to 100px / auto auto auto

You can edit and try running this code using Edit & Run option.

<!DOCTYPE html>
<html>
<head>
    <title>CSS - grid-template</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style type="text/css">
       .grid-container {
            display: grid;
            grid-template: 100px / auto auto auto;
            color: white;
            text-align: center;
            width: 360px;
            border: 2px solid rgb(29, 231, 80);
            background-color: rgb(29, 231, 80);
        }
        .grid-container > div {
            background-color: rgb(228, 9, 9);
            border: 2px solid rgb(29, 231, 80);
            padding: 10px;
        }
    </style>
</head>
<body>
    <h4>The grid layout has 3 columns and and the height of the first row is set to 100px.</h4>
    <div class="grid-container">
        <div class="grid-item1">Grid item 1</div>
        <div class="grid-item2">Grid item 2</div>
        <div class="grid-item3">Grid item 3</div>
        <div class="grid-item4">Grid item 4</div>
        <div class="grid-item5">Grid item 5</div>
        <div class="grid-item6">Grid item 6</div>
        <div class="grid-item7">Grid item 7</div>
        <div class="grid-item8">Grid item 8</div>
        <div class="grid-item9">Grid item 9<div>
    </div>
</body>
</html>

The grid-template-areas property accepts a grid template string that represents the layout of the grid in rows and columns. every name within the template string represents a specific named grid area that a grid item will fill.

Here is an example where the value of grid-template-areas property is set with name item1Area

You can edit and try running this code using Edit & Run option.

<!DOCTYPE html>
<html>
<head>
    <title>CSS - grid-template-areas</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style type="text/css">
        .grid-container {
            display: grid;
            grid-template-areas: 'item1Area item1Area item1Area item1Area';
            color: white;
            text-align: center;
            width: 360px;
            border: 2px solid rgb(29, 231, 80);
            background-color: rgb(29, 231, 80);
        } 
        .grid-container > div {
            background-color: rgb(228, 9, 9);
            border: 2px solid rgb(29, 231, 80);
            padding: 10px;
        }
        .grid-item1 {
            grid-area: item1Area;
        }
    </style>
</head>
<body>
    <h4>The item1Area is Grid item 1, it will span across all four columns.</h4>
    <div class="grid-container">
        <div class="grid-item1">Grid item 1</div>
        <div class="grid-item2">Grid item 2</div>
        <div class="grid-item3">Grid item 3</div>
        <div class="grid-item4">Grid item 4</div>
        <div class="grid-item5">Grid item 5</div>
        <div class="grid-item6">Grid item 6</div>
        <div class="grid-item7">Grid item 7</div>
        <div class="grid-item8">Grid item 8</div>
        <div class="grid-item9">Grid item 9</div>
    </div>
</body>
</html>

The grid-template-columns property is used to define the size and number of columns in a grid container.

Here is an example where the value of grid-template-columns property is set to auto auto auto

You can edit and try running this code using Edit & Run option.

<!DOCTYPE html>
<html>
<head>
    <title>CSS - grid-template-columns</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style type="text/css">
        .grid-container {
            display: grid;
            grid-template-columns: auto auto auto;
            color: white;
            text-align: center;
            width: 360px;
            border: 2px solid rgb(29, 231, 80);
            background-color: rgb(29, 231, 80);
        }
        .grid-container > div {
            background-color: rgb(228, 9, 9);
            border: 2px solid rgb(29, 231, 80);
            padding: 10px;
        }     
    </style>
</head>
<body>
    <h3>The grid layout has 3 columns.</h3>
    <div class="grid-container">
        <div class="grid-item1">Grid item 1</div>
        <div class="grid-item2">Grid item 2</div>
        <div class="grid-item3">Grid item 3</div>
        <div class="grid-item4">Grid item 4</div>
        <div class="grid-item5">Grid item 5</div>
        <div class="grid-item6">Grid item 6</div>
    </div>
</body>
</html>

The grid-template-rows property is used to define the size and number of rows in a grid container.

Here is an example where the value of grid-template-row property is set to 120px 160px

You can edit and try running this code using Edit & Run option.

<!DOCTYPE html>
<html>
<head>
    <title>CSS - grid-template-row</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <style type="text/css">
       .grid-container {
            display: grid;
            grid-template-columns: auto auto auto;
            grid-template-rows: 80px 100px;
            color: white;
            text-align: center;
            width: 360px;
            border: 2px solid rgb(29, 231, 80);
            background-color: rgb(29, 231, 80);
        }
        .grid-container > div {
            background-color: rgb(228, 9, 9);
            border: 2px solid rgb(29, 231, 80);
            padding: 10px;
        }     
    </style>
</head>
<body>
    <h3>The grid layout consists of first row with the height of 80px and second row with height of 100px.</h3>
    <div class="grid-container">
        <div class="grid-item1">Grid item 1</div>
        <div class="grid-item2">Grid item 2</div>
        <div class="grid-item3">Grid item 3</div>
        <div class="grid-item4">Grid item 4</div>
        <div class="grid-item5">Grid item 5</div>
        <div class="grid-item6">Grid item 6</div>
        <div class="grid-item7">Grid item 7</div>
        <div class="grid-item8">Grid item 8</div>
    </div>
</body>
</html>
Advertisements