.grid-item {\n grid-column: 1 \/ 3; \/* start at grid column line 1 and span to line 3 *\/\n}<\/code><\/pre>\n\n\n\nBut grid item line numbers can change if our layout changes at a breakpoint. In those cases, it’s not like we can rely on the same line numbers we used at a specific breakpoint. This is where it takes extra cognitive encumbrance to understand the code.<\/p>\n\n\n\n
That’s why I think an ASCII-based approach works best. We can redefine the layout for each breakpoint using grid-template-areas<\/code> within the grid container, which offers a convenient visual for how the layout will look directly in the CSS \u2014 it’s like self-documented code!<\/p>\n\n\n\n.grid {\n grid-template-areas:\n \"head head\"\n \"nav main\"\n \"foot ....\"; \/* much easier way to see the grid! *\/\n}\n\n.grid-item {\n grid-area: foot; \/* much easier to place the item! *\/\n}<\/code><\/pre>\n\n\n\nWe can actually see a grid’s line numbers and grid areas in DevTools. In Firefox, for example, go to the Layout panel. Then, under the Grid<\/strong> tab, locate the “Grid display settings”<\/strong> and enable the “Display line number”<\/strong> and “Display area names”<\/strong> options.<\/p>\n\n\n\n <\/figure>\n\n\n\nThis ASCII approach using named areas requires a lot less effort to visualize and easily find the placement of elements.<\/p>\n\n\n\n <\/figure>\n\n\nLet’s look at the “universal” use case<\/h3>\n\n\n Whenever I see a tutorial on named grid areas, the common example is generally some layout pattern containing header<\/code>, main<\/code>, sidebar<\/code>, and footer<\/code> areas. I like to think of this as the “universal” use case since it casts such a wide net.<\/p>\n\n\n\n <\/figure>\n\n\n\nIt’s a great example to illustrate how grid-template-areas<\/code> works, but a real-life implementation usually involves media queries set to change the layout at certain viewport widths. Rather than having to re-declare grid-area<\/code> on each grid item at each breakpoint to re-position everything, we can use grid-template-areas<\/code> to “respond” to the breakpoint instead \u2014 and get a nice visual of the layout at each breakpoint in the process!<\/p>\n\n\n\nBefore defining the layout, let’s assign an ident to each element using the grid-area<\/code> property as a base style.<\/p>\n\n\n\nheader {\n grid-area: head;\n}\n\n.left-side {\n grid-area: left;\n}\n\nmain {\n grid-area: main;\n}\n\n.right-side {\n grid-area: right;\n}\n\nfooter {\n grid-area: foot;\n}<\/code><\/pre>\n\n\n\nNow, let’s define the layout again as a base style. We’re going with a mobile-first approach so that things will stack by default:<\/p>\n\n\n\n
.grid-container {\n display: grid;\n grid-template-areas:\n \"head\"\n \"left\"\n \"main\"\n \"right\"\n \"foot\";\n}<\/code><\/pre>\n\n\n\n <\/figure>\n\n\n\nEach grid item is auto-sized<\/strong> in this configuration \u2014 which seems a little bit weird \u2014 so we can set min-height: 100vh<\/code> on the grid container to give us more room to work with:<\/p>\n\n\n\n.grid-container {\n display: grid;\n grid-template-areas:\n \"head\"\n \"left\"\n \"main\"\n \"right\"\n \"foot\";\n min-height: 100vh;\n}<\/code><\/pre>\n\n\n\n <\/figure>\n\n\n\nNow let’s say we want the main<\/code> element to sit to the right of the stacked left<\/code> and right<\/code> sidebars when we get to a slightly wider viewport width. We re-declare grid-template-areas<\/code> with an updated ASCII layout to get that:<\/p>\n\n\n\n@media (min-width: 800px) {\n .parent {\n grid-template-columns: 0.5fr 1fr;\n grid-template-rows: 100px 1fr 1fr 100px;\n grid-template-areas:\n \"head head\"\n \"left main\"\n \"right main\"\n \"foot foot\";\n }\n}<\/code><\/pre>\n\n\n\nI tossed some column and row sizing in there purely for aesthetics.<\/p>\n\n\n\n <\/figure>\n\n\n\nAs the browser gets even wider, we may want to change the layout again, so that main<\/code> is sandwiched between the left<\/code> and right<\/code> sidebars. Let’s write the layout visually!<\/p>\n\n\n\n.grid-container {\n grid-template-columns: 200px 1fr 200px; \/* again, just for sizing *\/\n grid-template-areas:\n \"head head head\"\n \"left main right\"\n \"left main right\"\n \"foot foot foot\";\n}<\/code><\/pre>\n\n\n\n <\/figure>\n\n\n\n