grid-template-areas Property in CSS Grid Layouts
grid-template-areas specify the area where you want to place named grid items within a grid container.
Example 1: How to Place a Named Grid Item across Three Columns
The snippet above used the grid-template-areas
property to place grid-item1
across the first three column areas.
Note the following:
- Quotation marks (
""
) define each grid row. - A period symbol (
.
) defines an unnamed grid item. - We used the whitespace character to separate grid columns.
Example 2: How to Specify Multiple Named Grid Items’ Placements
The snippet above used the grid-template-areas
property to specify where browsers should place the grid items across the rows and columns of the grid container.
Important Stuff to Know about the grid-template-areas
Property
Here are four essential facts to remember when using the grid-template-areas
property:
1. grid-template-areas
do not permit empty cells
The grid-template-areas
property requires you to provide an item for all grid cells.
For instance, consider this snippet:
Above is an invalid grid-template-areas
value because the first row is incomplete.
In other words, the first row is the only one with two columns. However, grid-template-areas
expect all the rows in a grid container to have the same number of columns.
2. You can use dots to specify empty cells
Suppose you wish to leave some cells empty. In that case, use a dot (.
) or multiple unspaced dots (....
).
Here’s an example:
The snippet above used the three spaced dot (.
) symbols to indicate three empty cells.
3. grid-template-areas
do not permit placing an item in multiple locations
The grid-template-areas
property cannot place items twice within a grid container.
For instance, consider this snippet:
Above is an invalid grid-template-areas
value because the header
item occupies two grid areas.
4. grid-template-areas
allow rectangular areas only
The grid-template-areas
property cannot create non-rectangular areas (such as T-shaped or L-shaped).
For instance, consider this snippet:
Above is an invalid grid-template-areas
value because the ads1
item creates a non-rectangular grid area.