liveBook · Manning

Grid container

As explained in Web Design Playground, Second Edition

A grid container is an element whose child elements are laid out according to CSS Grid rules.

A grid container is a parent element that surrounds child elements. Once Grid is switched on, those children become grid items, ready to be arranged across rows and columns instead of simply stacking in the browser’s default flow. section 13.1 section 13.2

The magic-switch-looking declaration is display: grid. Apply it to a block-level parent such as a div or even the body element. Nothing extra is needed on each child—Grid quietly promotes them all to grid items. section 13.2 section 13.1

.container {
    display: grid;   #1
}

This small rule turns the element with class container into a grid container. Its child divs become grid items automatically. section 13.2

At first, a new grid container can look as though nothing changed: its items form a one-column grid that resembles ordinary stacking. Specify rows or columns to make the grid’s structure visible. section 13.2 section 13.10

A grid container controls the larger layout picture with properties such as:

  • grid-template-columns and grid-template-rows to set track counts and sizes. section 13.10
  • column-gap and row-gap to add gutters between tracks. section 13.10
  • justify-content and align-content to position items within the whole grid. section 13.9
  • justify-items and align-items to position items within their columns or rows. section 13.10

Centering a page’s main content

Make body a three-column grid with 1fr auto 1fr. The outer columns share leftover space, while the middle column fits its content; placing a nested container in column 2 centers that content in the browser window. section 20.5

How Web Design Playground, Second Edition covers it

How deep it goes
Defines container conversion and automatic items, then develops sizing, placement, gaps, areas, and alignment.
Examples
Demonstrates a div and body converted with display: grid, plus a three-column body that centers portfolio content.
What it assumes
Assumes familiarity with block elements and HTML semantic page elements while introducing Grid container setup directly.
What you can do after
Apply it via CSS Grid to convert a parent element and control its child layout with tracks and alignment.
How it connects
Connects CSS Grid to grid items, rows, columns, gutters, named areas, alignment, Flexbox, and default flow.

Also explained in

Compare how 2 books explain grid container →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Design Playground, Second Edition. The section links point to the parts of the book it draws on.