Grid-template-areas maps named cells to positions in an explicit grid. A grid item chooses an area with grid-area; the naming affects visual placement, not source order.
CSS grid template areas: name regions without rewriting document order
When to use it
A review page has a primary record and a secondary tools rail. At a narrow width, the record comes first. At a wide width, the rail can sit beside it, yet keyboard and screen-reader order still follow the HTML. Name the regions to make the stylesheet easy to inspect, and keep the area map rectangular. The sample gives the main region the flexible track and the rail a bounded width. If the rail contains a required approval action that logically belongs before the record, moving it visually will not fix the document; change the markup contract instead. Inspect the visual and focus sequences at both breakpoints, and test long tool names that might exceed the rail width.
.review-layout { display: grid; grid-template-areas: "record" "tools"; gap: 1.5rem; }
.review-layout__record { grid-area: record; min-inline-size: 0; }
.review-layout__tools { grid-area: tools; }
@media (min-width: 60rem) {
.review-layout { grid-template-columns: minmax(0, 1fr) 16rem; grid-template-areas: "record tools"; }
}Cost and verification
The extra area names are a maintenance aid, not a performance feature. Grid layout work grows with the number of tracks and items, but a two-region page has little overhead. The real risk is a visually convenient area map that contradicts logical reading order. Check keyboard traversal after changing the map. An unused named area or misspelled grid-area can leave an item auto-placed, so inspect every breakpoint rather than trusting the source text. Keep essential content in the record region and bound the rail's content so it can wrap without horizontal overflow.
Common Mistakes
- Do not use an area map to conceal a wrong HTML sequence.
- Do not leave a grid-area name absent from a breakpoint map.
- Do not give the main track a fixed width that breaks at zoom.
