Lesson 7
30 min
Build two-dimensional grid layouts.
Definition
CSS Grid (“Grid Layout”) is a layout module that divides a container intorows and columns, forming a grid in which you then place the items. Unlike Flexbox, which works along a single axis, Grid controls both dimensions at once. An element becomes a grid container withdisplay: grid;.
Where Flexbox aligns items along a single axis, CSS Grid lets you buildtwo-dimensional layouts: rows AND columns at the same time. It’s the ideal tool for full-page layouts (header/sidebar/content/footer) or card grids.
.grid {
display: grid;
grid-template-columns: 200px 200px 200px; /* 3 columns of 200px */
grid-template-rows: 100px 100px; /* 2 rows of 100px */
gap: 16px;
}Grid has its own vocabulary, which is worth mastering before going further: it comes up in every property and in the browser’s inspection tools.
display: grid.The fr unit (“fraction”) distributes the available space proportionally — far more flexible than fixed pixels.
.grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* 3 columns of equal width */
gap: 16px;
}
.uneven-grid {
display: grid;
grid-template-columns: 2fr 1fr; /* the 1st column is twice as wide as the 2nd */
}
.repeated-grid {
display: grid;
grid-template-columns: repeat(4, 1fr); /* equivalent to "1fr 1fr 1fr 1fr" */
}minmax(min, max) defines a track that never goes below a minimum size and never exceeds a maximum size. Combined with fr, it gives you columns that stretch while staying readable. The auto keyword lets the content decide the size of the track.
.grid {
display: grid;
grid-template-columns: minmax(150px, 1fr) minmax(300px, 2fr) auto;
/* column 1: at least 150px; column 2: at least 300px and twice as wide;
column 3: sized to fit its content */
}A very powerful combination lets you create a card grid that automatically adapts to the number of columns that fit, without any media query.
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 20px;
}
/* Each card is at least 250px wide, and the grid adds or removes
columns automatically depending on the screen width. */gap sets the space between tracks, never around the grid. With a single value, it applies to both rows and columns; with two, the first applies to rows and the second to columns. You can also set them separately with row-gap andcolumn-gap.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 24px; /* same space between rows and columns */
gap: 12px 32px; /* 12px between rows, 32px between columns */
row-gap: 12px; /* between rows only */
column-gap: 32px; /* between columns only */
}Both keywords create as many columns as possible, but they differ when there is little content. With auto-fill, empty columns stay reserved: the cards keep their size and leave a gap on the right. With auto-fit, empty columns collapse and the existing cards stretch to take up the full width.
/* Empty columns are kept: the cards don't stretch */
.grid-a {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}
/* Empty columns disappear: the cards stretch */
.grid-b {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}You can position an item at a precise spot in the grid using grid line numbers (the lines between the cells, not the cells themselves).
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(3, 100px);
gap: 12px;
}
.big-block {
grid-column: 1 / 3; /* from vertical line 1 to line 3: spans 2 columns */
grid-row: 1 / 3; /* spans 2 rows */
}
.shorthand {
grid-column: span 2; /* equivalent: spans 2 columns from its position */
}Once the grid is in place, you can control how items are aligned inside their cell:justify-items works on the horizontal axis, align-items on the vertical axis, andplace-items combines both. To distribute the grid itself within its container, use justify-content and align-content.
.grid {
display: grid;
grid-template-columns: repeat(3, 150px);
height: 400px;
justify-items: center; /* centers each item horizontally in its cell */
align-items: center; /* centers each item vertically in its cell */
place-items: center; /* shorthand for the two previous lines */
justify-content: center; /* centers the whole grid in the container */
}
.special {
justify-self: end; /* one specific item aligns to the right of its cell */
}If the grid contains more items than declared cells, Grid automatically creates new rows: this is called the implicit grid. You set their size withgrid-auto-rows (or grid-auto-columns for implicit columns), and the filling direction with grid-auto-flow.
.grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-auto-rows: minmax(100px, auto); /* each new row is at least 100px tall */
grid-auto-flow: row; /* fills row by row (default) */
gap: 16px;
}For a full-page layout, you can name areas and literally draw the arrangement in the CSS — very readable.
.page {
display: grid;
grid-template-columns: 220px 1fr;
grid-template-rows: auto 1fr auto;
grid-template-areas:
"header header"
"sidebar content"
"footer footer";
min-height: 100vh;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; }
.content { grid-area: content; }
.footer { grid-area: footer; }<div class="page">
<header class="header">Header</header>
<nav class="sidebar">Menu</nav>
<main class="content">Main content</main>
<footer class="footer">Footer</footer>
</div>grid-template-areas makes the page structure readable at a glance, right in the CSS.. in the pattern.grid-template-areas pattern.With grid-template-areas, adapting a page to mobile comes down to redrawing the pattern inside a media query: the HTML elements don’t have to move. Here, the sidebar moves below the content on a narrow screen.
@media (max-width: 700px) {
.page {
grid-template-columns: 1fr;
grid-template-rows: auto 1fr auto auto; /* one row per area: header, content, sidebar, footer */
grid-template-areas:
"header"
"content"
"sidebar"
"footer";
}
}Debugging a grid
In the browser inspector, a small “grid” badge appears next to the container. Clicking it displays the numbered lines, the area names and the gaps: the ideal tool for understanding where an item is actually placed.
When to choose which?
A simple rule: if you’re aligning items in a single direction (a navigation bar, a list of buttons), use Flexbox. If you’re building atwo-dimensional layout (a whole page, a card grid), use Grid. The two combine very well together.
Exercise
display: grid;and repeat(auto-fit, minmax(200px, 1fr)), with spacing (gap) between each card.Exercise: placement
grid-column andgrid-row, then fill the rest with small cards.Exercise: alignment
place-items: center;, then align a single item to the right of its cell withjustify-self: end;.Going further
grid-template-areas, as in the example above.