CSS · Lesson 8 of 12 · 3 min
Advanced CSS Grid & Area Layouts
This shows you can build readable, maintainable page-level layouts, not just position boxes.
With grid-template-areas, you name the regions of your grid and assign elements to those names. In an interview, this signals you think about layout architecture and code clarity, not just the mechanics of grid-column and grid-row. It's the difference between giving turn-by-turn directions and showing someone a map.
Describe the layout, don't just place items
The grid-template-areas property on a grid container lets you draw your layout with named strings. Each string is a row, and the words in it are the names of the areas.
It looks like ASCII art, and that’s its strength. Anyone reading the CSS can immediately visualize the grid's structure without inspecting the HTML or counting grid lines.
The syntax is strict. Each string must have the same number of columns. Use a . (dot) to represent an empty cell. All cells for a single named area must form a single, solid rectangle. You can't create L-shaped or disconnected areas.
Connect content to areas
Once you've defined the named areas on the container, you assign each child element to an area using the grid-area property.
This decouples the element's source order from its visual position. The <header> can appear anywhere in the HTML; as long as it has grid-area: header, it will go to the region you named "header".
.container {
display: grid;
/* Define track sizes first */
grid-template-columns: 200px 1fr 150px;
grid-template-rows: auto 1fr auto;
gap: 10px;
/* Now, map the layout */
grid-template-areas:
"header header header"
"nav main sidebar"
"footer footer footer";
}
.header { grid-area: header; }
.nav { grid-area: nav; }
.main { grid-area: main; }
.sidebar { grid-area: sidebar; }
.footer { grid-area: footer; }
The trap: If you forget to assign grid-area to an item, or you have a typo in the name, the item will be placed automatically according to the default grid flow. This usually means it piles up in the first available empty cells at the start of the grid, breaking your intended layout.
Redefine the entire layout in one place
The real power of grid-template-areas is in responsive design. To change the entire page structure, you only need to redefine the grid-template-areas and track properties on the container. The child elements don't need to be touched.
@media (max-width: 768px) {
.container {
/* Switch to a single-column layout */
grid-template-columns: 1fr;
grid-template-rows: auto; /* Let content define row height */
/* Redraw the layout map for mobile */
grid-template-areas:
"header"
"nav"
"main"
"sidebar"
"footer";
}
}
The same elements (.nav, .main, etc.) automatically flow into their new positions because their grid-area name now maps to a different location in the grid's new structure.
In an interview, contrast this with older methods. You're not adding or removing classes, or changing positioning properties on multiple elements. You are changing the layout's blueprint in one place. This is less complex, less prone to error, and far more maintainable.
Watch out for the source order
Visually reordering elements does not change the DOM order. This is a major accessibility concern.
Screen readers read the DOM, not the screen. The tab order for keyboard navigation also follows the DOM. If your HTML source is main, nav, footer, but your grid-template-areas puts the nav visually at the top, a keyboard user will tab through the entire main content before reaching the navigation.
The trap: A layout that looks correct but is illogical or unusable for non-mouse users. Always test your layouts with a keyboard. If visual order must differ significantly from a logical source order, you may need to reconsider the design or use ARIA attributes to guide assistive technologies, but a logical DOM order is always the best foundation.
Try it
Create a 2x2 grid layout for four items using grid-template-areas. Then, write a media query that rearranges it into a single 4-row column for mobile screens. Open the page and use the Tab key to navigate through the items on both desktop and mobile layouts. Notice how the tab order doesn't change.
Next: Responsive Design Strategies & Mobile First.
Lock it in — apply this lesson under interview conditions.