grid-template

CSS grid-template Property

The grid-template property defines the grid columns, rows and areas. It is a shorthand property for the following properties:

  • grid-template-rows

  • grid-template-columns

  • grid-template-areas

The grid-template property sets the rows and columns, separated by a forward slash.

The grid-template property does not affect any gutters (gap) cascading down.

The CSS grid shorthand property resets grid properties to their first values. If you don’t want these values cascade in separately, instead of the grid-template property use the grid property.

类目类目
Initial Valuenone none none
Applies toGrid containers.
InheritedNo.
AnimatableYes. Grid layout is animatable.
VersionCSS Grid Layout Module Level 1
DOM Syntaxobject.style.gridTemplate =“100px / auto auto”;

Syntax

Syntax

grid-template: none | grid-template-rows / grid-template-columns | grid-template-areas | initial | inherit;

Example of the grid-template property:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .grid-container {
        display: grid;
        grid-template: 170px / auto auto auto;
        grid-gap: 10px;
        background-color: #ccc;
        padding: 10px;
      }
      .grid-container > div {
        background-color: #eee;
        text-align: center;
        padding: 30px 0;
        font-size: 20px;
      }
    </style>
  </head>
  <body>
    <h2>Grid-template property example</h2>
    <div class="grid-container">
      <div>1</div>
      <div>2</div>
      <div>3</div>
      <div>4</div>
      <div>5</div>
      <div>6</div>
    </div>
  </body>
</html>

Result

Example of the grid-template property, where “item1” name is given to a grid item:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .box {
        grid-area: item1;
      }
      .grid-container {
        display: grid;
        grid-template: 'item1 item1 . .' 'item1 item1 . .';
        grid-gap: 10px;
        background-color: #ccc;
        padding: 10px;
      }
      .grid-container > div {
        background-color: #eee;
        text-align: center;
        padding: 30px 0;
        font-size: 20px;
      }
    </style>
  </head>
  <body>
    <h2>Grid-template property example</h2>
    <div class="grid-container">
      <div class="box">1</div>
      <div>2</div>
      <div>3</div>
      <div>4</div>
      <div>5</div>
      <div>6</div>
    </div>
  </body>
</html>

Values

Values

ValueDescriptionPlay it
noneNo size will be defined. This is the default value of this property.
grid-template-rows / grid-template-columnsSpecifies the size of the rows and columns.Play it »
grid-template-areasSpecifies the grid layout using named items.Play it »
initialMakes the property use its default value.
inheritInherits the property from its parents element.


请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部