Specify grid columns, rows, and areas at once with the grid-template shorthand

Rory Smith
InstructorRory Smith
Share this video with your friends

Social Share Links

Send Tweet
Published 8 years ago
Updated 6 years ago

We can specify grid columns, rows, and areas in one property using the grid-template shorthand.

[00:00] In our markup, we have three navs in a section, inside a container div, and in our CSS, our container is display-grid with a 20-pixel gutter, and it's taking up all the available height. Let's save it in this layout. We're going to have a small row on the top, and a large row underneath.

[00:18] In the small row, menu one and menu two will sit next to each other. Menu three will span both rows, and it will act as a sidebar, and main will take up the rest of the space.

[00:31] Let's set up some grid-template-areas. Now, we can map out what we just described. We can say nav1, nav2, nav3 across the top row, and underneath main, main, nav3. We'll need to width that up to each individual element using grid-area.

[01:12] There's our basic layout. Let's set up some grid-template-rows for it. The top grid line can be called headerTop. The header is going to be 1fr tall. The next grid line will be headerBottom, and it will be the same as mainTop.

[01:31] The main section will be 5fr, and mainBottom will be the name of the bottom grid line. HeaderTop is this top grid line, headerBottom and mainTop is here, and mainBottom is this grid line down here.

[01:55] Finally, let's set up some grid-template-columns. The first column can be 2fr, the second can be taking up the available space, and the side-bar column can be 1fr. Here we have a grid layout which is using grid-template-areas, grid-template-rows, and grid-template-columns.

[02:20] If we wanted to, we could specify all three of these in one shorthand property called grid-template. Grid-template acts as an ASCII art visualization of what our grid's going to look like.

[02:34] Let's write a grid-template property. We'll start with our grid-template-areas, then we'll insert our grid-template-rows, and lastly our grid-template-columns. Now we have the same layout but it's specified a shorthand way. If we look under the hood here, we can see all our values being applied separately.

[03:21] If we wanted to, we could also just pass grid-template-rows and grid-template-columns to our grid-template property if we didn't want to specify any grid-template-areas.

egghead
egghead
~ 16 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today