Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. CSS:层叠样式表
  3. CSS 参考
  4. 属性
  5. grid-template-areas

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

grid-template-areas

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2017年10月.

grid-template-areas CSS 属性是网格区域grid areas 在 CSS 中的特定命名。

尝试一下

grid-template-areas:  "a a a"  "b c c"  "b c c";
grid-template-areas:  "b b a"  "b b c"  "b b c";
grid-template-areas:  "a a ."  "a a ."  ". b c";
<section>  <div>    <div>      <div>One (a)</div>      <div>Two (b)</div>      <div>Three (c)</div>    </div>  </div></section>
#example-element {  border: 1px solid #c5c5c5;  display: grid;  grid-template-columns: 1fr 1fr 1fr;  grid-template-rows: repeat(3, minmax(40px, auto));  grid-gap: 10px;  width: 200px;}#example-element :nth-child(1) {  background-color: rgba(0, 0, 255, 0.2);  border: 3px solid blue;  grid-area: a;}#example-element :nth-child(2) {  background-color: rgba(255, 0, 200, 0.2);  border: 3px solid rebeccapurple;  grid-area: b;}#example-element :nth-child(3) {  background-color: rgba(94, 255, 0, 0.2);  border: 3px solid green;  grid-area: c;}

网格区块 (grid areas) 和网格项 (grid item) 沒有关联,但是它们可以和一些网格定位属性 (grid-placement properties) 关联起来,比如grid-row-startgrid-row-endgrid-column-startgrid-column-end;也可以和一些速记 (shorthands) 属性关联起来,比如grid-rowgrid-columngrid-area

语法

css
/* Keyword value */grid-template-areas: none;/* <string> values */grid-template-areas: "a b"; /* 一行 两列 */grid-template-areas:  "a b b"  "a c d"; /* 两行 三列 *//* Global values */grid-template-areas: inherit; /* 继承 */grid-template-areas: initial; /* 默认值 */grid-template-areas: unset; /* 未设置 */

none

网格容器没有定义任何的网格区块 (grid areas)。

<string>+

每一个给定的字符串会生成一行,一个字符串中用空格分隔的每一个单元 (cell) 会生成一列。多个同名的,跨越相邻行或列的单元称为网格区块 (grid area)。非矩形的网格区块是无效的。

形式语法

grid-template-areas =
none|
<string>+

示例

HTML

html
<section>  <header>Header</header>  <nav>Navigation</nav>  <main>Main area</main>  <footer>Footer</footer></section>

CSS

css
#page {  display: grid; /* 1.设置 display 为 grid */  width: 100%;  height: 250px;  grid-template-areas:    "head head"    "nav  main"    "nav  foot"; /* 2.区域划分 当前为 三行 两列 */  grid-template-rows: 50px 1fr 30px; /* 3.各区域 宽高设置 */  grid-template-columns: 150px 1fr;}#page > header {  grid-area: head; /* 4. 指定当前元素所在的区域位置,从 grid-template-areas 选取值 */  background-color: #8ca0ff;}#page > nav {  grid-area: nav;  background-color: #ffa08c;}#page > main {  grid-area: main;  background-color: #ffff64;}#page > footer {  grid-area: foot;  background-color: #8cffa0;}

结果

规范

Specification
CSS Grid Layout Module Level 2
# grid-template-areas-property
初始值none
适用元素网格容器
是否是继承属性
计算值as specified
动画类型离散值

浏览器兼容性

参见

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp