Skip to main content
Albero Academy

Grid in Depth

Two-dimensional layout mastery — defining tracks, the fr unit, minmax and auto-fit, placing items, gaps and named template areas.

Chapter 13CSS17 min readBeginner friendly
  • #CSS
  • #Grid
  • #Layout

Grid is CSS's two-dimensional layout engine — rows and columns at once. Where Flexbox distributes items along one line, Grid builds a proper matrix, which makes whole-page layouts and complex galleries straightforward. This chapter goes well beyond Chapter 3's intro.

Defining tracks

rows & columns
.grid {
display: grid;
grid-template-columns: 200px 1fr 200px; /* 3 columns: fixed, fluid, fixed */
grid-template-rows: auto 1fr auto; /* header, body, footer */
gap: 16px; /* row & column gaps */
}

fr, minmax & auto-fit

The fr unit represents a fraction of the free space. Combined with repeat, minmax and auto-fit, you get responsive grids with no media queries at all:

the responsive grid one-liner
grid-template-columns: repeat(3, 1fr); /* 3 equal columns */
grid-template-columns: 2fr 1fr; /* first column twice as wide */
/* As many ≥240px columns as fit, then wrap — fully responsive, no @media */
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));

Placing items

span & position items
.featured {
grid-column: span 2; /* take two columns */
grid-row: 1 / 3; /* from line 1 to line 3 (two rows) */
}
/* Align a single item within its cell */
.item { justify-self: center; align-self: end; }
/* Align the whole grid's content */
.grid { justify-content: center; align-items: start; }

Template areas

Named areas let you draw a layout in ASCII — remarkably readable for page structure:

grid-template-areas
.page {
display: grid;
grid-template-columns: 200px 1fr;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
.page > header { grid-area: header; }
.page > nav { grid-area: sidebar; }
.page > main { grid-area: main; }
.page > footer { grid-area: footer; }

Practice

Build a responsive card gallery with repeat(auto-fit, minmax(240px, 1fr)) and make one card span two columns. Then build a full page layout (header, sidebar, main, footer) using grid-template-areas, and resize to watch it hold together.

Key takeaways

  1. 01grid-template-columns/rows define tracks; gap spaces them.
  2. 02The fr unit shares free space; 2fr 1fr makes proportional columns.
  3. 03repeat(auto-fit, minmax(240px, 1fr)) is a responsive grid with no media queries.
  4. 04Place items with grid-column/row and span; align with justify-/align-self.
  5. 05grid-template-areas draws a readable, named page layout.