Sub topic 14
Common pitfalls and how to avoid them
This chapter assumes you can already build a 12-column grid with display: grid, repeat(), fr units, gap, and grid-column, and that you understand box-sizing. Most grid bugs come from a small set of causes. Each section below names a symptom, explains the mechanism behind it, and gives a fix.
Pitfall 1: Columns that stretch because of their content
Many people read 1fr as "an equal share of the space." The real meaning is minmax(auto, 1fr). The auto minimum lets a track grow to fit its item's minimum content size. A long URL, a wide table, or a <pre> block can therefore push one column wider than the others. The twelve columns are no longer equal, and everything after that point is misaligned.
<div class="grid">
<main class="main">
<p>https://example.com/a/very/long/path/that/has/no/spaces/or/break/opportunities/at/all</p>
</main>
<aside class="sidebar">Sidebar</aside>
</div>.grid {
display: grid;
grid-template-columns: repeat(12, minmax(0, 1fr));
column-gap: 1.5rem;
}
.main {
grid-column: span 8;
overflow-wrap: anywhere;
}
.sidebar {
grid-column: span 4;
}Two changes fix it:
minmax(0, 1fr)lowers the track minimum to zero, so the track can no longer be forced wider than its share.overflow-wrap: anywherelets the long string break. Without it, the text now overflows the item instead of the track. That is better, because the grid stays intact, but the text still spills out.
A grid item also has min-width: auto. If you nest a flex container, a scrolling table, or a code block inside an item, you may need min-width: 0 on the item as well.
Pitfall 2: Spans that are larger than the grid
Suppose a layout uses a 12-column grid on wide screens and a 4-column grid on phones. If .main { grid-column: span 8; } is still active on the phone, the item asks for eight columns in a four-column grid. The browser does not clamp the request. It creates implicit columns to hold the item, and the page overflows sideways.
The fix is to write mobile first. Give small screens a simple rule, and add spans only inside a breakpoint where the column count supports them.
.grid {
display: grid;
grid-template-columns: repeat(4, minmax(0, 1fr));
gap: 1rem;
}
.main,
.sidebar {
grid-column: 1 / -1;
}
@media (min-width: 48em) {
.grid {
grid-template-columns: repeat(12, minmax(0, 1fr));
gap: 1.5rem;
}
.main {
grid-column: span 8;
}
.sidebar {
grid-column: span 4;
}
}The line 1 / -1 means "from the first line to the last explicit line." It works at any column count, so it is a good default span. Use it instead of span 4 or span 12 when you mean "full width."
Pitfall 3: Inconsistent spacing
A grid can be correct and still look uneven. These are the usual causes.
- Margins on top of
gap. If each item hasmargin-right: 1.5remand the grid hascolumn-gap: 1.5rem, the real gutter is 3rem. Put all spacing between tracks ingap. Leave item margins at zero. - Mixed units. A
20pxgutter next to1.5remmargins drifts apart when the user changes the browser font size. Chooseremfor gutters and margins and keep it throughout. - Container padding with the wrong
box-sizing. With the defaultcontent-box,max-width: 72rempluspadding-inline: 1remproduces a box that is 74rem wide. Setbox-sizing: border-box, or use the global reset below. width: 100vw.100vwincludes the width of the vertical scrollbar on many systems. A full-width element then becomes a few pixels wider than the visible page, and a horizontal scrollbar appears. Usewidth: 100%or let a block element fill its parent.
*,
*::before,
*::after {
box-sizing: border-box;
}
.page {
max-width: 72rem;
margin-inline: auto;
padding-inline: 1rem;
}Pitfall 4: Auto-fit grids with a minimum that is too big
The self-adjusting card grid uses repeat(auto-fit, minmax(16rem, 1fr)). It has a flaw. If the container is narrower than 16rem, for example a 15rem-wide phone viewport after the margins are subtracted, the single column cannot shrink below 16rem and the grid overflows. Wrapping the minimum in min() removes the problem.
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
gap: 1.5rem;
}Also be aware of the difference between auto-fit and auto-fill:
| Keyword | Behavior with few items | When to choose it |
|---|---|---|
| `auto-fit` | Empty tracks collapse, so the existing cards stretch to fill the row | Cards should always fill the width |
| `auto-fill` | Empty tracks are kept, so the cards stay at their minimum size and leave a gap at the end | Card width should stay consistent when there are only one or two cards |
An auto-fit grid with one card makes that card as wide as the whole container. If that looks wrong, switch to auto-fill, or cap the width of the cards.
Be aware that an auto-fit grid does not use a 12-column structure. It is a different tool. Do not combine it with span 8 rules on the cards, because the column count changes with the container width.
Pitfall 5: Visual order that differs from source order
Grid lets you place an item anywhere. That power is risky. Keyboard focus, screen readers, and reading mode follow the order of the HTML, not the order on screen. If you move the sidebar to the left visually while it stays after the main content in the markup, a keyboard user tabs through the page in a different order than they see it.
<div class="grid">
<aside class="sidebar">
<a href="/archive">Archive</a>
<a href="/about">About</a>
</aside>
<main class="main">
<a href="/post/1">Read the first post</a>
</main>
</div>.grid {
display: grid;
grid-template-columns: repeat(12, minmax(0, 1fr));
column-gap: 1.5rem;
}
.sidebar {
grid-column: 1 / span 4;
}
.main {
grid-column: 5 / span 8;
}Here the markup order and the visual order agree, so the tab order is natural. If the design wants the sidebar on the right, change the placement and not the markup: grid-column: 9 / span 4 for the sidebar and 1 / span 8 for the main content. This still places the sidebar visually after the main content, matching the order a reader would expect, and it needs no order property.
Use these rules:
- Write the HTML in the order that makes sense when read alone, with no CSS.
- Use
grid-columnto place items. Avoidorderfor anything that contains links or form controls. - Test by pressing Tab through the page. The focus should move in a pattern that matches what you see.
Pitfall 6: Nested grids that drift off the page grid
A nested element with its own grid-template-columns: repeat(12, 1fr) creates a new twelve-column grid inside its parent's area. Its columns are 12 shares of a narrower space, so they do not line up with the page columns above and below. The gutters do not match either.
subgrid solves this. The nested grid borrows the tracks of its parent, so its children land on the page's column lines.
.grid {
display: grid;
grid-template-columns: repeat(12, minmax(0, 1fr));
column-gap: 1.5rem;
}
.article {
grid-column: 1 / -1;
display: grid;
grid-template-columns: subgrid;
}
.article .figure {
grid-column: 1 / span 4;
}
.article .text {
grid-column: 5 / span 8;
}Remember that the subgrid element must span the tracks you want to inherit. In the example, .article spans all twelve columns, so the numbers inside it (1 / span 4, 5 / span 8) refer to the same columns as the page. If .article spanned only six columns, its subgrid would have six columns, and 5 / span 8 would create implicit tracks.
Subgrid is supported in current versions of all major browsers. If you must support older ones, put the subgrid rules inside @supports (grid-template-columns: subgrid) and give the fallback a simple layout.
Pitfall 7: Breakpoints and gutters that ignore user settings
Two smaller issues cause quiet problems.
- Pixel breakpoints.
@media (min-width: 768px)ignores a user who raised the default font size. With48em, the breakpoint scales with that size, so the layout changes earlier when text is larger and lines do not become too narrow. - Percentage gutters. A gutter of
2%changes with the width of the container, so it can become very small on a narrow screen and very large on a wide one. Aremvalue or aclamp()value is more predictable, for examplecolumn-gap: clamp(1rem, 2vw, 2rem).
Diagnosing a broken grid
The symptom often points to the cause. The flowchart gives an order in which to check.
The table below summarizes the chapter.
| Symptom | Likely cause | Fix |
|---|---|---|
| One column wider than the others | `1fr` grows to fit content | `minmax(0, 1fr)`, `overflow-wrap: anywhere` |
| Sideways scroll on phones only | Span larger than the column count | Mobile-first spans, `1 / -1` |
| Gutters twice as wide as intended | Margins plus `gap` | Use `gap` only |
| Page slightly wider than the screen | `100vw` or content-box padding | `width: 100%`, `box-sizing: border-box` |
| Card grid overflows a small screen | Fixed minimum in `minmax()` | `minmax(min(100%, 16rem), 1fr)` |
| Tab order jumps around | Visual order differs from source | Reorder the markup, avoid `order` |
| Nested content misses the page columns | Separate nested grid | `grid-template-columns: subgrid` |
Exercise
A page uses grid-template-columns: repeat(4, 1fr) on phones. One article has grid-column: span 6 and is not inside a media query. Users report a horizontal scrollbar on phones. Explain why it happens and write the fix.
Show answer
The article asks for six columns in a four-column grid. The browser creates two implicit columns to hold it, so the grid becomes wider than the container. The fix is to make the small-screen rule fit the explicit grid, for example .article { grid-column: 1 / -1; }, and to move grid-column: span 6 into a @media (min-width: 48em) block where the grid has twelve columns. Using minmax(0, 1fr) in the column definition also stops content from widening the tracks.
Exercise
A designer wants the sidebar on the left on desktop, but the HTML lists the main content first for readers who use a screen reader. Which approach avoids a mismatch between visual and keyboard order, and why is order: -1 a poor choice for a sidebar that contains links?
Show answer
Put the sidebar first in the HTML and place it with grid-column: 1 / span 4, with the main content at 5 / span 8, so source order and visual order agree. If the content must stay first in the markup, keep it first visually as well. order: -1 changes only the painted position. Keyboard focus and screen readers still follow the HTML, so a user tabbing through the sidebar links would jump to a part of the page that is not where they expect.