We have some mockup here for a page. Let's start making a grid layout from it. We want display-grid on the container, a 10px gutter. It will take up all the available height and will show up the grid items so that they're easier to see.
We're going to use the grid-template-areas property to define how our grid looks. Firstly, we're going to set up a grid-area value on each of our grid items. This is so that they can be referred to in grid-template-areas. Let's see that in action.
Let's say we want a two-column layout. The header is going to take up two columns. The aside is going to be on the side. The section is going to take up most of the room underneath the header. The footer will also take up two columns.
We can visualize how it's going to look. Now, we can easily describe our visualization using grid-template-areas. We want the header to take up two columns, aside and section next to each other, and footer to take up two columns. There we have our grid layout.
Let's set up some grid-template-rows so that our aside and section take up more vertical space. We'll specify some grid-template-columns too. There we have a super easy way to define how our grid is going to look. It would be very easy for anyone to come along and see what this is going to look like straight off just from glancing at it.
Let's say we wanted to make it responsive. We'll set up a media query. For 400px and above, we're going to put the grid-template-columns, grid-template-rows, and grid-template-areas that we just defined. For under 400px, we'll set up another grid-template-areas property.
Let's say on mobile we want the header, then the section, then the aside, and then the footer in a one-column layout. We need to do header, section, aside, and footer. Now, when we go down to below 400px, we have the grid that we specified in this grid-template-areas.
It looks like it needs some grid-template-rows to make the section part bigger. There we have it. The values that we're defining in our grid-template-areas correspond to the grid-area setting that we set up in each of our grid items.
Let's see what's going on under the hood. If we look at our header, for example, we can see the grid-area setting here. We can see that it's actually just shorthand for grid-row-start, grid-column-start, grid-row-end, and grid-column-end.
We can actually refer to these grid lines on grid items. We could suffix them with start or end. If we specified grid-row-start: header-start and grid-row-end: section-end, we would have a
that overlaid our header and our section.
Another thing to note is that we've changed the order of how our items appear on our mobile layout, but they haven't changed in the DOM. In the DOM, we have header, aside, section, footer. On our grid, we have header, section, aside, footer. However, if we look down here, we can see that the order is how we specified it in our HTML.
One final thing to point out is that with grid-template-areas we could also specify that we want an empty space to show instead of a grid-area. We could do that with a period. Now we have an empty space down here. We could give it any number of periods, and it would do the same thing as long as there's no spaces between the periods.
Also, we could have our grid-areas span across rows. If we wanted the aside to carry on all the way down here, and we wanted the section to carry on all the way to the top, we could do that. We could place the aside across here.
Now, it's taking up the entire area below the header. We could stick the section here. Now the header is squashed into here. The section is taking up this space.