liveBook · Manning

CSS Grid

As explained in Web Design Playground, Second Edition

A grid layout is a web-page layout organized into rows and columns whose track sizes and placement are defined by CSS Grid.

CSS Grid is a CSS layout system for arranging page content on an invisible grid of rows and columns. It gives a parent element control over its child elements horizontally and vertically, rather than leaving blocks stacked in the browser’s usual top-to-bottom flow. section 11.2 section 13.1

Think of Grid as graph paper behind a page: the lines are invisible, but they give each item a place to go. That makes Grid a strong choice for a page’s overall layout, while Flexbox handles smaller one-direction arrangements inside it. section 11.2 section 13.1

A Grid layout has two main players:

  • A grid container is the parent element that holds the layout. It can be a div, a semantic HTML element, or even body. section 13.1
  • Grid items are the container’s child elements. Once their parent becomes a grid container, they become grid items automatically—no ceremonial extra code required. section 13.2

Start by applying display: grid to a block-level element. The children then belong to a one-column grid, which may look suspiciously like the default stacked layout until you define rows or columns. section 13.2

body {
    display: grid;
}

This smallest possible rule turns the body into a grid container. Its direct child elements automatically become grid items. section 13.2

Center a page content column

Use Grid on body to make three columns: equal leftover space on the left and right, plus a middle column sized to its content. Put the content wrapper in column 2. The wrapper can then use Flexbox for its own vertical stack—Grid and Flexbox are teammates, not rivals. section 20.5 section 11.2

body {
    display: grid;                                                 #1
    grid-template-columns: 1fr auto 1fr;                           #1
    min-height: 100vh;
    margin-top: 1rem;
    font-family: "Open Sans", Verdana, sans-serif; >               #2
    background-color: #cdc9c1; >                                   #2
    background-image: radial-gradient(circle farthest-side at 
    center top, hsl(0, 0%, 100%) 0%, #cdc9c1 100%);>               #2
    color: #362507;                                                #2
}
.container {
    grid-column: 2;                                                 #1
    display: flex;                                                  #2
    flex-direction: column;                                         #3
    justify-content: flex-start;                                    #2
    max-width: 60rem;                                               #4
}

The CSS shows the three-column body grid and places .container in the middle column; that nested container uses Flexbox for its internal layout. section 20.5

Grid is standards-friendly and works in current and recent major browsers without a layout library. It is meant for escaping default document flow when a page needs deliberate two-dimensional placement. section 13.0 section 11.2

How Web Design Playground, Second Edition covers it

How deep it goes
Defines containers and items, demonstrates display: grid, and applies a three-column centering layout; row and column configuration is deferred.
Examples
Uses colored div elements, a display: grid container conversion, and a portfolio page centered with three Grid columns.
What it assumes
Introduces Grid from default block stacking while relating it to Flexbox, floats, inline blocks, and semantic HTML elements.
What you can do after
Apply it via CSS declarations to create a grid container and center page content with a three-column layout.
How it connects
Connects Grid with Flexbox, default browser flow, floats, inline blocks, semantic HTML elements, and grid-template-columns.

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 Web Design Playground, Second Edition. The section links point to the parts of the book it draws on.