grid-template
试试
组成属性
此属性是以下 CSS 属性的简写形式
语法
css
/* Keyword value */
grid-template: none;
/* grid-template-rows / grid-template-columns values */
grid-template: 100px 1fr / 50px 1fr;
grid-template: auto 1fr / auto 1fr auto;
grid-template: [linename] 100px / [columnname1] 30% [columnname2] 70%;
grid-template: fit-content(100px) / fit-content(40%);
/* grid-template-areas grid-template-rows / grid-template-column values */
grid-template:
"a a a"
"b b b";
grid-template:
"a a a" 20%
"b b b" auto;
grid-template:
[header-top] "a a a" [header-bottom]
[main-top] "b b b" 1fr [main-bottom]
/ auto 1fr auto;
/* Global values */
grid-template: inherit;
grid-template: initial;
grid-template: revert;
grid-template: revert-layer;
grid-template: unset;
值
none
-
是一个关键字,将所有三个长写属性都设置为
none
,这意味着没有显式网格。没有命名网格区域。行和列将被隐式生成;它们的大小将由grid-auto-rows
和grid-auto-columns
属性决定。 <'grid-template-rows'> / <'grid-template-columns'>
-
将
grid-template-rows
和grid-template-columns
设置为指定的值,并将grid-template-areas
设置为none
。 [ <line-names>? <string> <track-size>? <line-names>? ]+ [ / <explicit-track-list> ]?
-
将
grid-template-areas
设置为列出的字符串,grid-template-rows
设置为每个字符串后面的轨迹大小(对于任何缺失的大小填充auto
),并插入在每个大小之前/之后定义的命名行,以及grid-template-columns
设置为斜杠后面的轨迹列表(如果没有指定,则为none
)。注意:
repeat()
函数在这些轨迹列表中不允许使用,因为轨迹旨在与“ASCII 艺术”中的行/列一一对应地进行视觉对齐。
注意: grid
简写接受相同的语法,但还会将隐式网格属性重置为它们的初始值。使用 grid
(而不是 grid-template
)来防止这些值单独级联。
正式定义
初始值 | 与简写属性的每个属性相同
|
---|---|
应用于 | 网格容器 |
继承 | 否 |
百分比 | 与简写属性的每个属性相同
|
计算值 | 与简写属性的每个属性相同
|
动画类型 | 与简写属性的每个属性相同
|
正式语法
grid-template =
none |
[ <'grid-template-rows'> / <'grid-template-columns'> ] |
[ <line-names>? <string> <track-size>? <line-names>? ]+ [ / <explicit-track-list> ]?
<grid-template-rows> =
none |
<track-list> |
<auto-track-list> |
subgrid <line-name-list>?
<grid-template-columns> =
none |
<track-list> |
<auto-track-list> |
subgrid <line-name-list>?
<line-names> =
'[' <custom-ident>* ']'
<track-size> =
<track-breadth> |
minmax( <inflexible-breadth> , <track-breadth> ) |
fit-content( <length-percentage [0,∞]> )
<explicit-track-list> =
[ <line-names>? <track-size> ]+ <line-names>?
<track-list> =
[ <line-names>? [ <track-size> | <track-repeat> ] ]+ <line-names>?
<auto-track-list> =
[ <line-names>? [ <fixed-size> | <fixed-repeat> ] ]* <line-names>? <auto-repeat> [ <line-names>? [ <fixed-size> | <fixed-repeat> ] ]* <line-names>?
<line-name-list> =
[ <line-names> | <name-repeat> ]+
<track-breadth> =
<length-percentage [0,∞]> |
<flex [0,∞]> |
min-content |
max-content |
auto
<inflexible-breadth> =
<length-percentage [0,∞]> |
min-content |
max-content |
auto
<length-percentage> =
<length> |
<percentage>
<track-repeat> =
repeat( [ <integer [1,∞]> ] , [ <line-names>? <track-size> ]+ <line-names>? )
<fixed-size> =
<fixed-breadth> |
minmax( <fixed-breadth> , <track-breadth> ) |
minmax( <inflexible-breadth> , <fixed-breadth> )
<fixed-repeat> =
repeat( [ <integer [1,∞]> ] , [ <line-names>? <fixed-size> ]+ <line-names>? )
<auto-repeat> =
repeat( [ auto-fill | auto-fit ] , [ <line-names>? <fixed-size> ]+ <line-names>? )
<name-repeat> =
repeat( [ <integer [1,∞]> | auto-fill ] , <line-names>+ )
<fixed-breadth> =
<length-percentage [0,∞]>
示例
定义网格模板
CSS
css
#page {
display: grid;
width: 100%;
height: 200px;
grid-template:
[header-left] "head head" 30px [header-right]
[main-left] "nav main" 1fr [main-right]
[footer-left] "nav foot" 30px [footer-right]
/ 120px 1fr;
}
header {
background-color: lime;
grid-area: head;
}
nav {
background-color: lightblue;
grid-area: nav;
}
main {
background-color: yellow;
grid-area: main;
}
footer {
background-color: red;
grid-area: foot;
}
HTML
html
<div id="page">
<header>Header</header>
<nav>Navigation</nav>
<main>Main area</main>
<footer>Footer</footer>
</div>
结果
规范
规范 |
---|
CSS 网格布局模块 Level 2 # explicit-grid-shorthand |
浏览器兼容性
BCD 表格仅在浏览器中加载