grid-template
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since October 2017.
Thuộc tính CSS grid-template là thuộc tính viết tắt để định nghĩa cột lưới, hàng lưới, và vùng lưới.
Try it
grid-template:
"a a a" 40px
"b c c" 40px
"b c c" 40px / 1fr 1fr 1fr;
grid-template:
"b b a" auto
"b b c" 2ch
"b b c" 1em / 20% 20px 1fr;
grid-template:
"a a ." minmax(50px, auto)
"a a ." 80px
"b b c" auto / 2em 3em auto;
<section class="default-example" id="default-example">
<div class="example-container">
<div class="transition-all" id="example-element">
<div>One</div>
<div>Two</div>
<div>Three</div>
</div>
</div>
</section>
#example-element {
border: 1px solid #c5c5c5;
display: grid;
grid-gap: 10px;
width: 200px;
}
#example-element :nth-child(1) {
background-color: rgb(0 0 255 / 0.2);
border: 3px solid blue;
grid-area: a;
}
#example-element :nth-child(2) {
background-color: rgb(255 0 200 / 0.2);
border: 3px solid rebeccapurple;
grid-area: b;
}
#example-element :nth-child(3) {
background-color: rgb(94 255 0 / 0.2);
border: 3px solid green;
grid-area: c;
}
Thuộc tính thành phần
Thuộc tính này là viết tắt cho các thuộc tính CSS sau:
Cú pháp
/* Giá trị từ khóa */
grid-template: none;
/* Giá trị grid-template-rows / grid-template-columns */
grid-template: 100px 1fr / 50px 1fr;
grid-template: auto 1fr / auto 1fr auto;
grid-template: [line-name] 100px / [column-name1] 30% [column-name2] 70%;
grid-template: fit-content(100px) / fit-content(40%);
/* Giá trị grid-template-areas grid-template-rows / grid-template-column */
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;
/* Giá trị toàn cục */
grid-template: inherit;
grid-template: initial;
grid-template: revert;
grid-template: revert-layer;
grid-template: unset;
Giá trị
none-
Đặt cả ba thuộc tính dài thành
none, nghĩa là không có lưới tường minh. Không có vùng lưới được đặt tên. Các hàng và cột sẽ được tạo ra ẩn; kích thước của chúng sẽ được xác định bởi các thuộc tínhgrid-auto-rowsvàgrid-auto-columns. Đây là giá trị mặc định. <'grid-template-rows'> / <'grid-template-columns'>-
Đặt
grid-template-rowsvàgrid-template-columnstheo các giá trị được chỉ định, và đặtgrid-template-areasthànhnone. [ <line-names>? <string> <track-size>? <line-names>? ]+ [ / <explicit-track-list> ]?-
Đặt
grid-template-areastheo các chuỗi được liệt kê,grid-template-rowstheo các kích thước track theo sau mỗi chuỗi (điềnautocho bất kỳ kích thước bị thiếu nào), và nối các đường được đặt tên xác định trước/sau mỗi kích thước, vàgrid-template-columnstheo danh sách track được chỉ định sau dấu gạch chéo (hoặcnone, nếu không được chỉ định).Note: Hàm
repeat()không được phép trong các danh sách track này, vì các track được thiết kế để căn chỉnh trực quan một-một với các hàng/cột trong "ASCII art".
Note:
Viết tắt grid chấp nhận cú pháp tương tự, nhưng cũng đặt lại các thuộc tính lưới ẩn về giá trị ban đầu của chúng. Sử dụng grid (thay vì grid-template) để ngăn các giá trị này cascade riêng biệt.
Định nghĩa hình thức
| Initial value | as each of the properties of the shorthand:
|
|---|---|
| Applies to | grid containers |
| Inherited | no |
| Percentages | as each of the properties of the shorthand:
|
| Computed value | as each of the properties of the shorthand:
|
| Animation type | as each of the properties of the shorthand:
|
Cú pháp hình thức
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>? <track-size> ]+ <line-names>? )
<name-repeat> =
repeat( [ <integer [1,∞]> | auto-fill ] , <line-names>+ )
<integer> =
<number-token>
<fixed-breadth> =
<length-percentage [0,∞]>
Ví dụ
>Định nghĩa một mẫu lưới
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
<div id="page">
<header>Header</header>
<nav>Navigation</nav>
<main>Main area</main>
<footer>Footer</footer>
</div>
Kết quả
Đặc tả
| Specification |
|---|
| CSS Grid Layout Module Level 2> # explicit-grid-shorthand> |