Implicit vs Explicit Grid
The explicit grid is what you define with grid-template-*. The implicit grid is what the browser generates when items overflow your defined tracks. Understanding this distinction is crucial.
1. The Explicit Grid
Tracks you define explicitly with grid-template-columns and grid-template-rows form the explicit grid. These tracks have the sizes you specify.
/* Explicit grid: 3 cols Γ 2 rows = 6 cells */ .container { grid-template-columns: 200px 1fr 100px; grid-template-rows: 60px 80px; } /* Explicit grid ends at row 3, col 4 */
2. The Implicit Grid β Overflow Items
Add more items than your grid can hold. Extra items are placed in automatically generated (implicit) tracks. These have default size auto (sized by content).
/* Only 1 explicit row defined */ .container { grid-template-columns: repeat(3, 1fr); grid-template-rows: 60px; /* 1 explicit row */ } /* Items 4-6 create implicit rows */ /* Implicit rows height = auto */
3. grid-auto-rows Controls Implicit Rows
Use grid-auto-rows to give implicit rows a specific size. Without it, they're sized by their content (auto).
/* Implicit rows sized by content (cramped): */ .no-auto { grid-template-columns: repeat(2, 1fr); /* no grid-auto-rows β height = auto */ } /* Implicit rows have fixed height: */ .with-auto { grid-template-columns: repeat(2, 1fr); grid-auto-rows: 80px; /* implicit rows = 80px */ }
4. Mixing Explicit & Implicit Track Sizes
You can define some explicit rows and let the rest be implicit. The explicit rows use your defined sizes; implicit rows use grid-auto-rows.
.container {
grid-template-columns: repeat(3, 1fr);
/* 2 explicit rows with specific heights: */
grid-template-rows: 100px 60px;
/* all rows after row 2 will be 40px: */
grid-auto-rows: 40px;
}5. Implicit Columns (column overflow)
Items can also create implicit columns β for example, when you explicitly place an item beyond your defined column count, or when using grid-auto-flow: column.
.container {
grid-template-columns: 1fr 1fr; /* 2 explicit cols */
grid-auto-columns: 80px; /* implicit cols width */
}
/* placing item at col 3 creates an implicit column */
.item-c { grid-column: 3; }
.item-d { grid-column: 4; }