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 July 2020.

* Learn more
* See full compatibility
* Report feedback
的\*\*`grid-template`\*\*CSS 屬性是一個速記屬性，用於定義grid columns (en-US)，rows (en-US)，和area (en-US)。

嘗試一下
----

作者可以為長期屬性設置值：`grid-template-rows` (en-US),`grid-template-columns` (en-US)和`grid-template-areas`  (en-US)。

句法
--


```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-rows` (en-US)和`grid-auto-columns` (en-US)屬性確定。

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

將`grid-template-rows` (en-US)和`grid-template-columns` (en-US)設置為指定值，並設置`grid-template-areas` (en-US)至`none`。

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

 Sets `grid-template-areas` (en-US) to the strings listed, `grid-template-rows` (en-US) to the track sizes following each string (filling in `auto` for any missing sizes), and splicing in the named lines defined before/after each size, and `grid-template-columns` (en-US) to the track listing specified after the slash (or `none`, if not specified).
 Note: The `repeat()` (en-US) 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:** The `grid` (en-US) shorthand accepts the same syntax, but also resets the implicit grid properties to their initial values. Use `grid` (as opposed to `grid-template`) to prevent these values from cascading in seperately.

### Formal syntax
```

grid-template =   
  none                                                | (en-US)  
  [ (en-US) <'grid-template-rows'> / <'grid-template-columns'> ] (en-US)  | (en-US)  
  [ (en-US) <line-names>? (en-US) <string> (en-US) <track-size>? (en-US) <line-names>? (en-US) ] (en-US)+ (en-US) [ (en-US) / <explicit-track-list> ] (en-US)? (en-US)    
  
<line-names> =   
  '[' <custom-ident> (en-US)\* (en-US) ']'    
  
<track-size> =   
  <track-breadth>                                   | (en-US)  
  minmax( <inflexible-breadth> , <track-breadth> )  | (en-US)  
  fit-content( <length-percentage [0,∞]> )            
  
<explicit-track-list> =   
  [ (en-US) <line-names>? (en-US) <track-size> ] (en-US)+ (en-US) <line-names>? (en-US)    
  
<track-breadth> =   
  <length-percentage [0,∞]>  | (en-US)  
  <flex [0,∞]>               | (en-US)  
  min-content                | (en-US)  
  max-content                | (en-US)  
  auto                         
  
<inflexible-breadth> =   
  <length-percentage [0,∞]>  | (en-US)  
  min-content                | (en-US)  
  max-content                | (en-US)  
  auto                         
  
<length-percentage> =   
  <length> (en-US)      | (en-US)  
  <percentage> (en-US)    
  
```

Examples
--------

### 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 id="page">
  <header>Header</header>
  <nav>Navigation</nav>
  <main>Main area</main>
  <footer>Footer</footer>
</section>
```

### Result

Specifications
--------------

| Specification |
| --- |
| CSS Grid Layout Module Level 2 # explicit-grid-shorthand |

|  |  |
| --- | --- |
| 預設值 (en-US) | as each of the properties of the shorthand:* `grid-template-columns` (en-US): `none`
* `grid-template-rows` (en-US): `none`
* `grid-template-areas` (en-US): `none`
 |
| Applies to | grid containers |
| 繼承與否 | no |
| Percentages | as each of the properties of the shorthand:* `grid-template-columns` (en-US): refer to corresponding dimension of the content area
* `grid-template-rows` (en-US): refer to corresponding dimension of the content area
 |
| Computed value (en-US) | as each of the properties of the shorthand:* `grid-template-columns` (en-US): as specified, but with relative lengths converted into absolute lengths
* `grid-template-rows` (en-US): as specified, but with relative lengths converted into absolute lengths
* `grid-template-areas` (en-US): as specified
 |
| Animation type | as each of the properties of the shorthand:* `grid-template-columns` (en-US): 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` (en-US): 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` (en-US): discrete
 |

Browser compatibility
---------------------

BCD tables only load in the browser with JavaScript enabled. Enable JavaScript to view data.

See also
--------

* Related CSS properties: `grid-template-rows` (en-US), `grid-template-columns` (en-US), `grid-template-areas` (en-US)
* Grid Layout Guide: *Line-based placement with CSS Grid (en-US)*
* Grid Layout Guide: *Grid template areas - Grid definition shorthands (en-US)*
* Video tutorial:\_ Grid Template shorthand\_