The grid-template CSS property is a shorthand property for defining grid columns, rows, and areas.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Constituent properties
This property is a shorthand for the following CSS properties:
Syntax
/* 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;
Values
none- Is a keyword that sets all three longhand properties to
none, meaning there is no explicit grid. There are no named grid areas. Rows and columns will be implicitly generated; their size will be determined by thegrid-auto-rowsandgrid-auto-columnsproperties. <'grid-template-rows'> / <'grid-template-columns'>- Sets
grid-template-rowsandgrid-template-columnsto the specified values, and setsgrid-template-areastonone. [ <line-names>? <string> <track-size>? <line-names>? ]+ [ / <explicit-track-list> ]?- Sets
grid-template-areasto the strings listed,grid-template-rowsto the track sizes following each string (filling inautofor any missing sizes), and splicing in the named lines defined before/after each size, andgrid-template-columnsto the track listing specified after the slash (ornone, if not specified).
Note: The
repeat()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 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 definition
| 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 | discrete |
Formal syntax
none | [ <'grid-template-rows'> / <'grid-template-columns'> ] | [ <line-names>? <string> <track-size>? <line-names>? ]+ [ / <explicit-track-list> ]?where
<line-names> = '[' <custom-ident>* ']'
<track-size> = <track-breadth> | minmax( <inflexible-breadth> , <track-breadth> ) | fit-content( [ <length> | <percentage> ] )
<explicit-track-list> = [ <line-names>? <track-size> ]+ <line-names>?where
<track-breadth> = <length-percentage> | <flex> | min-content | max-content | auto
<inflexible-breadth> = <length> | <percentage> | min-content | max-content | autowhere
<length-percentage> = <length> | <percentage>
Examples
Defining a grid template
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
<section id="page"> <header>Header</header> <nav>Navigation</nav> <main>Main area</main> <footer>Footer</footer> </section>
Result
Specifications
| Specification | Status | Comment |
|---|---|---|
| CSS Grid Layout The definition of 'grid-template' in that specification. |
Candidate Recommendation | Initial definition |
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
grid-template | Chrome
Full support
57
| Edge Full support 16 | Firefox
Full support
52
| IE No support No | Opera
Full support
44
| Safari Full support 10.1 | WebView Android Full support 57 | Chrome Android
Full support
57
| Firefox Android
Full support
52
| Opera Android
Full support
43
| Safari iOS Full support 10.3 | Samsung Internet Android Full support 6.0 |
Legend
- Full support
- Full support
- No support
- No support
- User must explicitly enable this feature.
- User must explicitly enable this feature.
See also
- Related CSS properties:
grid-template-rows,grid-template-columns,grid-template-areas - Grid Layout Guide: Line-based placement with CSS Grid
- Grid Layout Guide: Grid template areas - Grid definition shorthands
- Video tutorial: Grid Template shorthand
