Sub topic 2
Defining columns and rows
This chapter assumes you know how to write CSS selectors, how the box model works, and how display changes an element's layout. Here you will define the explicit tracks of a grid. A track is a single column or a single row.
Columns and rows as lists of sizes
Setting display: grid on an element makes it a grid container, and its direct children become grid items. By default the container has one column, so items stack as they would in normal flow. Two properties change that:
grid-template-columnstakes a space-separated list of sizes. Each size creates one column.grid-template-rowsworks the same way for rows.
The number of values is the number of tracks. The tracks you define this way make up the explicit grid.
<div class="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>.container {
display: grid;
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: 80px auto;
}
.container > div {
background: #dbe8ff;
border: 1px solid #5b7bd5;
padding: 0.5rem;
}This grid has three columns and two rows, so it has six cells. The six children fill the cells from left to right, one row at a time. The first column is fixed at 200px. The 1fr values are flexible tracks that share the remaining width equally. The fr unit gets its own chapter, so for now treat it as "a share of the leftover space".
Which sizes you can use
Track sizes accept several kinds of values:
| Value | Meaning |
|---|---|
| `200px`, `12rem` | A fixed length. |
| `30%` | A percentage of the container's size in that direction. |
| `auto` | The track grows to fit its content, and stretches if space is left. |
| `min-content`, `max-content` | The narrowest or widest size the content needs. |
| `1fr` | A flexible share of the free space. |
You can mix them freely. A common pattern is grid-template-columns: 250px auto for a fixed sidebar beside a content column.
Rows are often left undefined
Row heights usually come from the content, so many grids define columns only. If you omit grid-template-rows, the browser creates rows as items need them, and each row is sized to its tallest item. Those rows belong to the implicit grid, which a later chapter covers. Define rows explicitly only when you need a specific height or a known number of rows.
Common errors
- Forgetting
display: grid. Without it, the template properties have no effect. - Putting the template on the wrong element. Set it on the container. Grandchildren are not grid items.
- Using percentages for a full-width grid with a gap or padding. The tracks can add up to more than the container's width. Prefer
frunits when tracks should fill the container. - Expecting a row height to apply to rows you did not define.
grid-template-rows: 80pxsets only the first row. Later rows are implicit and useauto.
Exercise
A container has display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 50px; and holds seven items. How many columns does it have, how many rows are there in total, and how tall is each row?
Show answer
It has three columns. The seven items fill three rows (3 + 3 + 1). The first row is 50px tall because it is defined explicitly. The other two rows are implicit, so they are auto and sized to their content.