Explicit and implicit grids
<div class="metrics">
<div class="metric">Visitors</div>
<div class="metric">Orders</div>
<div class="metric">Revenue</div>
<div class="metric">Returns</div>
</div>.metrics {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
grid-auto-rows: minmax(5rem, auto);
gap: 1rem;
}
Direct in-flow children of a grid container become grid items. grid-template-columns and grid-template-rows define explicit tracks; the browser generates implicit tracks when more space is needed. Tracks are spaces between grid lines, and a cell is the intersection of a row and a column. [1][2]
Four metrics occupy three columns and two rows in this example. grid-auto-rows: minmax(5rem, auto) gives implicit rows a 5rem minimum while letting them grow. gap separates tracks without adding an outer gutter.
How the fr unit is calculated
<div class="track-demo">
<div>Navigation</div>
<div>Content</div>
<div>Details</div>
</div>.track-demo {
display: grid;
inline-size: 900px;
grid-template-columns: 240px minmax(0, 1fr) minmax(0, 2fr);
gap: 24px;
}
An fr is a fraction of the space available to flexible tracks, not a percentage of the entire container. At 900px, subtract a 240px fixed track and two 24px gaps to leave 612px. Tracks with 1fr and 2fr receive 204px and 408px respectively when no other constraints intervene. [2][3]
A standalone 1fr has an automatic minimum. Consequently, 1fr 1fr can produce unequal columns if long unbreakable content raises one track's minimum. minmax(0, 1fr) sets the track minimum to zero, but does not automatically make its content wrap.
Responsive columns: auto-fit and auto-fill
<div class="catalog">
<article>Starter</article>
<article>Professional</article>
</div>.catalog {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr));
gap: 16px;
}
.catalog--reserve-space {
grid-template-columns: repeat(auto-fill, minmax(min(100%, 240px), 1fr));
}
repeat(auto-fill, ...) creates as many tracks as fit in the available size. auto-fit uses the same mechanism but collapses empty repeated tracks and their adjacent gutters after placement. They can look identical when every track is occupied. [2][3]
At 800px, three tracks with a 240px minimum and 16px gaps fit. With two cards, auto-fit gives each 392px; auto-fill retains a third track and each card is 256px wide. Add catalog--reserve-space to switch modes. min(100%, 240px) avoids forcing a 240px track into a narrower container.
Application layout with grid-template-areas
<div class="workspace">
<header class="workspace__header">Workspace</header>
<nav class="workspace__navigation" aria-label="Workspace">
<a href="/en/reports">Reports</a>
</nav>
<main class="workspace__main">
<h1>Overview</h1>
</main>
<footer class="workspace__footer">End of report</footer>
</div>.workspace {
display: grid;
grid-template-columns: minmax(0, 1fr);
grid-template-areas:
"header"
"navigation"
"main"
"footer";
gap: 1rem;
}
.workspace__header {
grid-area: header;
}
.workspace__navigation {
grid-area: navigation;
}
.workspace__main {
grid-area: main;
min-inline-size: 0;
}
.workspace__footer {
grid-area: footer;
}
@media (min-width: 48rem) {
.workspace {
grid-template-columns: 14rem minmax(0, 1fr);
grid-template-areas:
"header header"
"navigation main"
"footer footer";
}
}
Named areas describe relationships among header, navigation, main content and footer. Each named area must form a rectangle; every template string must contain the same number of cells. A dot denotes an unnamed cell. [2]
The mobile-first example uses one column below a 48rem viewport and places navigation next to content above that breakpoint. HTML source order remains unchanged. For a reusable component rather than a page shell, container queries can base a similar switch on the component's available space.
Why a grid overflows its container
<div class="report">
<aside>Filters</aside>
<section class="report__content" aria-label="Request log">
<pre><code>GET /api/international/customer/123456789012345678901234567890/orders</code></pre>
</section>
</div>.report {
display: grid;
grid-template-columns: 7rem minmax(0, 1fr);
gap: 1rem;
}
.report__content {
min-inline-size: 0;
}
.report__content pre {
max-inline-size: 100%;
overflow: auto;
}
A track or item's automatic minimum can depend on its content. A long URL in a pre element illustrates why 1fr alone may not suffice. minmax(0, 1fr) constrains the track while min-inline-size: 0 allows the relevant item to shrink. [2]
Content still needs a strategy: code or tables may require local scrolling via overflow: auto, text can use overflow-wrap: anywhere, and images can use max-inline-size: 100%. Hiding overflow on the entire document masks the problem and can clip content or focus indicators.
Grid lines, spans and automatic placement
<div class="gallery">
<article class="gallery__featured">Featured report</article>
<article>Sales</article>
<article>Traffic</article>
<article>Retention</article>
</div>.gallery {
display: grid;
grid-template-columns: repeat(3, minmax(0, 1fr));
grid-auto-rows: minmax(5rem, auto);
grid-auto-flow: row;
gap: 1rem;
}
.gallery__featured {
grid-column: 1 / -1;
}
Explicit grid lines count from 1; -1 refers to the last explicit line, not the end of all implicit tracks. grid-column: 1 / -1 makes the featured card span all explicit columns. span 2 covers two tracks when the grid structure permits it. [2][4]
Default grid-auto-flow: row places subsequent items along rows. The dense mode can fill earlier holes with later items, changing visual order without changing the DOM. Avoid it for forms, navigation and other content whose sequence matters to keyboard or screen-reader users. [4][5]
Subgrid: sharing rows across cards
<div class="plans">
<article class="plan">
<h2>Starter</h2>
<p>Core features.</p>
<a href="/en/plans/starter">View plan</a>
</article>
<article class="plan">
<h2>Professional</h2>
<p>Advanced reporting and integrations for larger teams.</p>
<a href="/en/plans/professional">View plan</a>
</article>
</div>.plans {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr));
gap: 1rem;
}
.plan {
display: grid;
grid-template-rows: auto 1fr auto;
gap: 1rem;
min-inline-size: 0;
}
@supports (grid-template-rows: subgrid) {
.plan {
grid-row: span 3;
grid-template-rows: subgrid;
}
}
.plan h2,
.plan p {
margin: 0;
}
A nested grid normally calculates its own tracks independently. subgrid adopts the parent's tracks along the selected axis, allowing neighboring cards' content to contribute to shared row sizing. Each example card spans three parent tracks: heading, description and link. [6]
Even with different description lengths, links in neighboring cards align. The @supports block retains a basic independent-card layout where subgrid is unavailable. Alignment applies to cards sharing those tracks, not every row of the catalog.
Alignment and debugging in DevTools
<div class="status-panel">
<p class="status-panel__message">No results</p>
</div>.status-panel {
display: grid;
min-block-size: 12rem;
place-items: center;
}
.status-panel__message {
margin: 0;
text-align: center;
}
justify-items and align-items align items within their grid areas along the inline and block axes respectively. place-items: center combines both. justify-content and align-content distribute the entire grid when space remains after track sizing. [2]
Select the grid badge next to the container in Chrome DevTools' Elements panel. Its overlay exposes line numbers and actual track sizes. Test narrow widths, long translations and enlarged text. Grid and Flexbox cooperate: use Grid for structure and Flexbox for actions inside components. [7][8]
