liveBook · Manning

Chapter 5: Grid layout

What you will learn in chapter 5 of CSS in Depth, Second Edition

The introduction presents CSS grid as a two-dimensional layout tool that complements flexbox and allows flexible sizing and placement of elements.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction presents CSS grid as a two-dimensional layout tool that complements flexbox and allows flexible sizing and placement of elements.

Key terms: CSS grid, two-dimensional layout, columns, rows, grid, place items, flow naturally, Flexbox, Grid Layout Module

5.1 Building a basic grid

The section builds a six-item grid and explains how the container, track sizes, and gaps shape its layout.

Key terms: display: grid, grid container, grid items, inline-grid, grid-template-columns, grid-template-rows, fr, fraction unit, grid track, gap

5.2 Anatomy of a grid

This section names the parts of a grid and uses a page layout to show how grid items, tracks, and the repeat() function fit together.

Key terms: grid line, grid track, grid cell, grid area, grid container, grid item, flatten the HTML, grid-template-columns, grid-template-rows, fr

5.2.1 Numbering grid lines

This section explains how numbered grid lines, grid-row, grid-column, and span place items in a CSS grid.

Key terms: grid line, grid-column, grid-row, grid area, grid-column-start, grid-column-end, grid-row-start, grid-row-end, span, grid item placement algorithm

5.2.2 Working together with flexbox

Explains how grid and flexbox complement each other, using alignment needs and the influence of content on sizing to choose between them.

Key terms: flexbox, grid, one dimensional, two dimensional, flex-wrap, track, content out, layout in, grid track, margin collapsing

5.3 Alternate syntaxes

Introduces named grid lines and named grid areas as alternative syntaxes for laying out grid items, with readability guiding the choice between them.

Key terms: named grid lines, named grid areas

5.3.1 Naming grid lines

This section shows how to name grid lines and use those names, including shared and repeated names, to place grid items.

Key terms: named grid lines, grid tracks, left-start, left-end, grid-column, repeat(), row 3, col 2 / span 2

5.3.2 Naming grid areas

This section shows how to lay out grid items with named areas instead of grid lines, including the syntax’s shape and empty-cell rules.

Key terms: grid-template-areas, grid-area, named grid areas, named grid area, rectangle, period

5.4 Explicit and implicit grid

Explains how implicit tracks and automatic column sizing let a grid accommodate an unknown number of items and adapt to available space.

Key terms: explicit grid, implicit grid, grid-template-*, implicit tracks, grid item placement algorithm, auto, grid-auto-columns, grid-auto-rows, implicit grid rows, 1fr

5.4.1 Adding variety

This section enlarges featured grid items, explains the gaps automatic placement can leave, and uses dense auto-flow to fill those gaps while cautioning about display order.

Key terms: featured, span, grid tracks, grid item placement algorithm, grid-auto-flow, dense, row dense, backfill, source order, display order

5.4.2 Adjusting grid items to fill the grid track

This section fixes gaps beneath images in a grid by stretching the images with flexbox and using object-fit to avoid distortion.

Key terms: grid item, grid area, <figure>, flex container, column, flex-grow, object-fit, fill, cover, contain

5.5 Subgrid

This section explains how subgrid aligns elements inside separate grid cards to their parent grid’s tracks, then shows how to apply it with a fallback.

Key terms: subgrid, grid lines, grid container, grid items, display: grid, grid-template-rows, grid-template-columns, fallback, @supports

5.5.1 Additional options

The section explains how subgrid items can be placed with inherited or newly named grid lines, then briefly describes masonry layout as a possible future grid feature.

Key terms: subgrid, line numbers, line names, grid area names, parent grid, subgrid keyword, grid line names, grid-row, masonry layout, equal-width columns

5.6 Alignment properties

Explains how CSS alignment properties position grid tracks within a container and how placement shorthands combine alignment values.

Key terms: justify-content, justify-items, justify-self, align-content, align-items, align-self, grid tracks, grid container, start, end

5.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 4: Flexbox Chapter 6: Positioning and stacking contexts →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.