Open In App

CSS grid-auto-rows Property

Last Updated : 27 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The grid-auto-rows property in CSS is used to specify the size for the rows of implicitly generated grid containers. 

Syntax

grid-auto-rows: auto |max-content | min-content | length | 
percentage | minmax(min, max) | initial | inherit;

Property Values

Property Value

Description

auto

This is the default value. The size is determined implicitly according to the size of the container. 

length

It is used to set the grid-auto-rows property to the given length. The length value can not be negative. 

percentage

It sets the grid-auto-rows property to the percentage value. 

max-content

Specifies the size depending on the largest item in the container.

min-content

Specifies the size depending on the smallest item in the container.

minmax(min, max)

Specifies the size in the range of [min, max]. greater than or equal to min and less than or equal to max.

initial

Initializes the value with its default value. 

inherit

Inherits the value from its parent element. 

Example 1: Using grid-auto-rows: auto

In this example we creates a CSS grid with automatically sized rows. The grid has two columns in the first row and one column in subsequent rows. Each grid item has a white background.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>
        CSS grid-auto-rows Property
    </title>

    <style>
        .main {
            display: grid;
            grid-template-areas: "a a";
            grid-gap: 20px;
            padding: 30px;
            background-color: green;
            grid-auto-rows: auto;

        }

        .GFG {
            text-align: center;
            font-size: 35px;
            background-color: white;
            padding: 20px 0;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="GFG">1</div>
        <div class="GFG">2</div>
        <div class="GFG">3</div>
        <div class="GFG">4</div>
        <div class="GFG">5</div>
    </div>
</body>

</html>

Output:

Example 2: Using grid-auto-rows: length

In this example we creates a CSS grid where each row is 3.5cm tall. The grid has two columns in the first row, and each item has a white background and centered text.

HTML
<!DOCTYPE html>
<html>
<head>
    <title>
        CSS grid-auto-rows Property
    </title>

    <style>
        .main {
            display: grid;
            grid-template-areas: "a a";
            grid-gap: 20px;
            padding: 30px;
            background-color: green;
            grid-auto-rows: 3.5cm;

        }

        .GFG {
            text-align: center;
            font-size: 35px;
            background-color: white;
            padding: 20px 0;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="GFG">1</div>
        <div class="GFG">2</div>
        <div class="GFG">3</div>
        <div class="GFG">4</div>
        <div class="GFG">5</div>
    </div>
</body>
</html>

Output:

Example 4: Using grid-auto-rows: minmax(min, max)

In this example we creates a grid with rows automatically set to 30% of the container’s height. It includes five centered, white-background items in a green grid with a 20px gap.

HTML
<!DOCTYPE html>
<html>
<head>
    <title>
        CSS grid-auto-rows Property
    </title>

    <style>
        .main {
            display: grid;
            grid-template-areas: "a a";
            grid-gap: 20px;
            padding: 30px;
            background-color: green;
            grid-auto-rows: 30%;

        }

        .GFG {
            text-align: center;
            font-size: 35px;
            background-color: white;
            padding: 20px 0;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="GFG">1</div>
        <div class="GFG">2</div>
        <div class="GFG">3</div>
        <div class="GFG">4</div>
        <div class="GFG">5</div>
    </div>
</body>
</html>

Output: Example 5 : Using grid-auto-rows: minmax( ).

In this example we defines a grid container with auto-sized rows that adjust between a minimum of 100px and a maximum of 3.5cm. Items inside are centered with white backgrounds, spaced by 20px, within a green grid layout.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>
        CSS grid-auto-rows Property
    </title>

    <style>
        .main {
            display: grid;
            grid-template-areas: "a a";
            grid-gap: 20px;
            padding: 30px;
            background-color: green;
            grid-auto-rows: minmax(100px, 3.5cm);

        }

        .GFG {
            text-align: center;
            font-size: 35px;
            background-color: white;
            padding: 20px 0;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="GFG">1</div>
        <div class="GFG">2</div>
        <div class="GFG">3</div>
        <div class="GFG">4</div>
        <div class="GFG">5</div>
    </div>
</body>

</html>

Output:

Example 6: Using grid-auto-rows: initial

In this example we creates a grid with rows set to their initial value, including five centered, white-background items in a green grid with a 20px gap and 30px padding.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>
        CSS grid-auto-rows Property
    </title>

    <style>
        .main {
            display: grid;
            grid-template-areas: "a a";
            grid-gap: 20px;
            padding: 30px;
            background-color: green;
            grid-auto-rows: initial;

        }

        .GFG {
            text-align: center;
            font-size: 35px;
            background-color: white;
            padding: 20px 0;
        }
    </style>
</head>

<body>
    <div class="main">
        <div class="GFG">1</div>
        <div class="GFG">2</div>
        <div class="GFG">3</div>
        <div class="GFG">4</div>
        <div class="GFG">5</div>
    </div>
</body>

</html>

Output:

Example 7: Using grid-auto-rows: inherit

In this example we defines a container with rows set to 80px, and a nested grid that inherits this row height, containing five centered, white-background items in a green grid with a 20px gap.

HTML
<!DOCTYPE html>
<html>

<head>
    <title>
        CSS grid-auto-rows Property
    </title>

    <style>
        .container {
            grid-auto-rows: 80px;
        }

        .main {
            display: grid;
            grid-template-areas: "a a";
            grid-gap: 20px;
            padding: 30px;
            background-color: green;
            grid-auto-rows: inherit;
        }

        .GFG {
            text-align: center;
            font-size: 35px;
            background-color: white;
            padding: 20px 0;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="main">
            <div class="GFG">1</div>
            <div class="GFG">2</div>
            <div class="GFG">3</div>
            <div class="GFG">4</div>
            <div class="GFG">5</div>
        </div>
    </div>
</body>

</html>

Output:

Supported Browsers

The browser supported by grid-auto-rows property are listed below:



Next Article

Similar Reads

  翻译: