Grid Areas and Named Lines
How to describe a layout as a picture with grid-template-areas, place items by name, and use named lines for layouts like a full-bleed column.
Grid Areas and Named Lines
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `grid-template-areas` lets you draw the layout in strings: each string is a row, each word a cell. Place an item with `grid-area: name`. Repeated names make an area that spans cells.
- Areas must be rectangles. An L-shaped or split name makes the whole declaration invalid, and the property computes to `none`. Use `.` for an empty cell.
- Declaring an area also creates implicit named lines: area `main` gives you `main-start` and `main-end`, which you can use in `grid-column` and `grid-row`.
- You can name lines yourself inside `grid-template-columns`, like `[full-start] 1fr [content-start] minmax(0, 60ch) [content-end] 1fr [full-end]`. Items then choose `grid-column: content` or `grid-column: full`.
- `grid-area: row-start / column-start / row-end / column-end` is the numeric shorthand. It runs in the opposite order to the `top right bottom left` habit, so it's easy to get wrong.
Placing items with numbers gets old
Line numbers work, but grid-column: 2 / 4 says nothing about what the item is, and inserting a column changes every number after it. Naming things fixes both. You can name the regions of the grid, or name the lines between tracks.
grid-template-areas: draw the layout
You write the template as a picture. Each quoted string is a row and each word is a cell:
.page {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"head head"
"side main"
"foot foot";
}
.page > header { grid-area: head; }
.page > nav { grid-area: side; }
.page > main { grid-area: main; }
.page > footer { grid-area: foot; }
A name that appears in several adjacent cells covers all of them, so head spans both columns. You can read the layout from the CSS, and responsive changes are a matter of redrawing the picture inside a media query. The tracks themselves are still sized by grid-template-columns and grid-template-rows, so the areas are just labels on the cells those create.
Two rules apply. Every row must have the same number of cells, and every named area must be a rectangle. Browsers drop the whole declaration for an L-shape or a split area. In a test, "a a" "a b" "c" (uneven rows) and "a b a" (split area) both computed to none, while a valid two-by-two template was kept. Use a . for an empty cell.
Areas are often used for bento-style dashboards, where one tile spans two rows and another two columns, and this video’s subject is exactly that.
Implicit lines from areas
Declaring an area also names the lines around it. Area main gives you lines main-start and main-end on each axis, so you can span by name:
.banner { grid-column: side-start / main-end; } /* from the sidebar's left to main's right */
In a test, that placement with a template of "side main" spanned the full 800px width. This is useful for something that crosses areas, like a banner stretching across two named regions.
Naming lines yourself
You can also write line names in square brackets inside the track list. Then items refer to them directly:
.wrapper {
display: grid;
grid-template-columns:
[full-start] minmax(1rem, 1fr)
[content-start] minmax(0, 600px)
[content-end] minmax(1rem, 1fr)
[full-end];
}
.wrapper > * { grid-column: content; } /* the middle track */
.wrapper > .wide { grid-column: full; } /* edge to edge */
grid-column: content is shorthand for content-start / content-end. A reading column that is at most 600px wide and centered, with images that bleed to the edges, is one of the most useful layouts in editorial design, and without named lines it needs negative margins or extra wrappers. In a test with a 1000px wrapper, content items were 600px wide starting at x = 200, and .wide items ran the full 1000px.
Several lines can share a name, and an item can use span with it: grid-column: span 2 / content-end.
The numeric grid-area shorthand
grid-area has a second form with four numbers or names:
.item { grid-area: 1 / 2 / 3 / 4; } /* row-start / column-start / row-end / column-end */
It is the same as grid-row: 1 / 3; grid-column: 2 / 4;. The order is not the top right bottom left order of margins, so people often mix it up. Spelling out grid-row and grid-column is usually clearer.
What the video covers
Coding2GO’s “Learn GRID-AREA to Build Amazing BENTO GRIDS” (February 2025, about six minutes) has these chapters: introduction to bento grids, setting up the grid, using grid template areas, responsive design techniques, using implicit grids, and handling content and styling. Its description says it combines areas with media queries and implicit grids. I could not read a transcript, so I do not describe the demos. The video says nothing about named lines in its title or chapter list, so the line-naming sections here are the prose’s own. The runner-up covers overlapping named lines.
Where this fits
Areas and named lines label the tracks from grid fundamentals and tracks. Items you don’t place by name or number get placed by the algorithm in auto-placement and responsive grids, and subgrid later lets a nested grid share these named lines.
Check yourself
Why does `grid-template-areas: 'a b a'` not work?
Area `a` is split into two non-adjacent cells, and areas must form a rectangle. The browser discards the whole declaration, so the property computes to `none` and no areas exist.
How do you leave a cell empty in `grid-template-areas`?
Write a `.` (one or more periods, no spaces between them) for that cell, as in `"header header" "sidebar ."`. The cell stays part of the grid but has no named area.
A `.page` grid has `grid-template-areas: 'side main'`. What does `grid-column: side-start / main-end` do?
It places the item from the left edge of the `side` area to the right edge of the `main` area, spanning both. Area names create implicit `-start` and `-end` lines, which any placement property can use.
How does a full-bleed layout use named lines?
You name lines in the column template: `[full-start] minmax(1rem, 1fr) [content-start] minmax(0, 600px) [content-end] minmax(1rem, 1fr) [full-end]`. Most children get `grid-column: content`, which spans the middle track, and an image gets `grid-column: full` to run edge to edge.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.