Sub topic 14
Common pitfalls and how to avoid them
This chapter assumes you can build a 12-column grid with display: grid, repeat(), fr units and gap, place items with grid-column, and write a media query. Each pitfall below has a symptom, a cause and a fix.
Pitfall 1: columns that overflow
A grid of repeat(12, 1fr) can still push the page sideways. The reason is that 1fr behaves like minmax(auto, 1fr). The auto minimum lets a column grow to fit its content: a long URL, a wide <pre> block or an image with a fixed width. One such item stretches its column and the grid becomes wider than its container.
Give the columns a minimum of 0 and let items shrink:
.grid {
display: grid;
grid-template-columns: repeat(12, minmax(0, 1fr));
column-gap: 1.5rem;
}
.grid > * {
min-width: 0;
}
img {
max-width: 100%;
height: auto;
}Use overflow-x: auto on code blocks so they scroll inside their own box and do not widen the page.
Pitfall 2: spans larger than the grid
On a four-column mobile grid, a leftover grid-column: span 8 asks for more tracks than exist. The browser creates implicit columns, sized by content, and the layout breaks. Set the span for each breakpoint, as in the responsive example from earlier chapters: 1 / -1 on small screens and span 4 from 48em.
Pitfall 3: inconsistent spacing
Spacing drifts when it comes from several places at once, for example gap plus a margin on each card plus padding on the container. Assign each part of the grid one owner:
| Part | Owner |
|---|---|
| Gutters | `gap` or `column-gap` on the grid |
| Margins | `padding-inline` on the container |
| Centering | `margin-inline: auto` with `max-width` |
Do not put side margins on grid items. They add to the gutter and make some columns look wider than others.
Pitfall 4: auto-fit grids on narrow screens
repeat(auto-fit, minmax(16rem, 1fr)) forces every column to be at least 16rem wide. In a container narrower than that, such as a phone with side margins, the column overflows. Wrap the minimum in min():
.cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
gap: 1.5rem;
}Pitfall 5: edge-to-edge content and `100vw`
Without side padding on the container, text touches the screen edge once the viewport is narrower than max-width. Keep padding-inline on the container. Avoid width: 100vw for full-width elements. 100vw includes the vertical scrollbar, so it is wider than the visible area and causes horizontal scrolling on some systems. Use width: 100% or leave the element at its default block width.
Pitfall 6: visual order that disagrees with source order
grid-column, grid-row and order change where an item appears. They do not change the order in the HTML. Keyboard focus, screen readers and the page without CSS all follow the source. If you place the sidebar visually before the main content but write it after, tab focus jumps in a way that looks random.
A quick debugging path
In your browser's developer tools, turn on the grid overlay for the container. It shows the real tracks and gutters, which usually exposes an extra implicit column or an item that is too wide.
Exercise
A card grid uses grid-template-columns: repeat(auto-fit, minmax(20rem, 1fr)). On a 320px-wide phone with 1rem of padding on each side, the page scrolls sideways. Why does this happen, and what is the fix?
Show answer
The container is about 288px wide (320px minus 32px of padding), but each column must be at least 20rem (320px with a 16px root font size), so the column overflows. Replace the minimum with min(100%, 20rem), as in minmax(min(100%, 20rem), 1fr), so the column can never be wider than its container.