Stop placing grid cells by feel: a practical guide to CSS Grid layout, from container to areas
Grid layout is not about guessing where things land: it is lines, tracks, and named areas. This guide follows MDN's basic concepts of grid layout and grid template areas: containers and items, fr and repeat, explicit vs implicit grids, line-based placement, named areas, gap, overlap, and subgrid, with a five-category rework breakdown.

First, the boundaries of this guide. The site already has a guide on CSS custom properties, about how variables are defined, inherited, and used when switching themes — that one is about values, while this one is about position: how several blocks of content on a page sit up, down, left, and right, who takes how many columns, and who spans how many rows. There is also a guide on event delegation, about handling click events; this guide is not about events, only about layout. The "White Tower Bookstore" used below is a fictional practice project: the homepage of a small shop selling second-hand books, with a header, a sidebar, a book list, and a footer, whose author wants to change it from a single stacked column to a two-column layout with a sidebar. Every operation, code sample, and number built around it was invented to explain the technique, not a real record. All syntax, properties, and behavior of grids come from MDN's "Basic concepts of grid layout" and "Grid template areas" guides; see each article's meta information and evidence record for sources. MDN's own examples, such as the five-cell wrapper and the nine-column header-and-sidebar layout, are the documentation's examples, not real user data.
Step 1: Tell the container from the items — only then does the grid have somewhere to land
The first sentence of grid layout, as MDN writes it at the start of the basic concepts, is that a grid is a set of intersecting horizontal and vertical lines defining rows and columns, and elements can be placed onto the grid within these lines. Before doing anything, tell two roles apart. The grid container is the element declared as a grid; grid items are its direct children. Write display: grid on an element and it immediately becomes a grid container, and all its direct children immediately become grid items. Note the word "direct": grandchildren do not count — they still lay out in normal document flow, and only become items of another grid when they themselves are given display: grid.
Writing display: grid alone, with no track definitions, changes nothing visible on the page. MDN puts it plainly: that only creates a single-column grid, and five child elements still stack in one column, exactly as before. The difference is in the developer tools: next to the display: grid value in the browser's developer tools there is a small icon, and clicking it overlays the grid's lines on the page. That is the most important habit when learning grids: do not guess — open the grid inspector and look at the lines. Where the lines are, how wide the tracks are, and which cell each item lands in, the inspector shows at a glance, far more reliably than imagining it from code.
Step 2: Tracks are the gaps between rows and columns, and fr divides the leftover space by shares
Rows and columns in a grid are called tracks: the space between any two adjacent lines is one track. Tracks are defined with two properties: grid-template-columns for column tracks and grid-template-rows for row tracks. MDN's first example is three 200-pixel column tracks — grid-template-columns: 200px 200px 200px — so five child elements land one per cell, in three columns and two rows. Tracks can be written in any length unit — pixels, percentages, em — but grids also bring a new unit: fr. The fr unit represents a fraction of the available space in the grid container: grid-template-columns: 1fr 1fr 1fr is three equal tracks that grow and shrink with the container, while 2fr 1fr 1fr splits the available space into four shares, two for the first track and one each for the next two.
When fr is mixed with fixed widths, get the order of the arithmetic right: the fixed-width part is taken out of the container first, and only the remaining space is divided by shares. MDN's example is 500px 1fr 2fr: the 500 pixels are taken first, the remaining space is split into three shares, one for the 1fr track and two for the 2fr track. Thinking about it the other way around gets it wrong: fr always divides what is left after the fixed parts are removed, never the container's total width. This order comes up once more in the section on gap, whose space is also deducted before dividing.
Long track lists get wordy, and repeat helps. grid-template-columns: repeat(3, 1fr) is the same as 1fr 1fr 1fr; repeat can also repeat just one part of the list, as in 20px repeat(6, 1fr) 20px — one 20-pixel track, six 1fr tracks, and one more 20-pixel track, eight in total. It can also repeat a pattern, as in repeat(5, 1fr 2fr) — the 1fr, 2fr combination repeated five times, ten tracks in total. When writing tracks, first count how many you need, then decide whether repeat is worth it; with few tracks, writing them out directly is easier to read.
Step 3: The explicit grid is the one you draw; the implicit grid is the one content pushes out
The rows and columns defined with grid-template-columns and grid-template-rows make up the explicit grid; when content does not fit and spills past the explicit grid, the rows and columns the grid adds by itself make up the implicit grid. MDN gives a common case: only three columns are defined and no rows, so the first three items take the first row and the last two drop to a second row — that second row was generated by the implicit grid. Implicit tracks are auto-sized by default, meaning their size is influenced by both their content and the available free space: tall content makes them tall, little content makes them short.
The size of implicit tracks can be set: grid-auto-rows governs automatically generated rows and grid-auto-columns governs automatically generated columns. Writing grid-auto-rows: 200px makes every automatically created row 200 pixels tall. More often, minmax wraps it: grid-auto-rows: minmax(100px, auto) means automatically created rows are at least 100 pixels tall, and grow with content past that, up to the content's own height. In MDN's example, the second cell holds two paragraphs of text, taller than 100 pixels, so the whole row grows — row height is shared across the row, so when one cell grows taller, the entire row grows with it.
Step 4: Place items against lines — line numbers work better than cell numbers
When defining a grid, you define tracks; when placing items, you work against lines. MDN flags this point: the grid gives you numbered lines to use when positioning items, not numbered cells. A three-column, two-row grid has four column lines and three row lines. Lines are numbered according to the writing mode of the document: in a left-to-right language, line 1 is on the left-hand side; in a right-to-left language, it is on the right-hand side. Placing an item means saying which line it starts at and which line it ends at. In MDN's example, the first item has grid-column-start: 1 and grid-column-end: 4, spanning from column line 1 to column line 4 and taking the full width of the row, and grid-row-start: 1 and grid-row-end: 3, spanning two row tracks. The second item starts at column line 1, takes one track, and spans from row line 3 to row line 5, taking two rows.
The four longhand properties compress into two shorthands: grid-column for columns and grid-row for rows, with the start line before the forward slash and the end line after it. The item above becomes grid-column: 1 / 4 and grid-row: 1 / 3, meaning exactly the same thing in two fewer lines. When an item spans only one track, the end value can be omitted: grid-column: 1 means starting at line 1 and taking one track. Shorthands have another benefit: reading the code shows at a glance how many tracks an item spans — 1 / 4 is three tracks, 3 / 5 is two, and the difference between the numbers is the span.
Miscounting line numbers is the most common placement mistake, and it fails silently: writing grid-column-end as 3 when you meant three columns gives you only two, and the page is missing a block without any error. MDN's suggested remedies are twofold: use the grid inspector and count lines against the lines it overlays, and once there are many items, switch to the area-naming method in the next section — names are harder to miscount than numbers. Line-number placement suits layouts with few items in fixed positions; when items multiply and positions change often, it is time to switch to names.
Step 5: Name the areas, and write the whole page layout in one place
The grid-area property can take all four line numbers directly, as in grid-area: 1 / 1 / 4 / 2, meaning starting at row 1, column 1 and ending at row 4, column 2; or it can take a single name, such as grid-area: hd, with the container's grid-template-areas arranging the names into a picture. MDN's example is a nine-column layout: the header hd spans all nine columns, the sidebar sd spans three, the main content main spans six, and the footer ft spans all nine. Written out, the layout is the picture itself: three rows of strings in grid-template-areas, nine names per row, and the page looks the way the strings look. Naming the items by itself creates no layout — it only pins a name tag on each item; the container's grid-template-areas does the actual placing.
Area naming has three rules. First, empty cells are written with a full stop: one dot is one empty cell, so to put the footer directly under the main content and leave the three cells under the sidebar empty, write three dots in the first three positions of the third row. Second, every area must be rectangular — there is no way to create an L-shaped area, as MDN states plainly, though the specification notes a future level might provide it. Third, every row must have the same number of cells: rows that do not match, or areas that are not rectangular, make the whole grid-template-areas invalid and the property is ignored, so the layout falls back to automatic placement without any error — the hardest kind of failure to track down, where the page looks like the definition "did not work" when in fact one row has the wrong number of names.
The biggest benefit of area naming is that changing the layout means changing one place. In MDN's example, at narrow widths the container becomes one column and four rows, stacking hd, main, sd, and ft in turn; at 30em wide it returns to the nine-column, two-column layout; at 60em wide the footer moves to the right of the main content. The area names are defined outside the media queries, so hd is always called hd wherever it is placed — only its position in the picture changes. The fictional bookstore example fits exactly: on a phone the book list sits above the sidebar, on a wide screen the sidebar sits on the left and the list on the right, and the HTML does not move — two lines of grid-template-areas are enough. The same goes for a small media-object component: image on the left and text on the right, flipped by changing one area definition and one track definition, without touching the HTML.
Step 6: The space gap takes is deducted before fr gets its share
Gutters, or alleys, between grid cells are created with the column-gap and row-gap properties, or the gap shorthand, which takes both values at once. MDN's example is column-gap: 10px and row-gap: 1em — 10 pixels between columns and 1em between rows. Gap looks minor, but it has one easily missed rule of arithmetic: any space used by gaps is accounted for before space is assigned to the flexible fr tracks. MDN's own words are that gaps act for sizing purposes like a regular grid track, except that nothing can be placed into a gap; for line-based positioning, the gap acts like a thick, transparent line.
The practical consequence is this: when the tracks add up to less than the container width and the page still overflows, nine times out of ten the gap was forgotten. Three 1fr columns plus two 10-pixel gaps need room for three tracks plus 20 pixels, not just three tracks. The fix is simple: count the gap in the width, or look at the inspector, which marks the gap's width too. Another common mistake is writing gap on an item: gap is a container property and does nothing on an item. When row and column gaps differ, write gap with two values, such as gap: 10px 1em — the first value is the row gap and the second is the column gap; do not swap them.
Step 7: One cell can hold several items — use z-index to layer them when they overlap
The smallest unit on a grid is called a grid cell, conceptually like a table cell. Items can span one or more cells both by row and by column, and the range they cover is a grid area, which must be rectangular. MDN also covers a point many people do not know: more than one item can be placed into a grid cell, and they will overlap. That is when z-index controls the stacking order: the later item in source order displays on top by default, and changing z-index swaps them. In the docs' example, box1 and box2 overlap, box2 is on top because it comes later, and giving box1 z-index: 2 and box2 z-index: 1 flips the order.
Overlapping is not only an accident; it can be a deliberate design technique: a heading layered over an image, or a badge over a card, are both two items placed in the same cell with their alignment set. The trap to watch for is that items in a cell stretch to fill the whole cell by default, so two overlapping items that both stretch will cover each other. The fix is to set an alignment or an explicit size on the item being layered, rather than letting it stretch to fill. One more point: z-index only compares items inside the same cell — items in different cells are unrelated, so do not expect z-index to lay one cell's item over another cell.
Step 8: Nested grids mind their own lines — use subgrid when they must line up
A grid item can itself become a grid container: give it display: grid and a track definition of its own, and its children lay out on the new grid. MDN flags two things people take for granted. First, the nested grid has no relationship to the parent: it does not inherit the parent's gap, and its lines do not align with the parent's lines — each grid does its own arithmetic. Second, before nesting, those grandchildren do not participate in grid layout at all and display in normal document flow; only when their direct parent becomes a grid container do they get cells to land in.
To make a child grid share the parent's lines, use subgrid. Setting a child's grid-template-columns to subgrid makes it use the parent grid's track definition to lay out its own items. MDN's example changes box1's grid-template-columns: repeat(3, 1fr) to subgrid, and the three small items inside box1 then fall on the parent grid's tracks, lining up exactly with the items outside. Subgrid is for when alignment is needed; a nested grid that does not need to align can simply take a track definition of its own. When unsure which to use, ask one question: do the items inside need to line up with the items outside? If yes, subgrid; if no, ordinary nesting.
Step 9: Grids handle two dimensions, flexbox handles one — do not swap the jobs
Near the end of the basic concepts, MDN positions grids in one sentence: grid is a powerful layout method that, when combined with other parts of CSS such as flexbox, can help you create layouts that are responsive, flexible, and accessible. The implication is a division of labor: grids handle two-dimensional layout, flexbox handles one-dimensional layout. A page-level skeleton — how the header, sidebar, main content, and footer divide into rows and columns — is a two-dimensional problem, so use a grid; arranging a few buttons in a row, or aligning a piece of text with an icon, is a one-dimensional problem, so use flexbox. A one-dimensional problem can be forced into a grid, but it takes a pile of track definitions; a two-dimensional problem can be cobbled together with flexbox, but it takes several nested containers, and changing one place pulls at the rest. In the fictional bookstore example, the page skeleton uses a grid, while the logo and search box inside the header sit side by side with flexbox — each handles one part, and the code reads clearly too.
Step 10: Debugging a grid always starts with the inspector
The debugging method MDN mentions again and again is only one: the browser's grid inspector. Click the small icon next to the display: grid value, and the grid's lines, tracks, and gaps overlay on the page — which cell each item lands in, how many lines it spans, and how wide the gap is, all visible at a glance. A wrong placement, a miscounted track, or a non-rectangular area, the inspector points out directly, much faster than counting line numbers in code.
Step 11: Five failure scenes — check yourself against the chart

The chart above shows a fictional redesign of the White Tower Bookstore: 72 layout rework items in total. Tracks written in fixed pixels that do not fit narrow screens: 20, or 27.8%, the largest group; implicit row heights left unset, so rows jumped up and down as content grew: 16, or 22.2%; line numbers miscounted, so items landed in the wrong cells: 14, or 19.4%; areas put together non-rectangularly, so the whole definition was ignored: 12, or 16.7%; the space taken by gap forgotten, so tracks plus gap overflowed the container: 10, or 13.9%. 20 + 16 + 14 + 12 + 10 = 72, and the books balance. These numbers are invented examples, meant to show at a glance that the first two groups together are exactly half — and that they are the same mistake: drawing only the desktop layout without thinking about what happens on narrow screens or when content grows.
Checking yourself against the chart means asking five questions in turn. First, are there any fixed-pixel tracks that could be fr or minmax instead? Second, is grid-auto-rows set, and will row heights stay under control when content grows? Third, were the line numbers counted against the inspector when placing items, or written by feel? Fourth, does every row of grid-template-areas have the same number of names, and is every area rectangular? Fifth, when working out widths, was the space taken by gap counted too? If all five can be answered, the lessons of this guide have landed.
Checklist: after writing a grid layout, tick each item
First, container and items are told apart: display: grid goes on the container, the items are its direct children, and grandchildren do not take part. Second, track definitions are complete: columns with grid-template-columns, rows with grid-template-rows, and implicit rows covered by grid-auto-rows, preferably wrapped in minmax. Third, fr divides what is left after fixed widths and gaps are deducted, not the container's total width. Fourth, placement works against lines: grid-column: 1 / 4 runs from line 1 to line 4, spanning three tracks, and the difference between the numbers is the span. Fifth, when there are many items or positions change often, switch to named areas: areas must be rectangular, every row must have the same number of names, and empty cells are written with a full stop. Sixth, gap goes on the container, and its space counts in the width. Seventh, overlapping items are layered with z-index, and their alignment is set first so they do not stretch to fill and cover each other. Eighth, nested grids mind their own lines, and subgrid is only for when they must align with the parent grid. Ninth, two-dimensional skeletons use grids and one-dimensional arrangements use flexbox — do not swap the jobs. Tenth, debugging starts with the browser grid inspector, counting lines, checking cells, and looking at gaps. If any item cannot be answered, go back to its section and read it again — that is worth more than memorizing ten more properties.
Comments (0)
Sources
Related articles
localStorage only stores strings and has no indexes. IndexedDB is a real database in the browser: structured data, far larger capacity, indexes, and transactions. Following MDN's Using IndexedDB, IDBDatabase, and IDBObjectStore: the five-step routine, opening, upgrades, keys, indexes, transactions, add versus put, get, cursors, ranges, and multi-tab upgrades.
You already call functions every day; this guide names what you have been doing. Following MDN's Functions, Arrow function expressions, and Rest parameters: declarations versus expressions, parameters and arguments, default values, rest parameters, spread syntax, return values, arrow function shorthand, the this trap, callbacks, and scope.
One page should stand up on a phone, a tablet, and a computer. This guide follows MDN's Responsive web design, Using media queries, and the viewport meta reference: fluid HTML, the viewport tag, fluid grids and max-width, images that never burst their container, media query syntax, breakpoints set by content, mobile first, responsive type, testing capability instead of width, and a pre-launch checklist.