Full answer
CSS Grid places items in rows and columns. You turn it on for the parent, and the children become grid items.
Basic steps:
display: gridon the container.- Define the columns with
grid-template-columns. For example100px 1fr 2frmakes three columns. Thefrunit is a share of the free space. After the fixed 100 pixels, the second column gets one share and the third gets two shares. - Rows are made automatically as needed. Their size is
autoby default. Setgrid-template-rowsorgrid-auto-rowsto control them. gapsets the space between rows and columns. It only adds space between tracks, not at the outer edges.repeat(3, 1fr)is a short way to write three equal columns.- Items are placed in order. To make an item bigger, use
grid-column: span 2, or give start and end lines, such asgrid-column: 1 / 3. Lines are numbered from 1.
Use Grid when you plan rows and columns together, for example a card list, a gallery or a page layout. Use Flexbox when you lay out items in one line or one direction. They also work together.
Useful extras: align-items and justify-items align items inside their cells. minmax(0, 1fr) stops long content from stretching a column. The browser tools show grid lines and track sizes, which helps when learning.
The first grid has widths of 100, 160 and 320 pixels. The free space is 600 minus 100 minus 20 for the two gaps, which is 480 pixels, shared as one part and two parts. The fourth item wraps into a second row. In the second grid, the wide item takes two of three equal columns, the next item fills the last column of the first row, and the third item moves to the second row.
Draw the rows and columns on paper first, and then write the template.
<style>
.g { display: grid; grid-template-columns: 100px 1fr 2fr; gap: 10px; width: 600px; }
.g div { background: #cde; padding: 4px; }
.h { display: grid; grid-template-columns: repeat(3, 1fr); width: 600px; }
.h .wide { grid-column: span 2; }
</style>
<div class="g" id="g"><div id="c1">1</div><div id="c2">2</div><div id="c3">3</div><div id="c4">4</div></div>
<div class="h" id="h"><div class="wide" id="w">Wide</div><div id="o1">1</div><div id="o2">2</div></div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- display: grid makes children grid items.
- fr is a share of the free space after fixed sizes and gaps.
- gap adds space between tracks only.
- repeat() shortens repeated tracks.
- span or line numbers place larger items.
Common mistakes
- Using margins for spacing between items instead of gap.
- Thinking that fr includes the fixed columns and the gaps.
- Counting grid lines from 0.
Follow-ups
- What is the difference between 1fr and auto?
- How do you make an item span two rows?
Tips
- Describe the three steps: display grid, columns, gap.
- Explain fr as a share of the free space after fixed sizes and gaps.