网格
The grid
CSS property is a shorthand property that sets all of the explicit and implicit grid properties in a single declaration.
Using grid
you specify one axis using grid-template-rows
or grid-template-columns
, you then specify how content should auto-repeat in the other axis using the implicit grid properties: grid-auto-rows
, grid-auto-columns
, and grid-auto-flow
.
试一试
注意:您未指定的子属性将设置为其初始值,就像简写一样。此外,间距属性不会被此简写重置。
构成属性
语法
/* <'grid-template'> values */
grid: none;
grid: "a" 100px "b" 1fr;
grid: [linename1] "a" 100px [linename2];
grid: "a" 200px "b" min-content;
grid: "a" minmax(100px, max-content) "b" 20%;
grid: 100px / 200px;
grid: minmax(400px, min-content) / repeat(auto-fill, 50px);
/* <'grid-template-rows'> /
[ auto-flow && dense? ] <'grid-auto-columns'>? values */
grid: 200px / auto-flow;
grid: 30% / auto-flow dense;
grid: repeat(3, [line1 line2 line3] 200px) / auto-flow 300px;
grid: [line1] minmax(20em, max-content) / auto-flow dense 40%;
/* [ auto-flow && dense? ] <'grid-auto-rows'>? /
<'grid-template-columns'> values */
grid: auto-flow / 200px;
grid: auto-flow dense / 30%;
grid: auto-flow 300px / repeat(3, [line1 line2 line3] 200px);
grid: auto-flow dense 40% / [line1] minmax(20em, max-content);
/* Global values */
grid: inherit;
grid: initial;
grid: revert;
grid: revert-layer;
grid: unset;
值
<'grid-template'>
-
定义
grid-template
,包括grid-template-columns
、grid-template-rows
和grid-template-areas
. <'grid-template-rows'> / [ auto-flow && dense? ] <'grid-auto-columns'>?
-
通过
grid-template-rows
属性(以及grid-template-columns
属性设置为none
)显式设置行轨迹,并通过grid-auto-columns
(以及将grid-auto-rows
设置为auto
)指定如何自动重复列轨迹,从而设置自动流。grid-auto-flow
也相应地设置为column
,如果指定了dense
则为dense
。所有其他
grid
子属性都重置为其初始值。 [ auto-flow && dense? ] <'grid-auto-rows'>? / <'grid-template-columns'>
-
通过
grid-template-columns
属性(以及grid-template-rows
属性设置为none
)显式设置列轨迹,并通过grid-auto-rows
(以及将grid-auto-columns
设置为auto
)指定如何自动重复行轨迹,从而设置自动流。grid-auto-flow
也相应地设置为row
,如果指定了dense
则为dense
。所有其他
grid
子属性都重置为其初始值。
正式定义
正式语法
grid =
<'grid-template'> |
<'grid-template-rows'> / [ auto-flow && dense? ] <'grid-auto-columns'>? |
[ auto-flow && dense? ] <'grid-auto-rows'>? / <'grid-template-columns'>
<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-auto-columns> =
<track-size>+
<grid-auto-rows> =
<track-size>+
<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,∞]>
示例
创建网格布局
HTML
<div id="container">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
CSS
#container {
display: grid;
grid: repeat(2, 60px) / auto-flow 80px;
}
#container > div {
background-color: #8ca0ff;
width: 50px;
height: 50px;
}
结果
规范
规范 |
---|
CSS Grid Layout Module Level 2 # grid-shorthand |
浏览器兼容性
BCD 表格仅在浏览器中加载