CSS Grid
Two-dimensional layout: define columns and rows once, place items into cells, and build page scaffolds in a dozen lines.
Flexbox arranges a line; Grid arranges the whole board β rows and columns at once. Page scaffolds, galleries, dashboards: any place two dimensions matter.
The mental model
.gallery {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 1rem;
}
grid-template-columnsdefines the columns: here, three tracks, each1fr("one fraction of the free space" β equal thirds).repeat(3, 1fr)is shorthand for1fr 1fr 1fr.gapworks exactly as in flex β between rows and columns.- Items flow into cells automatically, left-to-right, top-to-bottom.
Fractions and friends
grid-template-columns: 200px 1fr; /* fixed sidebar, flexible main */
grid-template-columns: 1fr 2fr; /* one part, two parts */
grid-template-columns: auto 1fr; /* content-sized, then the rest */
fr distributes leftover space after fixed sizes β the "sidebar + main" layout is
one line, no floats, no hacks.
Explicit placement
.featured {
grid-column: 1 / -1; /* span from first to last line: full width */
}
Grid lines, not cells: column line 1 to line -1 (the end). span 2 also works:
grid-column: span 2. This is how one tile swallows a row in a gallery.
The page scaffold
.page {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-rows: auto 1fr auto;
min-height: 100vh;
}
header {
grid-column: 1 / -1;
}
aside {
grid-column: 1;
}
main {
grid-column: 2;
}
footer {
grid-column: 1 / -1;
}
<div class="page">
<header>β¦</header>
<aside>β¦</aside>
<main>β¦</main>
<footer>β¦</footer>
</div>
Named areas (grid-template-areas) are the next step β when you need it, it reads
like ASCII art of the page. For now, line-based placement covers real needs.
Flex or Grid?
Ask: one dimension or two? A row of buttons, a nav, a toolbar β flex (content drives, items flow). A page scaffold, a gallery, a form grid β grid (the container defines the structure, items slot in). They compose: a grid cell can be a flex container. That combination is how modern pages are built.
What you learned
display: grid+grid-template-columns;frfractions;repeat()gapfor the whole board; items auto-flow into cellsgrid-column: 1 / -1spans the full width- Flex = one dimension (content-driven); Grid = two dimensions (structure-driven)
Next: responsive design β the same layouts, adapting to every screen.