Sub topic 1
Grid Container and Explicit Columns
Prerequisites
This chapter assumes you know basic CSS selectors and the box model. It also assumes you can read length units such as px, rem, and percentages. You should recognize semantic HTML elements such as header, main, aside, and footer. Media queries are not used yet.
What a grid container does
CSS Grid is a two-dimensional layout system. You turn an element into a grid container with display: grid. Its direct children become grid items. Grandchildren are not grid items, and they keep their normal layout.
Start with the page structure that the rest of this tutorial builds on:
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</div>The div with class page will be the grid container. The four semantic elements inside it are the grid items.
Defining explicit columns
The grid-template-columns property defines the columns you declare yourself. These columns are the explicit grid. Each value in the list is the width of one column, which Grid calls a track.
.page {
display: grid;
grid-template-columns: 220px 1fr;
gap: 1rem;
}This rule creates two columns. The first is a fixed 220px wide. The second uses 1fr.
An fr unit is a share of the free space left in the container after fixed sizes and gaps are subtracted. With one fr track, that track takes all the remaining width. With 1fr 2fr, the second track gets twice as much of the free space as the first.
The gap property sets the space between tracks. It applies only between rows and columns, never on the outer edge. That makes it simpler than margins on each item.
How items are placed
You wrote no placement rules, but the items still land in cells. By default, Grid auto-places items in source order. It fills the first row from left to right, then starts a new row. You declared two columns and did not declare any rows, so Grid creates rows as needed.
With four children and two columns, you get two rows:
| Row | Column 1 (220px) | Column 2 (1fr) |
|---|---|---|
| 1 | header | aside |
| 2 | main | footer |
This is not the layout you want for a page. The header should span the top, and the sidebar should sit beside the main content. Fixing that is the job of named areas, which the next chapters cover.
Explicit and implicit tracks
Tracks you define with grid-template-columns are explicit. Tracks Grid adds on its own, such as the second row above, are implicit. Implicit rows are sized to fit their content by default. You can change that with grid-auto-rows, but this tutorial rarely needs it.
Common errors
- Forgetting
display: grid. Without it,grid-template-columnsandgaphave no effect. The children stay in normal block flow. - Expecting grandchildren to join the grid. Only direct children are grid items. To lay out a deeper element in a grid, make its parent a grid container too.
- Using percentages with
gap. A column list such as50% 50%plus a gap is wider than the container, and the grid overflows. Use1fr 1fr, which accounts for the gap. - Mixing up
frand%. A percentage is a share of the whole container. Anfris a share of what remains after fixed tracks and gaps.
Summary of the model
display: gridmakes the element a grid container and its children grid items.grid-template-columnsdefines explicit column tracks.frunits divide leftover space, andgapadds space between tracks.- Items fill cells in source order, and implicit rows appear as needed.
Exercise
A container has display: grid; grid-template-columns: 100px 1fr 2fr; gap: 10px; and is 530px wide. How wide is each column?
Show answer
The two gaps use 20px and the fixed column uses 100px, which leaves 410px of free space. That space is split into three shares, so 1fr is about 136.67px. The columns are 100px, about 136.67px, and about 273.33px.
Exercise
You set grid-template-columns: 1fr 1fr on a div, but the children still stack vertically. What is the most likely cause?
Show answer
The div is missing display: grid, so the column rule is ignored.