CSS grid-template-areas Property
The CSS grid-template-areas property defines named regions within a grid container using a visual arrangement of strings.
Each string represents a row of grid cells, and each name identifies an area that can contain a grid item. Combined with the grid-area property, named areas make it easier to create readable, maintainable, and responsive layouts without relying entirely on numbered grid lines.
grid-template-areas Syntax
The grid-template-areas property accepts none or one or more quoted strings describing the arrangement of named grid areas.
Basic Syntax
selector {
grid-template-areas: none;
}
Single Named Area
.container {
grid-template-areas: "content";
}
Two-Column Layout
.container {
grid-template-areas: "sidebar main";
}
Multiple Rows
.container {
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
Empty Grid Cells
.container {
grid-template-areas:
"header header"
"sidebar .";
}
Reset Named Areas
.container {
grid-template-areas: none;
}
CSS-Wide Keywords
.container {
grid-template-areas: initial;
}
.container {
grid-template-areas: inherit;
}
.container {
grid-template-areas: unset;
}
.container {
grid-template-areas: revert;
}
.container {
grid-template-areas: revert-layer;
}
Each quoted string defines one row. Names separated by whitespace represent individual cells, while repeated names identify cells belonging to the same area.
Property Values
The property uses a small set of values to describe the grid's named regions.
| Value | Description |
|---|---|
none |
Defines no named grid areas through this property. |
<string>+ |
One or more quoted strings representing rows of named or unnamed grid cells. |
<custom-ident> |
A name written inside a string to identify a grid area. |
. |
Represents an unnamed grid cell inside an area string. |
initial |
Resets the property to none. |
inherit |
Inherits the computed value from the parent. |
unset |
Resets this non-inherited property to none. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | none |
| Inherited | No |
| Applies to | Grid containers |
| Computed value | As specified |
| Animation type | Discrete |
| Percentages | Not applicable |
| Related properties | grid-area, grid-template, grid-template-rows, grid-template-columns |
The property describes the placement and names of grid regions. It does not directly specify track widths, heights, or spacing.
How It Works
The grid-template-areas property creates a visual map of the grid. Each name corresponds to a rectangular region that can be assigned to a grid item.
Create a Grid Container
.container {
display: grid;
grid-template-columns: 180px 1fr;
grid-template-rows: 70px 150px 50px;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
gap: 10px;
}
This defines a three-row, two-column grid with four named areas.
Assign the Areas
.header {
grid-area: header;
}
.sidebar {
grid-area: sidebar;
}
.main {
grid-area: main;
}
.footer {
grid-area: footer;
}
Each grid item is placed into the region matching its assigned area name.
Visual Structure
| Row | Column 1 | Column 2 |
|---|---|---|
| 1 | header | header |
| 2 | sidebar | main |
| 3 | footer | footer |
Repeating a name across adjacent cells makes the corresponding area span those cells.
Naming Grid Areas
Named areas use identifiers written inside quoted strings. Choose descriptive names that reflect the purpose of each region.
Descriptive Area Names
.container {
grid-template-areas:
"site-header site-header"
"navigation article"
"site-footer site-footer";
}
Assign Matching Names
.site-header {
grid-area: site-header;
}
.navigation {
grid-area: navigation;
}
.article {
grid-area: article;
}
.site-footer {
grid-area: site-footer;
}
Area Names Are Case-Sensitive
.container {
grid-template-areas: "main";
}
.item {
grid-area: Main;
}
The names main and Main are different. The item does not reference the defined main area.
Important Consideration
Area names are CSS identifiers. They cannot be reserved CSS-wide keywords such as initial or inherit. Use consistent, meaningful names throughout the layout.
Empty Grid Cells
A period inside an area string represents an unnamed grid cell. This is useful for leaving spaces between named regions.
Leave One Cell Unnamed
.container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-areas:
"header header header"
"sidebar main .";
}
The last cell in the second row has no assigned area name.
Create a Centered Region
.container {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
grid-template-areas: ". content .";
}
.content {
grid-area: content;
}
The named content area occupies the middle column.
Multiple Periods
.container {
grid-template-areas: "header ... sidebar";
}
A continuous sequence of periods represents one unnamed cell. In this example, the string defines three cells.
Important Consideration
An unnamed cell is not necessarily empty. Automatically placed grid items can still occupy it unless other placement rules prevent that.
Rectangular Grid Areas
Every named grid area must form a single rectangle. Names cannot create disconnected or irregularly shaped regions.
Valid Rectangular Area
.container {
grid-template-areas:
"main main sidebar"
"main main sidebar";
}
The main area forms a two-row, two-column rectangle. The sidebar area occupies the third column across both rows.
Invalid L-Shaped Area
.container {
grid-template-areas:
"main main sidebar"
"main footer sidebar";
}
The main area is L-shaped, making the entire declaration invalid.
Invalid Disconnected Area
.container {
grid-template-areas:
"main sidebar main";
}
The two main cells are separated by another named area and do not form a rectangle.
Equal Numbers of Cells
.container {
grid-template-areas:
"header header"
"sidebar main extra";
}
This declaration is invalid because the first row contains two cells and the second contains three.
Important Consideration
If a named area arrangement is invalid, the browser ignores the entire grid-template-areas declaration rather than applying only the valid rows.
Placing Items with grid-area
Defining named areas does not automatically place specific grid items into those areas. The grid-area property assigns an item to a named region.
Define the Layout
.container {
display: grid;
grid-template-areas:
"header header"
"menu content";
grid-template-columns: 160px 1fr;
}
Place Individual Items
.header {
grid-area: header;
}
.menu {
grid-area: menu;
}
.content {
grid-area: content;
}
Place an Item with Grid Lines
.content {
grid-row: 2;
grid-column: 2;
}
The numbered placement above targets the same cell as the named content area in this layout.
Overlapping Named Areas
Two items assigned to the same named area can overlap because both occupy the same grid region. Use separate area names when items should occupy different regions.
Important Consideration
The grid-area property also supports numbered grid line placement. A named area is one convenient way to specify an item's placement, not the only method.
Implicit Named Grid Lines
Defining a named grid area automatically creates corresponding grid line names with -start and -end suffixes.
Define a Named Area
.container {
display: grid;
grid-template-areas:
"header header"
"content content";
}
Reference Generated Row Lines
.item {
grid-row-start: content-start;
grid-row-end: content-end;
}
Reference Generated Column Lines
.item {
grid-column-start: content-start;
grid-column-end: content-end;
}
The generated names can be used for row or column placement according to the boundaries of the named area.
Combine Named Areas with Line Placement
.item {
grid-row: content-start / content-end;
grid-column: 1 / -1;
}
This technique is useful when an item needs the row boundaries of a named area but a different column placement.
Track Sizing
The grid-template-areas property defines the arrangement of named regions but does not directly set their dimensions.
Define Area Names
.container {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
Set Column and Row Sizes
.container {
grid-template-columns: 200px minmax(0, 1fr);
grid-template-rows: 70px minmax(150px, auto) 50px;
}
Use Flexible Tracks
.container {
grid-template-columns: 1fr 3fr;
grid-template-rows: auto 1fr auto;
}
Add Spacing
.container {
gap: 16px;
}
The gap property adds spacing between grid tracks without adding named area cells.
Automatically Created Explicit Tracks
If the area strings define more rows or columns than the track sizing properties specify, CSS Grid creates the remaining explicit tracks with automatic sizing.
Important Consideration
Use grid-template-columns and grid-template-rows alongside named areas when precise dimensions are needed.
Responsive Layouts
Named grid areas are especially useful for responsive design because you can rearrange the layout without changing the HTML structure.
Single-Column Mobile Layout
.page-layout {
display: grid;
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
grid-template-columns: minmax(0, 1fr);
gap: 16px;
}
Two-Column Desktop Layout
@media (min-width: 768px) {
.page-layout {
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: 220px minmax(0, 1fr);
}
}
Assign Areas Once
.header {
grid-area: header;
}
.main {
grid-area: main;
}
.sidebar {
grid-area: sidebar;
}
.footer {
grid-area: footer;
}
The same HTML elements occupy different positions as the viewport changes. Their grid-area assignments remain unchanged.
Important Consideration
Rearranging grid items changes their visual positions, not their document or keyboard navigation order. Keep the source order logical for accessibility.
Shorthand Relationship
The grid-template-areas property is one of three longhand properties combined by the grid-template shorthand.
Separate Longhand Properties
.container {
grid-template-areas:
"header header"
"sidebar main";
grid-template-rows: 70px 150px;
grid-template-columns: 180px 1fr;
}
Equivalent grid-template Shorthand
.container {
grid-template:
"header header" 70px
"sidebar main" 150px
/ 180px 1fr;
}
Reset Named Areas with a Shorthand
.container {
grid-template-areas:
"header header"
"sidebar main";
grid-template: 70px 150px / 180px 1fr;
}
The final declaration resets grid-template-areas to none because the row/column shorthand form does not define named areas.
Reset Named Areas Directly
.container {
grid-template-areas: none;
}
This removes the named area definitions without resetting existing grid-template-rows or grid-template-columns declarations.
Important Consideration
The broader grid shorthand also affects the grid template properties. Check declaration order when combining these shorthands with individual longhand properties.
Inheritance
The grid-template-areas property is not inherited.
Parent Grid Container
.parent {
display: grid;
grid-template-areas:
"header"
"content";
}
Nested Grid Container
.nested {
display: grid;
grid-template-areas: none;
}
A nested grid container does not automatically receive the named areas of its parent.
Explicit Inheritance
.nested {
grid-template-areas: inherit;
}
The inherit keyword copies the computed area definition from the parent. The nested container still establishes its own grid.
Reset to the Initial Value
.container {
grid-template-areas: initial;
}
Reset with unset
.container {
grid-template-areas: unset;
}
Because the property is not inherited, unset resets it to none.
grid-template-areas Example
This example creates a three-row, two-column layout using four named areas. The header and footer span both columns, while the sidebar and main content share the middle row.
HTML Example
<div class="grid-template-areas-demo">
<div class="demo-header">Header</div>
<div class="demo-sidebar">Sidebar</div>
<div class="demo-main">Main Content</div>
<div class="demo-footer">Footer</div>
</div>
CSS Example
.grid-template-areas-demo {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
grid-template-columns: minmax(90px, 1fr) minmax(0, 2fr);
grid-template-rows: 60px 110px 50px;
gap: 10px;
}
.grid-template-areas-demo > div {
display: flex;
align-items: center;
justify-content: center;
padding: 10px;
background-color: #596579;
color: #ffffff;
text-align: center;
font-weight: bold;
border-radius: 4px;
min-width: 0;
}
.grid-template-areas-demo .demo-header {
grid-area: header;
background-color: #2456a6;
}
.grid-template-areas-demo .demo-sidebar {
grid-area: sidebar;
}
.grid-template-areas-demo .demo-main {
grid-area: main;
}
.grid-template-areas-demo .demo-footer {
grid-area: footer;
background-color: #2456a6;
}
The grid-template-areas property defines the named regions, while grid-area assigns each item to its corresponding region.
The layout uses exactly three rows and two columns, with no unused tracks. The gap property provides spacing between the areas.
Change the Layout Arrangement
.grid-template-areas-demo {
grid-template-areas:
"header header"
"main sidebar"
"footer footer";
grid-template-columns: minmax(0, 2fr) minmax(90px, 1fr);
}
This revision places the main content on the left and the sidebar on the right without changing the HTML or the individual grid-area assignments.
Common Mistakes
Forgetting to Enable CSS Grid
.container {
grid-template-areas: "header" "main";
}
The declaration does not create a grid formatting context by itself. Use display: grid or display: inline-grid.
Using Different Numbers of Cells
.container {
grid-template-areas:
"header header"
"sidebar main extra";
}
Every row must contain the same number of cells.
Creating Nonrectangular Areas
.container {
grid-template-areas:
"main main"
"main sidebar";
}
The main area forms an L-shape, making the declaration invalid.
Using Different Area Names
.container {
grid-template-areas: "content";
}
.item {
grid-area: main;
}
The item references main, but the defined area is named content. The item will not occupy the intended named area.
Forgetting Quotation Marks
.container {
grid-template-areas: header header;
}
Area definitions must be written as quoted strings.
Using Commas Between Rows
.container {
grid-template-areas:
"header header",
"sidebar main";
}
Separate row strings with whitespace, not commas.
Assuming Unnamed Cells Stay Empty
A period creates an unnamed cell, but automatically placed items may still occupy it.
Confusing Area Names with Track Sizes
Use grid-template-columns and grid-template-rows to size tracks. The area strings define placement regions rather than dimensions.
Overlooking Visual and Source Order
Changing named areas can visually reorder items without changing their reading or keyboard navigation order. Keep the underlying HTML sequence meaningful.
Important Considerations
Keep the following points in mind when using grid-template-areas:
- Each quoted string represents one row of grid cells.
- Every row string must contain the same number of cells.
- Repeated area names must form a single rectangle.
- A period represents an unnamed grid cell.
- Area names are case-sensitive.
- The
grid-areaproperty assigns items to named regions. - Named areas automatically establish corresponding
-startand-endgrid line names. - The property can establish explicit grid tracks even when their sizes are not separately declared.
- Use
grid-template-columnsandgrid-template-rowsto control track sizes. - Use
gapto control spacing between tracks. - Media queries can redefine area arrangements for different screen sizes.
- The
grid-templateandgridshorthands can reset named area definitions. - Visual rearrangement does not change document or keyboard navigation order.
Browser Support
The CSS grid-template-areas property is widely available in modern browsers, including Chrome, Edge, Firefox, Safari, and Opera.
For current compatibility information, visit Can I Use? and search for grid-template-areas.
Named grid areas are part of standard CSS Grid Layout. Older browsers without modern CSS Grid support may require an alternative layout.
Specifications
The grid-template-areas property is defined in the CSS Grid Layout specification.
CSS Grid Layout Module Level 1: grid-template-areas
The specification defines named grid area syntax, rectangular area requirements, and the implicit grid line names generated from named areas.
CSS Grid Layout Module Level 2
For additional documentation and examples, consult MDN Web Docs.
