CSS - 网格行



描述

grid-row 属性用于控制网格项目在网格中的垂直位置和跨度。

示例

这是一个示例,其中网格项目 2 从第 1 行开始,跨越 3 行:

您可以使用编辑和运行选项编辑并运行此代码。

<!DOCTYPE html>
<html>
<head>
    <title>CSS - grid-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;
            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-item2{
            grid-row: 1 / span 3;
        }    
    </style>
</head>
<body>
    <h3>Grid item 2 start from row 1 and span 3 rows.</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>

grid-row-start 属性定义网格项目的起始行。

  • grid-row-start: span 3 - 网格项目将从其指定位置开始,并跨越 3 行。

  • grid-row-start: 2 - 它将从网格的第 2 行开始。

这是一个示例,其中包含可以传递给 grid-row-start 的所有不同值:

您可以使用编辑和运行选项编辑并运行此代码。

<!DOCTYPE html>
<html>
<head>
    <title>CSS - grid-row-start</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-container1 {
            display: grid;
            grid-template-columns: auto auto auto auto;
            color: white;
            text-align: center;
            width: 360px;
            border: 2px solid rgb(29, 231, 80);
            background-color: rgb(29, 231, 80);
        }
        .grid-container1 > div {
            background-color: rgb(228, 9, 9);
            border: 2px solid rgb(29, 231, 80);
            padding: 10px;
        }
        .grid-item2 {
            grid-row-start:  span 3;
        }   
        .grid-container2 {
            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-container2 > div {
            background-color: rgb(228, 9, 9);
            border: 2px solid rgb(29, 231, 80);
            padding: 10px;
        }
        .grid-row-item3{
            grid-row-start: 2;
        }
    </style>
</head>
<body>
    <h4>Grid item 2 will span 3 rows.</h4>
    <div class="grid-container1">
        <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>

    <h4>Grid item 3 will be positioned starting from row 2</h4>
    <div class="grid-container2">
        <div class="grid-row-item1">Grid item 1</div>
        <div class="grid-row-item2">Grid item 2</div>
        <div class="grid-row-item3">Grid item 3</div>
        <div class="grid-row-item4">Grid item 4</div>
        <div class="grid-row-item5">Grid item 5</div>
        <div class="grid-row-item6">Grid item 6</div>
    </div>
</body>
</html>

grid-row-end 属性用于定义网格项目的结束行。

  • grid-row-end: span 3 - 它将从其起始行跨越 3 行。

  • grid-row-end: 3 - 网格项目将在第 3 行结束。

这是一个示例,其中包含可以传递给 grid-row-end 的所有不同值:

您可以使用编辑和运行选项编辑并运行此代码。

<!DOCTYPE html>
<html>
<head>
    <title>CSS - grid-row-end</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-container1 {
            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-container1 > div {
            background-color: rgb(228, 9, 9);
            border: 2px solid rgb(29, 231, 80);
            padding: 10px;
        }
        .grid-item3 {
            grid-row-end: span 3;
        }   
        .grid-container2 {
            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-container2 > div {
            background-color: rgb(228, 9, 9);
            border: 2px solid rgb(29, 231, 80);
            padding: 10px;
        }
        .grid-row-item2 {
            grid-row-end: 3;
        }
    </style>
</head>
<body>
    <h4>Grid item 3 span 3 rows from its starting row line.</h4>
    <div class="grid-container1">
        <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>

    <h4>Grid item 2 will end on row-line 3.</h4>
    <div class="grid-container2">
        <div class="grid-row-item1">Grid item 1</div>
        <div class="grid-row-item2">Grid item 2</div>
        <div class="grid-row-item3">Grid item 3</div>
        <div class="grid-row-item4">Grid item 4</div>
        <div class="grid-row-item5">Grid item 5</div>
        <div class="grid-row-item6">Grid item 6</div>
        <div class="grid-row-item7">Grid item 7</div>
        <div class="grid-row-item8">Grid item 8</div>
    </div>
</body>
</html>

grid-row-gap 属性用于控制网格容器中行之间间隙或空间的大小。

这是一个示例,其中grid-row-gap 属性的值设置为50px

您可以使用编辑和运行选项编辑并运行此代码。

<!DOCTYPE html>
<html>
<head>
    <title>CSS - grid-row-gap</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-row-gap: 50px;
            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 50px gap between 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 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>
广告