Sub topic 3
Columns: choosing a count and width
This chapter assumes you know basic HTML and CSS syntax, units such as px, rem, and %, and the box model. It covers the first of the three grid parts: columns. You will learn how to pick a column count, how column width follows from the other grid values, and which mistakes to avoid.
What a column is
A column is one of the repeating vertical tracks that divide the content area of a page. Content blocks (text, images, cards) line up with the edges of these tracks. The columns themselves are usually invisible. Readers see their effect as consistent alignment.
A column is not the same as a CSS column-count column. CSS multi-column layout flows one stream of text through several newspaper-style columns. A layout grid is a set of tracks where you place separate blocks. This chapter is about the second kind.
Column width is never chosen in isolation. Three values determine it:
- The container width: the total space the grid occupies.
- The gutter: the gap between neighboring columns, covered in the next chapter.
- The column count.
The relationship is:
column width = (container width - gutter x (count - 1)) / countA grid with N columns has N - 1 gutters, because gutters sit only between columns. Forgetting this is a common source of arithmetic errors.
Worked example: computing a column width
Take a 72rem container, which is 1152px at a 16px root font size. Use 12 columns and a 24px gutter (1.5rem).
- Total gutter space: 11 gutters x 24px = 264px.
- Space left for columns: 1152px - 264px = 888px.
- Width of one column: 888px / 12 = 74px.
A block that spans 4 columns covers 4 columns and 3 gutters inside it: 4 x 74 + 3 x 24 = 368px. Note that a span includes the gutters it crosses. Spans of different sizes therefore do not scale in simple proportion to their column counts. A 4-column block is not one third of 1152px plus nothing else: 368px plus its share of the outer gutters is what makes three of them fill the row.
The same check for three 4-column blocks: 3 x 368 + 2 x 24 = 1152px. The numbers close exactly.
Choosing a column count
There is no correct number of columns. The count is a trade-off between flexibility and simplicity.
| Count | Divides evenly into | Typical use |
|---|---|---|
| 4 | 1, 2, 4 | Narrow screens, phones |
| 6 | 1, 2, 3, 6 | Simple content pages, tablets |
| 8 | 1, 2, 4, 8 | Tablet layouts |
| 12 | 1, 2, 3, 4, 6, 12 | Desktop layouts, general purpose |
| 16 | 1, 2, 4, 8, 16 | Dense dashboards, complex editorial layouts |
The column that "divides evenly" matters because it tells you which equal splits are possible. Twelve is popular because it supports halves, thirds, quarters, and sixths. It also supports unequal pairs such as 8 + 4 (content plus sidebar) and 9 + 3.
Use these criteria to decide:
- Layout variety: if you need halves, thirds, and quarters on the same page, choose 12. If you only need halves and quarters, 8 or 4 is enough.
- Available width: more columns on a narrow screen produce columns too small to be useful. A 12-column grid on a 360px phone gives columns of about 10px after gutters, which no content can sensibly occupy.
- Team discipline: every extra column adds placement choices. More choices mean more inconsistency between pages.
- Design system fit: many design systems fix 4 columns on phones, 8 on tablets, and 12 on desktops. Following that convention makes your grid familiar to other designers.
Column width: fixed, flexible, or minimum-bound
In CSS you can describe each column in several ways, and the choice changes how the grid behaves.
| Track value | Behavior | Risk |
|---|---|---|
| `74px` | Fixed width, never changes | Overflows on narrow screens |
| `1fr` | Shares the free space equally | Content can force a column wider |
| `minmax(0, 1fr)` | Shares free space and may shrink to zero | None of the above, but long content may be clipped or overflow visually |
| `minmax(16rem, 1fr)` | Never narrower than 16rem | Can overflow if the container is narrower than 16rem |
The fr unit is a fraction of the free space left after fixed sizes and gaps are subtracted. Twelve tracks of 1fr each receive one twelfth of that free space, so the grid does the arithmetic from the worked example for you.
Why `1fr` can misbehave
A plain 1fr track has an automatic minimum size equal to the content's minimum width. A long unbreakable string, such as a URL or a wide image, can push a column wider than its share and break the grid. Using minmax(0, 1fr) removes that minimum, so tracks stay equal.
.grid {
display: grid;
grid-template-columns: repeat(12, minmax(0, 1fr));
column-gap: 1.5rem;
}
.grid img {
max-width: 100%;
height: auto;
}With this rule, all twelve tracks stay equal regardless of content. The image rule keeps images inside the track they occupy. Overflowing content then needs its own handling, such as overflow-wrap: anywhere on text.
Setting up the markup and the columns
This markup is the base for the examples that follow in this tutorial.
<div class="page">
<div class="grid">
<main class="main">Main content</main>
<aside class="sidebar">Sidebar</aside>
</div>
</div>The next block defines a 12-column grid and places two items. Here the grid has twelve equal flexible columns, and the items span 8 and 4 of them.
.page {
max-width: 72rem;
margin-inline: auto;
padding-inline: 1rem;
}
.grid {
display: grid;
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}
.main {
grid-column: span 8;
}
.sidebar {
grid-column: span 4;
}The repeat(12, 1fr) function is shorthand for writing 1fr twelve times. The span 8 and span 4 values add up to 12, so the two items fill one row. If the spans added up to more than 12, the second item would wrap to a new row.
Column count and readable line length
Columns also affect text. A paragraph that spans all 12 columns of a wide desktop grid can run past 120 characters per line, which is hard to read. A common guideline is 45 to 75 characters per line.
Instead of making text narrower with an arbitrary max-width, span it across a sensible number of columns. For example, a body-text block that spans 8 of 12 columns usually lands in a comfortable range, and the remaining 4 columns can hold notes, images, or empty space. This ties text width to the grid, so the page keeps its alignment.
Edge cases and pitfalls
- Fractional pixel widths: with
frunits, a column can compute to a value like 73.67px. Browsers handle this by snapping to device pixels, which can leave hairline differences of one pixel between rows. This is normal. Avoid fixing it by hard-coding pixel widths, which reduces flexibility. - Uneven columns: when the container width is not divisible as you expect,
frcolumns are still equal. Hard-coded widths are the cause when columns look different. - Too many columns on small screens: reduce the count with a media query rather than shrinking columns. The chapter on responsive grids covers this.
- Counting gutters wrongly: when you do manual math for a design tool, remember N - 1 gutters, not N.
- Spans larger than the grid:
grid-column: span 14in a 12-column grid creates extra implicit columns, which usually causes horizontal overflow. Keep spans at or below the explicit column count. - Padding on grid items: padding added to an item inside a column is spent from that item's width, not from the grid. Prefer
box-sizing: border-boxso item padding does not make an item exceed its tracks.
Summary of decisions
- Fix the container width and the gutter first.
- Choose the lowest column count that supports the splits your design needs.
- Let CSS compute column width with
repeat()andfr, usingminmax(0, 1fr)when content might be wide. - Make spans add up to the column count on each row.
- Tie text blocks to a span that keeps line length readable.
Exercise
A container is 60rem wide (960px at 16px per rem). You want 12 columns with a 1.5rem (24px) gutter. What is the width of one column in pixels, and how wide is an element that spans 3 columns?
Show answer
There are 11 gutters, so gutter space is 11 x 24 = 264px. The columns share 960 - 264 = 696px, so one column is 696 / 12 = 58px. A 3-column span covers 3 columns and 2 gutters: 3 x 58 + 2 x 24 = 174 + 48 = 222px.
Exercise
Your 12-column grid uses repeat(12, 1fr). One column grows wider than the others after a long URL is added to its content. What change fixes this, and why does it work?
Show answer
Change the track definition to repeat(12, minmax(0, 1fr)). A plain 1fr track has an automatic minimum equal to its content's minimum width, so the unbreakable URL stretches the track. minmax(0, 1fr) sets the minimum to zero, so the tracks share the free space equally. You may also need overflow-wrap: anywhere on the text so the URL wraps instead of overflowing visually.