grid-area

CSS grid-area Property

The grid-area property is used to specify the size and the location of the grid item within the grid row. It is a shorthand property for the following properties:

  • grid-row-start, specifying the row on which the item should start.

  • grid-column-start, specifying the column on which the item should start.

  • grid-row-end, specifying the row on which the item should end.

  • grid-column-end, specifying the column on which the item should end.

The grid-area property also specifies a name to a grid item. Then, named grid items can be referenced to by the grid-template-areas property of the grid container.

类目类目
Initial Valueauto / auto / auto / auto
Applies toGrid items.
InheritedNo.
AnimatableYes. Grid-area is animatable.
VersionCSS Grid Layout Module Level 1
DOM Syntaxobject.style.gridArea = “1 / 2 / span 2 / span 3”;

Syntax

Syntax

grid-area: grid-row-start / grid-column-start / grid-row-end / grid-column-end / itemname | initial | inherit;

Example of the grid-area property:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .box1 {
        grid-area: header;
      }
      .box2 {
        grid-area: left;
      }
      .box3 {
        grid-area: main;
      }
      .box4 {
        grid-area: right;
      }
      .box5 {
        grid-area: footer;
      }
      .grid-container {
        display: grid;
        grid-template-areas: 'header header header header header header' 'left main main main right right' 'left footer footer footer footer footer';
        grid-gap: 5px;
        background-color: #555;
        padding: 10px;
      }
      .grid-container > div {
        background-color: #ccc;
        text-align: center;
        padding: 30px 0;
        font-size: 30px;
      }
    </style>
  </head>
  <body>
    <h2>Grid-area property example</h2>
    <p>You can use the grid-area property to name grid items.</p>
    <div class="grid-container">
      <div class="box1">Header</div>
      <div class="box2">Left</div>
      <div class="box3">Main</div>
      <div class="box4">Right</div>
      <div class="box5">Footer</div>
    </div>
  </body>
</html>

Result

In the following example, we apply the “itemname” value for the box1 which takes up the place of all five columns.

Example of the grid-area property with the “itemname” value:

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

Values

Values

ValueDescription
grid-row-startSpecifies on which row the item should be displayed.
grid-column-startSpecifies on which column the item should be displayed.
grid-row-endSpecifies on which row-line the item should be stopped, or how many rows to span.
grid-column-endSpecifies on which column-line the item should be stopped, or how many columns to span.
itemnameSpecifies a name for the item.
initialMakes the property use its default value.
inheritInherits the property from its parents element.


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

扫一扫,反馈当前页面

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