Use CSS Grid when a layout needs alignment across both rows and columns, such as a dashboard or product listing. Flexbox handles a single axis; Grid defines two-dimensional tracks on a container and places its children within them. This can keep the HTML semantic without adding wrappers solely for layout. section 4.2 section 4.2.2
Build a grid from the container outward:
- Set `display: grid`, then define columns and rows or use named areas. Children can span tracks or align to their edges. section 4.2.2
- For fluid columns, combine `repeat()`, `auto-fit` or `auto-fill`, `minmax()`, and fractional units. Intrinsic sizing can let content influence the tracks. section 4.2.2
- Use `gap` between items; reserve margins for space outside the grid. This keeps spacing consistent across both axes. section 4.2.2
- Use Subgrid when a nested grid needs to share its parent's tracks. Container queries can instead make a grid respond to its own available width. section 4.2.2 section 4.4
Responsive product cards
A product listing can remain a section of article cards while its stylesheet sets `grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr))`. With `display: grid` and `gap` on the section, the columns expand or collapse as space changes. Each card can also be a grid to space its own contents. section 4.2.2
Listing 4.4 The HTML markup for a Simple CSS Grid Demo
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Simple CSS Grid Demo</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="grids.css">
<h1>Products</h1>
<section class="grid" aria-label="Product grid">
<article class="card">
<div class="thumb" role="img" aria-label="Espresso blend"></div>
<h2>Espresso Blend</h2>
<p>Strong and rich, roasted daily.</p>
<p class="price">$14.90</p>
</article>
<article class="card">
<div class="thumb" role="img" aria-label="House roast"></div>
<h2>House Roast</h2>
<p>Smooth and balanced for every day.</p>
<p class="price">$12.50</p>
</article>
<!-- More articles -->
</section>
The product-list markup shows a labelled section containing article cards, without layout-only wrappers between the section and cards. section 4.2.2
Listing 4.5 The CSS stylesheet for a Simple CSS Grid Demo
:root {
--gap: 16px; --line: #ddd; --radius: 12px;
--ink: #222; --brand: #5d4037;
}
body {
* { box-sizing: border-box; }
margin: 0; padding: 2rem;
font: 16px/1.5 system-ui, sans-serif; color: var(--ink);
}
.grid { #1
display: grid;
gap: var(--gap);
grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
}
article.card {
border: 1px solid var(--line);
border-radius: var(--radius);
padding: 1rem;
background: #fff;
display: grid;
gap: .75rem;
}
.thumb {
aspect-ratio: 4 / 3;
border-radius: 8px;
background: conic-gradient(from 45deg, #f3e5f5,
#e0f2f1, #fff8e1, #f3e5f5);
}
h2 { margin: 0; font-size: 1.1rem; }
p { margin: 0; color: #666; }
.price { color: var(--brand); font-weight: 600; }
The stylesheet shows the responsive column rule, `gap`, and a nested card grid together. section 4.2.2






