CSS 网格布局模块

原文:https://www.geeksforgeeks.org/css-grid-layout-module/

CSS 网格布局模块用于创建基于网格的布局系统,借助行和列,在不使用浮动定位的情况下,更容易设计任何网页。

语法:

.class {
    display:grid;
}

注意:如果一个 HTML 元素设置了显示: 网格,则该元素成为网格;在样式部分或内嵌网格中。下面你会看到两个例子。

CSS 网格布局属性:以下是网格布局属性:

示例 1: 本示例说明了显示: 网格的使用;属性。

超文本标记语言

<!DOCTYPE html>
<html>

<head>
    <style>

        /* Designing all grid */
    .grid-container {
        display: grid;
        grid-template-columns: auto auto auto;
        background-color: gray;
        padding: 5px;
    }

    /* Designing all grid-items */
    .grid-item {
        background-color: rgba(255, 255, 255, 0.8);
        border: 1px solid black;
        padding: 20px;
        font-size: 30px;
        text-align: center;
    }

    /* Designing h1 element */
    h1 {
        color: green;
        text-align: center;
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>

    <!-- Creating grid -->
    <div class="grid-container">
        <div class="grid-item">1</div>
        <div class="grid-item">2</div>
        <div class="grid-item">3</div>
        <div class="grid-item">4</div>
        <div class="grid-item">5</div>
        <div class="grid-item">6</div>
        <div class="grid-item">7</div>
        <div class="grid-item">8</div>
        <div class="grid-item">9</div>
    </div>
</body>

</html>

输出:

示例 2: 该示例说明了显示的使用:内嵌网格;属性。

超文本标记语言

<!DOCTYPE html>
<html>

<head>
    <style>

        /* Designing all grid */
    .grid-container {
        display: inline-grid;
        grid-template-columns: auto auto auto;
        background-color: gray;
        padding: 5px;
    }

    /* Designing all grid-items */
    .grid-item {
        background-color: rgba(255, 255, 255, 0.8);
        border: 1px solid black;
        padding: 20px;
        font-size: 30px;
    }

    /* Designing h1 element */
    h1 {
        color: green;
        text-align: center;
    </style>
</head>

<body>
    <center>
        <h1>GeeksforGeeks</h1>

        <!-- Creating grids -->
        <div class="grid-container">
            <div class="grid-item">1</div>
            <div class="grid-item">2</div>
            <div class="grid-item">3</div>
            <div class="grid-item">4</div>
            <div class="grid-item">5</div>
            <div class="grid-item">6</div>
            <div class="grid-item">7</div>
            <div class="grid-item">8</div>
            <div class="grid-item">9</div>
        </div>
    </center>
</body>

</html>

输出:

您可以在网格系统上控制以下内容:

  • 栅柱间隙
  • 网格行间隙
  • 网格间隙

示例 3: 在下面的代码中,我们同时使用了格列间隙格行间隙

超文本标记语言

<!DOCTYPE html>
<html>

<head>
    <style>

        /* Designing all grid */
    .grid-container {
        display: grid;
        grid-template-columns: auto auto auto;
        background-color: gray;
        grid-column-gap: 50px;
        grid-row-gap: 50px;
        padding: 5px;
    }

    /* Designing all grid-items */
    .grid-item {
        background-color: rgba(255, 255, 255, 0.8);
        border: 1px solid black;
        padding: 20px;
        font-size: 30px;
        text-align: center;
    }

    /* Designing h1 element */
    h1 {
        color: green;
        text-align: center;
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>

    <!-- Creating grids -->
    <div class="grid-container">
        <div class="grid-item">1</div>
        <div class="grid-item">2</div>
        <div class="grid-item">3</div>
        <div class="grid-item">4</div>
        <div class="grid-item">5</div>
        <div class="grid-item">6</div>
        <div class="grid-item">7</div>
        <div class="grid-item">8</div>
        <div class="grid-item">9</div>
    </div>
</body>

</html>

输出:

注:同样格隙也起作用。

您可以在网格系统上控制以下内容:

  • 网格列线
  • 网格行线

示例:4 在下面的代码中,我们同时使用了网格-列线网格-行线

超文本标记语言

<!DOCTYPE html>
<html>

<head>
    <style>

    /* Designing all grid */
    .grid-container {
        display: grid;
        grid-template-columns: auto auto auto;
        grid-gap: 10px;
        background-color: gray;
        padding: 10px;
    }

    /* Designing all grid-items */
    .grid-container > div {
        background-color: rgba(255, 255, 255, 0.8);
        text-align: center;
        padding: 20px 0;
        font-size: 30px;
    }

    /* Grid Column */
    .item1 {
        grid-column-start: 1;
        grid-column-end: 3;
    }

    /* Grid row */
    .item3 {
        grid-row-start: 2;
        grid-row-end: 5;
    }

    /* Designing h1 element */
    h1 {
        color: green;
        text-align: center;
    }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>

    <!-- Creating grids -->
    <div class="grid-container">
        <div class="item1">1</div>
        <div class="item2">2</div>
        <div class="item3">3</div>
        <div class="item4">4</div>
        <div class="item5">5</div>
        <div class="item6">6</div>
        <div class="item7">7</div>
        <div class="item8">8</div>
    </div>
</body>

</html>

输出:

支持的浏览器:CSS 网格布局模块支持的浏览器如下:

  • 谷歌 Chrome 57.0
  • 微软边缘 16.0
  • Firefox 52.0
  • Safari 10.1
  • Opera 44.0