Sub topic 3
Columns: choosing a count and width
This chapter assumes you know basic HTML structure, CSS syntax and units (px, rem, %), and the box model. It covers how many columns a grid should have and how wide each one should be.
What a column is
A column is one of the repeating vertical tracks that a grid divides the page into. Content does not sit inside columns by itself. An item lines up with the grid when its left and right edges fall on column edges. Gutters (the gaps between columns) and margins are covered in later chapters, so here we treat the gap as a fixed 1.5rem.
Choosing a column count
The count sets how many different layouts the grid can express. A column count that divides evenly gives you more ways to split the page.
| Columns | Even splits available | Typical use |
|---|---|---|
| 4 | 1, 2, 4 | Phones |
| 6 | 1, 2, 3, 6 | Tablets, simple pages |
| 12 | 1, 2, 3, 4, 6, 12 | Desktop layouts |
Twelve is common because it divides into halves, thirds, quarters, and sixths. A main area of 8 columns with a sidebar of 4 gives a two-thirds and one-third split. More columns give finer control but also more choices to make, and they can encourage inconsistent layouts. Pick the smallest count that covers the splits your design needs.
Choosing a column width
Column width is a result, not a choice. It comes from the container width, the column count, and the gap:
column width = (container width − gaps) ÷ columns
Here the gaps total (columns − 1) × gap width. For a 72rem container with 12 columns and a 1.5rem gap, the gaps total 11 × 1.5rem = 16.5rem. The remaining 55.5rem divides into 12 columns of 4.625rem each.
You rarely calculate this by hand in CSS. The fr unit (a fraction of the free space in the grid) does it for you.
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}repeat(12, 1fr) creates twelve tracks of equal share. The browser subtracts the gaps first, then divides what is left. These columns are flexible, so they shrink and grow with the container.
Spanning columns
An item takes a number of columns with grid-column: span N.
<div class="grid">
<main class="main">Main content</main>
<aside class="sidebar">Sidebar</aside>
</div>.main {
grid-column: span 8;
}
.sidebar {
grid-column: span 4;
}Spans in a row should add up to the column count. Here 8 + 4 = 12, so the row is full. If they add up to more than 12, the extra item wraps to a new row.
Common errors
- Using
1frcolumns with wide content. A1frtrack has an implicit minimum of its content's minimum size. A long unbroken string or a wide image can push the track wider than its share. Useminmax(0, 1fr)if you need strictly equal columns. - Counting gaps as columns. A span of 4 columns also covers the 3 gutters between them. The item is wider than 4 column widths.
- Choosing a count for the widest layout only. Twelve columns on a 360px phone leaves each column about 15px wide. Use fewer columns on small screens, as a later chapter on responsive grids shows.
Exercise
A container is 60rem wide, uses 6 columns, and has a 2rem gap. How wide is each column in rem?
Show answer
The gaps total 5 × 2rem = 10rem. The remaining space is 60rem − 10rem = 50rem. Divided among 6 columns, each is about 8.33rem wide.
Exercise
Why would you choose 12 columns over 10 for a layout that needs halves, thirds, and quarters?
Show answer
Twelve divides evenly by 2, 3, and 4, so each of those splits uses a whole number of columns. Ten divides evenly by 2 and 5 but not by 3 or 4.