CSS Grid Layout - Understanding Grid Template Areas
Published by Josh Vogt on
An introduciton on the basics of grid-template-areas in CSS Grid layout. It's one the most powerful features in the spec.
This article references the new CSS Grid Layout Module. More specifically, it covers the grid-template-areas property. It is a powerfuly property that lets you visualize your grid in your CSS and explictly place grid items in a named area.
Defining a simple grid.
As simple grid layout could be a page with a sidebar and large content container surronded by a header and footer.The basic markup would consist of something like this:
The header
and footer
will span the full width of the container while the sidebar will take up about a 25% of the container and the content will fill the rest. To express this with grid-template-areas
I’ll need to apply the following CSS the the grid-container
:
- Set the
display
property togrid
. - Use the
grid-template-columns
property to set the number of coumns and define their width (in this exampe I’m using the new fr unit to set the width of the first column to 1/4 of the available width and content section to 3/4 of the available width. - The
grid-template-rows
property can be used to define the height of the rows. In this example theheader
andfooter
will take 4rem and 3rem respectivly and the body content will take up the remaining available space. - The
grid-gap
property defines the grid’s gutters and is shorthand forgrid-column-gap
andgrid-row-gap
. - This is the fun part.
grid-template-areas
defines the areas available in the grid. The name given to grid areas are arbitrary but should be sensible. Thegrid-template-areas
also provides a crude visual representation of the page layout.
Child elements of a grid are assigned to their location using the grid-area
property on the child elements. This is a very simple example but it already demonstrates the power the CSS Grid Layout. Simply changing the name a classes grid-area
can drastically change the layout.
Changing the grid-area
in the CodePen below can really mess with the layout. In a good way.
See the Pen Basic CSS Grid Layout by Josh Vogt (@josh_vogt) on CodePen.
Creating a nested grid.
CSS Grid Layout Level 1 was initially meant to include subgrid layout as well but as Rachel Andrew has pointed out, this have been moved to Level 2 of the CSS Grid specification. However, creating a nested grid is a simple as applying display: grid;
to an element whose parent is a grid container. In this example the simple layout above will have its content
grid-area contain its own two-column grid.
The nested grid will now contain a two-column grid of items, each column will take 50% of available space.
See the Pen Basic CSS Grid Layout with nested grid by Josh Vogt (@josh_vogt) on CodePen.
Learn more about CSS Grid Layout.
The two easiest things to do if you want to learn more about CSS Grid Layout is follow Rachel Andrew and Jen Simmons on Twitter and read their blogs.
Other Resources.
Corrections or comment can be directed to my twitter account @jshvgt.
Send a response
If you've written a response to this post enter the url of your post and send it over.