CSS grid-template-areas

View Output Full Screen Preview

The CSS grid-template-areas property specifies named grid areas within a CSS grid.

If your browser supports CSS grids, the above example should look like this:

Screenshot of a grid created with the 'grid-template-areas' property.
A grid with 5 named grid areas.

Here's an explanation of how grid-template-areas is used in the above example.

The grid-template-areas allows us to use a kind of "ASCII art" to create each named grid area within our grids. You simply write out the grid exactly how it should appear in the browser. You do this using string values across one or more lines. Each line represents a row, and each cell within the string represents a column.

You can repeat cell names to create a grid area that takes up multiple cells. This is how we did the header, footer, and nav area in the above example.

So we can easily see from the above code that the grid contains 3 rows, 3 columns, 9 grid cells, and 5 named grid areas.

Named Lines

The grid-template-areas property creates implicit named lines from the grid areas in the template. These named lines take their names from each named grid area in the grid, but with a -start or -end appended to the name.

So for each named grid area named header for example, four implicit named lines are created. Two named header-start name the row-start and column-start lines of the named grid area, and two named header-end name the row-end and column-end lines of the named grid area.

As with most things with CSS grids, named lines can be either explicit or implicit.

Syntax

Here's the official syntax of the grid-template-areas property:

Possible Values

none
Specifies that no named grid areas or explicit grid tracks are created by this property (however, explicit grid tracks could still be created by the grid-template-rows and grid-template-columns properties).
string+

Specifies the named grid areas as a series of string values. A row is created for each string. Each string contains a space-separated list of cell names. A column is created for each cell in the string.

All strings must have the same number of columns. If a named grid area spans multiple grid cells, but those cells do not form a single filled-in rectangle, the declaration is invalid.

In addition, all CSS properties also accept the following CSS-wide keyword values as the sole component of their property value:

initial
Represents the value specified as the property's initial value.
inherit
Represents the computed value of the property on the element's parent.
unset
This value acts as either inherit or initial, depending on whether the property is inherited or not. In other words, it sets all properties to their parent value if they are inheritable or to their initial value if not inheritable.

Basic Property Information

Initial Value
none
Applies To
Grid containers
Inherited?
No
Media
Visual
Animation type
Discrete (see example)

CSS Specifications

Browser Support

Browser support for CSS properties can vary, particularly for newer or experimental features. Check current browser compatibility before using a property in production, especially when supporting older browsers.

The following table provided by Caniuse.com shows the level of browser support for this feature.

Initial Value

CSS properties have an initial value that applies when the property has not been explicitly set. The initial value is different from the value inherited from a parent element and may vary between properties.

Inheritance

Some CSS properties inherit their computed value from their parent element by default, while others do not. The inherit, initial, unset, and revert keywords can also be used to control how a property gets its value.

Vendor Prefixes

Most modern CSS properties use the standard, unprefixed syntax. Vendor-prefixed properties such as -webkit- are mainly relevant to older browser versions or specific features and should only be used when required by your browser support targets.