1.grid {2 display: grid;3 grid-template-columns:4 100px 1fr 2fr;5 gap: 15px;6}7.a { grid-column: 1 / 4; }width400px1234ABCDEgrid100px 1fr 2fr15px1 / 4
Left: the stylesheet, style.cssRight: the grid container, its items A to E, and the ruler of tracks and grid linesTokens: each declaration on its way to the gridLeft: the stylesheet. Right: the grid container with its five items, and behind it a ruler that measures the column tracks and numbers the grid lines. display: grid makes the container a grid; grid-template-columns: 100px 1fr 2fr makes three tracks and the items fill the cells in order, adding a second row; fr shares the free space; gap opens 15px gutters; A spans lines 1 to 4. On a narrower screen the same CSS gives narrower fr tracks while the 100px track stays. A real browser applies all the declarations at once when it lays out the page. The figure applies them one at a time, in the order of the stylesheet, so you can see what each one does.

The grid container

  1. The HTML is one container, a div with class grid, holding five child divs, A to E. With no CSS yet they are block boxes in normal flow: each takes the full width, and they stack top to bottom.

Columns and fr

Rows, gaps and spans

Every screen size

What happens

;; step 1/8 · items
index.html, inside <body>:
  <div class="grid">
    <div class="a">A</div>
    <div>B</div>
    <div>C</div>
    <div>D</div>
    <div>E</div>
  </div>
normal flow: 5 blocks, each 400px wide, stacked
(the container is 400px wide in this example)

A real browser applies all the declarations at once when it lays out the page. The figure applies them one at a time, in the order of the stylesheet, so you can see what each one does. No row sizes are set, so each row is only as tall as its content, one line of text here. The figure draws the rows taller, and the blocks a little inside their cells, so the letters are easy to see.