Sub topic 2
Defining columns and rows
This chapter assumes you know HTML structure, CSS selectors and the box model, how display and normal flow work, and units such as px, %, and rem. You should also know that display: grid turns an element into a grid container, covered in the previous chapter. Here you will define the grid's explicit structure: how many columns and rows it has and how big each one is.
What an explicit track is
A grid is built from tracks. A track is one column or one row, the space between two neighboring grid lines. You declare tracks on the container with two properties:
grid-template-columnslists the width of each column, from left to right in a left-to-right writing mode.grid-template-rowslists the height of each row, from top to bottom.
Each value is a space-separated list. The number of entries is the number of tracks, and each entry is that track's size. The tracks you declare this way form the explicit grid.
.container {
display: grid;
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: auto auto;
gap: 16px;
}This container has three columns and two rows. The first column is always 200px wide. The other two share the remaining width equally, because 1fr means one share of the free space. The fr unit gets its own chapter, so treat it here as "a flexible share". The two rows are auto, so each is as tall as its tallest item. The gap property puts 16px between neighboring tracks. It is also covered in its own chapter.
A runnable example
The following page shows the same container with six items. Save it as an HTML file and open it in a browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Explicit tracks</title>
<style>
.container {
display: grid;
grid-template-columns: 200px 1fr 1fr;
grid-template-rows: auto auto;
gap: 16px;
}
.container > div {
background: #cfe3ff;
padding: 1rem;
}
</style>
</head>
<body>
<div class="container">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
</div>
</body>
</html>Items 1 to 3 fill the first row from left to right. Items 4 to 6 fill the second row. You did not say where any item goes. Grid placed each one in the next free cell, in source order. Placing items yourself is covered in later chapters.
Track sizes you can use
Each entry can be any of the following.
| Value | Meaning | Typical use |
|---|---|---|
| `200px`, `12rem` | A fixed length | Sidebars, icon columns |
| `25%` | A percentage of the container's size on that axis | Quick proportional columns |
| `1fr` | A share of the leftover space | Fluid content columns |
| `auto` | Size to the content, and stretch to absorb free space if no `fr` track is present | Header and footer rows |
| `min-content`, `max-content` | The narrowest or widest the content can be | Columns hugging their text |
| `minmax(a, b)` | A range between a minimum and a maximum | Covered in a later chapter |
You can mix them freely. The line grid-template-columns: 12rem auto 25% is valid. Fixed and percentage tracks are subtracted from the container first, and fr tracks divide whatever is left.
Grid lines are numbered
Tracks sit between grid lines, and the lines are numbered starting at 1. A grid with three columns has four vertical lines, and a grid with two rows has three horizontal lines. You do not declare lines directly. They exist because you declared tracks.
Lines can also be counted from the end with negative numbers, where -1 is the last explicit line. This is useful for "stretch to the far edge" without knowing how many tracks there are. Line-based placement is the subject of a later chapter, but knowing the numbering now helps you read track lists correctly.
Rows are often left alone
You do not need to declare rows. If you omit grid-template-rows, Grid creates rows as needed, and each is sized auto. Many real grids declare only columns:
.grid {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
}Declare rows when you need a specific height pattern, such as a fixed header with a flexible middle:
.page {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 64px 1fr auto;
min-height: 100vh;
}The first row is 64px, the last row fits its content, and the middle row takes the remaining viewport height.
Percentages, auto, and free space
Rows behave differently from columns in one important way. A container's width is usually known from its parent, but its height usually comes from its content. This has two consequences.
- A
1frrow only gets extra space if the container has a height larger than its content, for example throughheightormin-height. Without that,1frrows shrink to fit their content, likeauto. - A percentage row height such as
50%needs a definite container height. Without one, the browser treats it asauto.
In the .page example above, min-height: 100vh is what makes the middle 1fr row stretch.
Common errors
- Percentages plus gaps overflow. Three columns of
33.33%with a16pxgap are wider than the container, because the gaps are added on top. Use1frcolumns, which are calculated after gaps are removed. - Counting tracks wrong.
grid-template-columns: 200px 1fr 1fr;has three columns, not two. Count the entries, not thefrunits. - Expecting commas. Track lists are separated by spaces.
200px, 1fris invalid, and the browser ignores the whole declaration. - Forgetting
display: grid. The template properties do nothing on a container that is stilldisplay: block. - More items than cells. If the container has six cells and you add seven items, Grid does not drop the seventh. It creates an extra row outside your explicit definition, sized
auto. That row belongs to the implicit grid, covered in a later chapter. If a row suddenly looks wrong, check whether it is implicit. - Fixed tracks that do not fit.
grid-template-columns: 300px 300px 300pxinside a 600px container overflows horizontally. Fixed tracks never shrink. If the container might be narrow, combine them withfrorminmax().
The grid-template shorthand
grid-template sets rows and columns in one declaration, separated by a slash. Rows come first, then columns:
.container {
display: grid;
grid-template: auto auto / 200px 1fr 1fr;
}This is equivalent to the first example, minus the gap. The shorthand is compact, but it is easy to reverse the order. The longhand properties are clearer in most codebases.
Summary of the model
grid-template-columnsandgrid-template-rowsdefine the explicit grid.- One entry in the list means one track, and the entry is its size.
- Tracks create numbered lines, counted from 1 or from -1.
- Unsized rows are created automatically at
autoheight. - Row heights depend on the container's height, so
frrows need a container taller than its content to expand.
Exercise
A container has display: grid; grid-template-columns: 100px 2fr 1fr; width: 700px; and no gap. How wide is each column, and how many column lines does the grid have?
Show answer
The 100px column is fixed, leaving 600px. The 600px is divided into three shares (2 + 1), so the second column is 400px and the third is 200px. Three columns have four column lines, numbered 1 to 4.
Exercise
You write grid-template-columns: 1fr, 1fr, 1fr; and the columns do not appear. What is wrong, and what is the fix?
Show answer
Track sizes must be separated by spaces, not commas, so the declaration is invalid and ignored. Write grid-template-columns: 1fr 1fr 1fr;.
Exercise
A page container uses grid-template-rows: 64px 1fr auto; but the middle row is only as tall as its content. What property should you add to the container?
Show answer
Give the container a height larger than its content, for example min-height: 100vh. A 1fr row can only grow when the container has spare height.