liveBook · Manning

CSS Grid

As explained in Tiny CSS Projects

CSS Grid is the CSS layout system and specification for arranging elements in rows and columns using a two-dimensional grid.

CSS Grid arranges the direct children of a container in rows and columns. Use it for layout, from a whole page to a small component. Keep tables for tabular data rather than using them to position page content. section 2.1 section 5.6

Start with a grid container

Suppose a page’s <main> contains a header, article, and aside. Start with `main { display: grid; }`. The browser creates a one-column grid and enough rows for its children, so the page may look unchanged at first. Inspect the container in browser developer tools to see the grid. Use `display: inline-grid` instead when the grid itself should be an inline-level box. section 2.1 section 2.2

Add only the controls the layout needs:

  • Define track sizes with `grid-template-columns` and `grid-template-rows`. The `fr` unit provides a flexible length; `repeat()` avoids repeating equal track definitions; `minmax()` sets minimum and maximum sizes. section 2.7
  • Name locations with `grid-template-areas`, then assign children with `grid-area`. Use `gap` to create space between cells. section 2.7
  • Leave placement unspecified when automatic placement does the job. On narrow screens, this can keep items stacked; media queries can change the layout as space grows. section 5.2.1 section 5.2

Grid can also align content. For example, `body { display: grid; place-items: center; }` centers its child elements within their grid cells. `place-items` combines `align-items` and `justify-items`. section 5.2.1

Listing 8.22 Description list styles
dl {
  display: grid;
  gap: .5rem;
  grid-template-columns: auto max-content;     #1
  padding: 0 1rem;
}
dd { text-align: right; }

This checkout-cart description list uses `display: grid`, two columns, and a gap. Its children fill available cells automatically, without individual placement rules. section 8.3.2

Visual placement should not disrupt the logical order of the source. Check keyboard tab order in developer tools when a rearranged grid might make navigation confusing. section 2.7

How Tiny CSS Projects covers it

How deep it goes
Works from container setup through track sizing, placement, spacing, defaults, and accessibility checks, without descending into browser internals.
Examples
Uses a header–article–aside page, stacked summary cards, and a two-column checkout description list.
What it assumes
Assumes a basic understanding of CSS and starts layout work from existing HTML and starter styles.
What you can do after
Build it from scratch: create grid containers, size and place children, and adapt layouts across screen widths.
How it connects
Connects grid placement to media queries, semantic tables, and keyboard tab order.

Also explained in

Compare how 5 books explain CSS Grid →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Tiny CSS Projects. The section links point to the parts of the book it draws on.