Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Full-Stack Web Development — Alpha School

Lesson 7: Layout with Grid

CSS Grid is the layout system for two dimensions at once: rows and columns together. Where Flexbox arranges items in a single line, Grid lets you draw a grid on a container, place items into named areas of it, and control the size of every row and column in one place. That makes it the natural choice for whole-page layouts, photo galleries, dashboards and card collections, and it replaces the fragile frameworks and float tricks that developers used to rely on. Grid looks like more to learn than Flexbox, but its core is small: define the columns, define the rows, let items fall into place, and adjust the gaps. This lesson builds that core, step by step.

Defining rows and columns

You turn on Grid with display: grid on a container, and then describe its columns with grid-template-columns and its rows with grid-template-rows. A value such as grid-template-columns: 200px 1fr 200px makes three columns, the middle one flexible, and the fr unit, short for fraction, shares out whatever space is left after fixed sizes are taken, so 1fr 2fr splits the remaining space into one part and two parts. The repeat() function saves typing, so repeat(3, 1fr) means three equal columns, and the gap property puts even space between rows and columns without margins. You rarely need to define rows by hand: Grid creates rows automatically as items fill the grid, which is called implicit placement, and that is usually exactly what you want for a gallery or a list of cards.

Placing items and naming areas

Grid items fall into the next free cell by default, in the order they appear in the HTML, which is enough for most galleries and card lists. When you want control, you can span an item across several columns with grid-column: span 2, or place it exactly with grid-column: 1 / 3, which means start at grid line 1 and end at grid line 3. For whole pages, the clearest approach is named areas: grid-template-areas draws a little map of the layout in text, with each word naming a cell, and each item is assigned to an area with grid-area. A typical page map reads “header header header” on the first row, “nav main aside” on the second, and “footer footer footer” on the third, and anyone reading that code can picture the page at a glance, which is a rare and valuable thing in CSS.

.page {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  grid-template-areas:
    "header header header"
    "nav    main   aside"
    "footer footer footer";
  gap: 1rem;
}
.page header { grid-area: header; }
.page nav    { grid-area: nav; }
.page main   { grid-area: main; }
.gallery {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1rem;
}

Responsive grids without media queries

Grid has a trick that makes responsive layouts almost effortless: the functions repeat(), minmax() and auto-fit. The line grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); says “fit as many columns as you can, each at least 220 pixels wide, sharing the space equally”, and the browser works out the rest: three columns on a wide screen, two on a tablet, one on a phone, with no media queries at all. The minmax() function sets a minimum and a maximum size for a track, so a sidebar can be minmax(200px, 300px), never narrower than 200 and never wider than 300. This single pattern covers most card grids and galleries you will ever build, and it is worth memorising, because it replaces pages of breakpoint code with one line that adapts to any screen.

Grid and Flexbox together

Grid and Flexbox are partners, not rivals, and most real pages use both. The rule of thumb is to use Grid for the big picture and Flexbox for the small pieces: Grid draws the page layout, the gallery or the dashboard, and Flexbox arranges the navigation, the buttons inside a card, or a form field next to its label. A card in a grid, for instance, often has display: flex and flex-direction: column inside it, so its button sits neatly at the bottom no matter how much text the card holds. When a layout feels awkward in one system, try the other, and when you find yourself adding extra wrapper elements just to make a layout work, that is usually a sign you picked the wrong tool. Keep both in your toolbox, practise each on small examples, and the choice will soon feel natural.

Key Terms

CSS Grid
A layout system for rows and columns together, suited to whole pages, galleries and dashboards.
Grid track
One row or one column of a grid.
fr unit
A fraction of the free space left after fixed sizes, so 1fr 2fr splits it into one part and two parts.
grid-template-areas
A property that draws a text map of a grid, naming each cell so items can be placed by name.
grid-area
The property that assigns an item to a named area of the grid.
auto-fit
A keyword that fits as many columns as possible into the available space.
minmax()
A function setting a minimum and maximum size for a track, such as minmax(220px, 1fr).

Discussion Questions

  1. Why is Grid better than Flexbox for a photo gallery, but Flexbox better for a navigation bar?
  2. What does repeat(auto-fit, minmax(220px, 1fr)) do on a phone screen, and why is that useful?
  3. Grid template areas draw a map of the page in text. Why is that easier for a team to read than a list of pixel positions?

Practice Questions

  1. What does grid-template-columns: repeat(3, 1fr) create?
  2. How do you make an item span two columns?
  3. What does the fr unit measure?
  4. Explain in your own words what repeat(auto-fit, minmax(220px, 1fr)) does.
  5. When should you use Grid, and when Flexbox?
Show answers
  1. Three equal-width columns. repeat(3, 1fr) is shorthand for 1fr 1fr 1fr, sharing the free space equally.
  2. Set grid-column: span 2 on the item. It then takes up two column tracks instead of one.
  3. A share of the free space left after fixed sizes. In 200px 1fr, the second column takes all the space left after the 200-pixel column.
  4. It fits as many columns as possible, each at least 220px wide, sharing the space. The browser works out the column count, so the grid adapts to any screen width without media queries.
  5. Grid for two-dimensional layouts such as pages and galleries; Flexbox for one-dimensional rows or columns such as navigation and buttons. Most pages use both, Grid for the big picture and Flexbox for the small pieces.
Keep reading to complete this lesson