Sub topic 1
Grid Container and Explicit Columns
This chapter covers the first building block of CSS Grid: turning an element into a grid container and defining its columns yourself. Later chapters build named areas and auto-fit columns on top of this.
Prerequisites
This chapter assumes you already know:
- Basic CSS selectors and the box model (content, padding, border, margin).
- Semantic HTML elements such as
header,main,aside, andfooter. - Length units such as
px,rem, and percentages. - Basic use of media queries. They are not needed in this chapter.
What a grid container is
CSS Grid is a two-dimensional layout system. It arranges items in rows and columns at the same time. Flexbox, by comparison, lays items out along one axis at a time.
You create a grid by setting display: grid on an element. That element becomes the grid container. Its direct children become grid items. Grandchildren are not grid items. They stay in normal flow inside their parent item, unless you make that parent a grid too (a later chapter does this).
Here is the HTML structure used throughout this tutorial:
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</div>With only display: grid and no column definition, the browser creates one column. The four children stack vertically, each as wide as the container. That looks much like normal block layout, with one difference. The items are now grid items, and you can place and size them with grid properties.
.page {
display: grid;
}Use display: inline-grid if the container itself should sit inline with surrounding text. Most page layouts use display: grid.
Explicit tracks
A track is a single row or column in the grid. The tracks you declare with grid-template-columns and grid-template-rows form the explicit grid. If items need more rows than you declared, the browser adds implicit rows. Their size is controlled by grid-auto-rows.
This chapter focuses on columns, because column definitions drive most responsive layouts. You list one size per column, separated by spaces:
.page {
display: grid;
grid-template-columns: 220px 1fr;
}This defines two columns. The first is 220px wide. The second takes the remaining space. Items are placed into cells in source order, left to right, then top to bottom. 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 we want for a page. The header should span the full width. Naming regions with grid-template-areas fixes that in a later chapter. For now, the point is how auto-placement works. Without explicit placement, items fill cells in order.
Lines and tracks
Grid tracks are separated by grid lines. A grid with two columns has three vertical lines, numbered 1, 2, and 3 from the start edge. Line numbers matter later, because named areas create implicit named lines.
The diagram below shows the relationship between lines and tracks for a two-column grid.
Lines are numbered from 1, not 0. A common mistake is to assume the first line is 0.
Sizing tracks with fr units and gap
Fixed pixel columns do not adapt to their container. Grid offers the fr unit for flexible sizing.
The fr unit
One fr is a fraction of the free space in the grid container. Free space is the container's width minus all fixed-size tracks and gaps. The browser divides it among the fr tracks in proportion to their numbers.
.example-a {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* three equal columns */
}
.example-b {
display: grid;
grid-template-columns: 1fr 2fr; /* second column is twice as wide */
}
.example-c {
display: grid;
grid-template-columns: 220px 1fr; /* fixed sidebar, flexible main */
}In example-c, the container is, say, 1000px wide. The first column takes 220px. The remaining 780px goes to the 1fr column.
Gap
The gap property adds space between tracks. It does not add space at the outer edges of the grid.
.page {
display: grid;
grid-template-columns: 220px 1fr;
gap: 1rem;
}gap is shorthand for row-gap and column-gap. Writing gap: 1rem 2rem sets the row gap to 1rem and the column gap to 2rem. Gaps are subtracted from the container's width before fr units are calculated. That means fr columns never overflow because of gaps. You do not need margins or calc() to compensate for them.
A caveat about 1fr
1fr is shorthand for minmax(auto, 1fr). The auto minimum means a track cannot shrink below the minimum content size of its items. A long unbreakable string, such as a URL, or a wide image can force a 1fr column wider than you expect and cause overflow.
The usual fix is to write the minimum explicitly:
.page {
display: grid;
grid-template-columns: 220px minmax(0, 1fr);
gap: 1rem;
}With minmax(0, 1fr), the column may shrink to zero, so content is clipped or wrapped instead of stretching the track. The next chapter covers minmax() in detail.
Comparing track sizing options
| Value | Meaning | Adapts to container? |
|---|---|---|
| `220px` | Exactly 220 CSS pixels | No |
| `20%` | 20% of the container's width | Yes, but ignores gaps, so the total can overflow |
| `1fr` | One share of free space | Yes |
| `auto` | Sized by content, and takes leftover space if the alignment is `stretch` | Partly |
| `max-content` | As wide as the longest unbroken content | By content |
The percentage row deserves attention. Three columns of 33.33% with a 1rem gap total more than 100% of the container width, so they overflow. Three 1fr columns do not have this problem.
Mixing units
You can combine different units in one definition:
.layout {
display: grid;
grid-template-columns: 200px 1fr 2fr;
gap: 1rem;
}The browser first reserves 200px and the gaps. It then splits the rest in a 1:2 ratio.
Rows work the same way. grid-template-rows: auto 1fr auto is a common pattern for a full-height page. The header and footer size to their content, and the middle row takes the remaining height. For 1fr to have free space to divide in the block direction, the container needs a definite height, for example min-height: 100vh.
Worked example
This example builds a two-column page with a fixed sidebar and a flexible main column. It is complete and runs as shown.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Explicit columns</title>
<style>
* { box-sizing: border-box; }
body {
margin: 0;
font-family: system-ui, sans-serif;
}
.page {
display: grid;
grid-template-columns: 220px minmax(0, 1fr);
gap: 1rem;
padding: 1rem;
}
.page > * {
padding: 1rem;
background: #e8eef7;
border: 1px solid #9ab;
}
</style>
</head>
<body>
<div class="page">
<header>Header</header>
<aside>Sidebar</aside>
<main>Main content</main>
<footer>Footer</footer>
</div>
</body>
</html>Open this in a browser and resize the window. The first column stays at 220px. The second column grows and shrinks. Because there are four items and two columns, the items form two rows, in the order shown in the table earlier. To see the grid itself, open your browser's developer tools and toggle the grid overlay. Chrome, Firefox, and Safari all provide one.
Common errors
- Setting
display: gridon the wrong element. Only direct children become grid items. If your cards sit inside a wrapper, apply the grid to the wrapper. - Using
grid-template-columnswithoutdisplay: grid. The property has no effect on a non-grid element. - Expecting
gapto add outer padding. Gaps only appear between tracks. Addpaddingto the container for outer space. - Using percentages plus gap. The total exceeds 100% and the grid overflows. Use
fr. - Letting
1frexpand around wide content. Useminmax(0, 1fr)when a column holds long strings, tables, or images. - Forgetting that line numbers start at 1.
- Assuming margins on grid items collapse. Margins on grid items do not collapse with each other or with the container, so spacing can be larger than in block layout. Prefer
gap.
What comes next
You now have the foundation: a grid container, explicit columns, fr units, and gap. The next chapters use these in three ways:
- Name regions with
grid-template-areasand place items withgrid-area. - Repeat and bound tracks with
repeat()andminmax(). - Make columns respond to container width with
auto-fit, with no media queries.
Exercise
A container has display: grid; grid-template-columns: 200px 1fr 2fr; gap: 1rem; and is 1000px wide. How wide is each column?
Show answer
There are two gaps of 16px (assuming 1rem is 16px), so 32px is used by gaps. The fixed column takes 200px. Free space is 1000 - 200 - 32 = 768px. That space is split into 3 shares, so 1fr is 256px. The columns are 200px, 256px, and 512px.
Exercise
Three columns are defined as 33.33% 33.33% 33.33% with gap: 1rem. Why does the grid overflow, and how do you fix it?
Show answer
The percentages already add up to about 100% of the container width, and the two gaps add extra width on top. The total exceeds the container. Replace the percentages with 1fr 1fr 1fr, or repeat(3, 1fr), because fr units divide only the space left after gaps are removed.
Exercise
A 1fr column holding a very long URL stretches the page wider than the viewport. What change to grid-template-columns prevents this?
Show answer
Write the track as minmax(0, 1fr). The default minimum of 1fr is auto, which stops the track from shrinking below its content's minimum size. A minimum of 0 lets the track shrink so the content can wrap or be clipped.