Movatterモバイル変換


[0]ホーム

URL:


  1. 給開發者的 Web 技術文件
  2. CSS
  3. Reference
  4. Properties
  5. grid-template

此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。

View in EnglishAlways switch to English

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 ⁨2017年10月⁩.

CSS 的grid-template 屬性是一個用於定義網格欄區域簡寫屬性

嘗試一下

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>  <div>    <div>      <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: 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-template-rowsgrid-template-columnsgrid-template-areas

句法

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: unset;

價值觀

none

是將所有三個長期屬性設置為的關鍵字none,表示沒有顯式網格。沒有命名的網格區域。行和列將隱式生成;它們的大小將由grid-auto-rowsgrid-auto-columns屬性確定。

<'grid-template-rows'> / <'grid-template-columns'>

grid-template-rowsgrid-template-columns設置為指定值,並設置grid-template-areasnone

[ <line-names>? <string> <track-size>? <line-names>? ]+ [ / <explicit-track-list> ]?

Setsgrid-template-areas to the strings listed,grid-template-rows to the track sizes following each string (filling inauto for any missing sizes), and splicing in the named lines defined before/after each size, andgrid-template-columns to the track listing specified after the slash (ornone, if not specified).Note: Therepeat() function isn't allowed in these track listings, as the tracks are intended to visually line up one-to-one with the rows/columns in the 「ASCII art」.

Note: Thegrid shorthand accepts the same syntax, but also resets the implicit grid properties to their initial values. Usegrid (as opposed togrid-template) to prevent these values from cascading in seperately.

Formal syntax

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
<section>  <header>Header</header>  <nav>Navigation</nav>  <main>Main area</main>  <footer>Footer</footer></section>

結果

規範

Specification
CSS Grid Layout Module Level 2
# explicit-grid-shorthand
預設值as each of the properties of the shorthand:
Applies togrid containers
繼承與否no
Percentagesas each of the properties of the shorthand:
Computed valueas each of the properties of the shorthand:
Animation typeas each of the properties of the shorthand:
  • grid-template-columns: simple list of length, percentage, or calc, provided the only differences are in the values of the length, percentage, or calc components in the list
  • grid-template-rows: simple list of length, percentage, or calc, provided the only differences are in the values of the length, percentage, or calc components in the list
  • grid-template-areas: discrete

瀏覽器相容性

參見

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp