grid-template-areas

CSS grid-template-areas Property

The grid-template-areas property is used to refer to the name when setting up the grid layout. You can name grid items with the grid-area property. Each area is defined by apostrophes. These areas may be referenced from such grid-placement properties as grid-row-start, grid-row-end, grid-column-end, grid-column-start, as well as their shorthand properties, like grid-row, grid-area, and grid-column.

Some property extensions are added, such as -webkit- for Safari, Google Chrome, and Opera (newer versions), -moz- for Firefox, -o- for older versions of Opera, etc.

类目类目
Initial Valueauto
Applies toGrid containers.
InheritedNo.
AnimatableYes. Items are animatable.
VersionCSS Grid Layout Module Level 1
DOM Syntaxobject.style.gridTemplateAreas = ‘‘item1 item1 . .’’ ‘‘item1 item1 . .’’;

Syntax

Syntax

grid-template-areas: none | itemnames | initial | inherit;

Example of the grid-template-areas property:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
    <style>
      .box1 {
        grid-area: header;
      }
      .box2 {
        grid-area: menu;
      }
      .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' 'menu main main main right right' 'menu footer footer footer footer footer';
        grid-gap: 10px;
        background-color: #ccc;
        padding: 10px;
      }
      .grid-container > div {
        background-color: #eee;
        text-align: center;
        padding: 20px 0;
        font-size: 30px;
      }
    </style>
  </head>
  <body>
    <h2>Grid-template-areas property example</h2>
    <div class="grid-container">
      <div class="box1">Header</div>
      <div class="box2">Menu</div>
      <div class="box3">Main</div>
      <div class="box4">Right</div>
      <div class="box5">Footer</div>
    </div>
  </body>
</html>

Result

Example of the grid-template-areas property, where an “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 . . .";
        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 named grid areas will be defined.This is the default value of this property.Play it »
itemnamesA sequence which specifies the display of each column and row.Play it »


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

扫一扫,反馈当前页面

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