Grid placement algorithm
Definition and purpose
Grid placement determines the area occupied by each item before track sizing. The explicit grid comes from the template; implicit tracks are created when placement needs more lines. For row auto-flow, fully definite positions are handled first, then items locked to a row, then remaining items are placed using an auto-placement cursor.
Sparse placement
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #parent { display: grid; width: 180px; grid-template-columns: repeat(3, 60px); grid-auto-rows: 30px; } #parent > div { background: #dbeafe; } #a { grid-area: 1 / 2 / 2 / 4; } #b { grid-column: span 2; } </style> </head> <body> <div id="parent"> <div id="a">A</div> <div id="b">B</div> <div id="c">C</div> <div id="d">D</div> </div> </body></html>The earlier hole remains unoccupied.
Syntax and mental model
In a three-column row, put A at columns 2 through 3 of row 1, and give B a span of two columns. B cannot fit in the one-cell hole at row 1 column 1, so it moves to row 2. Sparse auto-placement continues forward: C goes in row 2 column 3. Dense mode restarts its search to fill earlier holes when possible, so C can occupy row 1 column 1. Column auto-flow uses the transposed procedure. Placement follows order-modified document order, not arbitrary visual reading order.
Dense placement
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> body { margin: 0; font: 16px sans-serif; color: #172033; background: white; } #parent { display: grid; width: 180px; grid-template-columns: repeat(3, 60px); grid-auto-rows: 30px; } #parent > div { background: #dbeafe; } #parent { grid-auto-flow: row dense; } #a { grid-area: 1 / 2 / 2 / 4; } #b { grid-column: span 2; } </style> </head> <body> <div id="parent"> <div id="a">A</div> <div id="b">B</div> <div id="c">C</div> <div id="d">D</div> </div> </body></html>C fills the earlier hole; D follows in row 2.
Common mistakes
Do not use dense packing to impose a meaningful reading order. Keyboard and DOM order need not follow the visual result. Explicit placement can intentionally overlap items, unlike the search for free auto-placement cells.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Flexible length resolution. Next lesson: Grid track sizing algorithm.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Grid placement algorithm — knowledge check
Five questions on grid placement algorithm.
When does placement happen relative to track sizing?
Which mode revisits earlier holes?
Where does C go in the sparse example?
What creates implicit tracks?
Does dense layout change DOM order?