CSS grid网格属性

grid 属性是以下属性的简写属性:

  • grid-template-rows
  • grid-template-columns
  • grid-template-areas
  • grid-auto-rows
  • grid-auto-columns
  • grid-auto-flow

只能指定显式网格属性(grid-template-rows、grid-template-areas、grid-template-columns)或者隐式网格属性(grid-auto-rows、grid-auto-columns、grid-auto-flow) 在一个网格声明中。
未指定的子属性设置为其初始值。

初始值none none none auto auto row
应用于网格容器。
继承无效
可动画的是的。网格布局是有动画的。
版本CSS网格布局模块级别1
DOM 语法object.Style.Grid ="150px/auto auto auto";

语法

grid: none | grid-template-rows/grid-template-columns | grid-template-areas | grid-template-rows/[grid-auto-flow] grid-auto-columns | [grid-auto-flow] grid-auto-rows/grid-template-columns | initial | inherit;

网格属性示例:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .grid-container {
        display: grid;
        grid-template-columns: auto auto auto;
        background-color: #ccc;
        padding: 10px;
      }
      .grid-box {
        background-color: #eee;
        border: 1px solid rgba(0, 0, 0, 0.8);
        padding: 30px;
        font-size: 30px;
        text-align: center;
      }
    </style>
  </head>
  <body>
    <h2>Grid 属性示例</h2>
    <div class="grid-container">
      <div class="grid-box">1</div>
      <div class="grid-box">2</div>
      <div class="grid-box">3</div>
      <div class="grid-box">4</div>
      <div class="grid-box">5</div>
      <div class="grid-box">6</div>
      <div class="grid-box">7</div>
      <div class="grid-box">8</div>
      <div class="grid-box">9</div>
    </div>
  </body>
</html>

具有“grid-auto-flow”值的网格属性示例:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .grid-container {
        display: grid;
        grid-template-columns: auto auto auto;
        grid-template-rows: auto auto;
        grid-gap: 5px;
        background-color: #1c87c9;
        padding: 10px;
        grid-auto-flow: column;
      }
      .grid-container > div {
        background-color: rgba(255, 255, 255, 0.8);
        text-align: center;
        padding: 20px 0;
        font-size: 20px;
      }
    </style>
  </head>
  <body>
    <h2>Grid 属性示例</h2>
    <div class="grid-container">
      <div class="grid-box1">1</div>
      <div class="grid-box2">2</div>
      <div class="grid-box3">3</div>
      <div class="grid-box4">4</div>
    </div>
  </body>
</html>

行大小设置为“100px”的网格属性示例:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .grid-box1 {
        grid-area: 1/1/2/2;
      }
      .grid-box2 {
        grid-area: 1/2/2/3;
      }
      .grid-box3 {
        grid-area: 1/3/2/4;
      }
      .grid-box4 {
        grid-area: 2/1/3/2;
      }
      .grid-box5 {
        grid-area: 2/2/3/3;
      }
      .grid-box6 {
        grid-area: 2/3/3/4;
      }
      .grid-container {
        display: grid;
        grid-auto-rows: 100px;
        grid-gap: 10px;
        background-color: #1c87c9;
        padding: 10px;
      }
      .grid-container > div {
        background-color: #eee;
        text-align: center;
        padding: 20px 0;
        font-size: 25px;
      }
    </style>
  </head>
  <body>
    <h2>Grid 属性示例</h2>
    <div class="grid-container">
      <div class="grid-box1">1</div>
      <div class="grid-box2">2</div>
      <div class="grid-box3">3</div>
      <div class="grid-box4">4</div>
      <div class="grid-box5">5</div>
      <div class="grid-box6">6</div>
    </div>
  </body>
</html>

CSS grid 属性值说明

描述
none未指定列和行的大小。 这是此属性的默认值。
grid-template rows/grid-template-columns指定列和行的大小。
grid-template-areas通过使用命名项目(item)(item)指定网格布局。
grid-template rows/grid-auto-columns规定了行的大小和列的自动大小。
grid-auto-rows/grid-template-columns指定了行的自动大小,并设置了网格模板列属性。
grid-template rows/grid-auto-flow grid-auto-columns通过使用命名项目(item)(item)指定网格布局。
grid-auto flow grid-auto-rows/grid-template-columns指定如何放置自动放置的项目(item)(item)和行的自动大小,并设置网格模板列属性。
initial使属性使用其默认值。
inherit从父母元素继承属性。
日期:2020-06-02 22:14:35 来源:oir作者:oir