Sub topic 10
Changing the Grid at Different Screen Sizes
This chapter assumes you know basic HTML and CSS syntax, the box model, and the 12-column grid from the earlier chapters: display: grid, repeat(), fr units, gap, and grid-column. Here you will change that grid as the viewport (the visible area of the browser window) gets wider or narrower.
Why one grid is not enough
A 12-column grid with a 1.5rem gutter works well on a laptop. On a phone 360px wide, the same grid leaves each column about 15px wide once the gutters are subtracted, which is too narrow to hold anything. Shrinking columns until they fit does not solve this. A responsive grid changes its structure, not only its size.
Three values usually change with screen width:
- Column count. Fewer columns on small screens, more on large ones.
- Gutter width. Narrower gutters on small screens, wider ones when there is room.
- Margins. Less outer space on small screens so content gets more of the width.
Items must also change how many columns they span. A card that spans 4 of 12 columns on a desktop should span all columns on a phone.
Choosing breakpoints
A breakpoint is a viewport width at which the layout changes. In CSS it is the condition of a media query, a rule block that applies only when a condition such as a minimum width is true.
Common column counts follow device classes:
| Viewport | Typical columns | Typical gutter | Typical side margin |
|---|---|---|---|
| Under 48em (about 768px) | 4 | 1rem | 1rem |
| 48em to 74.99em | 8 or 12 | 1.25rem to 1.5rem | 1.5rem |
| 75em and above | 12 | 1.5rem to 2rem | Set by the container's centering |
These numbers are starting points, not rules. A better way to pick breakpoints is to resize your real content and add a breakpoint where the layout starts to look wrong, such as lines becoming too long or cards too cramped. Do not tie breakpoints to specific devices, because device sizes keep changing.
Use em or rem units in media queries, as in 48em. These units scale with the user's font size setting, so a visitor who enlarges their text gets the wider layout rules at the right moment. Pixel-based queries ignore that setting.
Mobile-first with min-width
Mobile-first means you write the small-screen layout as the default and add rules for larger screens. You do this with min-width queries, which apply from a width upward.
The alternative is max-width queries, which start from the desktop layout and undo it for small screens. That approach means writing more overrides, and phones, which have the least processing power, have to evaluate the extra rules. With min-width, each larger layout builds on the one below it.
The markup stays the same as before:
<div class="page">
<div class="grid">
<article class="card">Card one</article>
<article class="card">Card two</article>
<article class="card">Card three</article>
</div>
</div>Now the CSS. The default is a 4-column grid where each card fills the row. From 48em the grid becomes 12 columns and each card spans 4.
.page {
max-width: 72rem;
margin-inline: auto;
padding-inline: 1rem;
}
.grid {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 1rem;
}
.card {
grid-column: 1 / -1;
}
@media (min-width: 48em) {
.page {
padding-inline: 1.5rem;
}
.grid {
grid-template-columns: repeat(12, 1fr);
gap: 1.5rem;
}
.card {
grid-column: span 4;
}
}The rule grid-column: 1 / -1 means "start at the first grid line and end at the last". The line number -1 counts from the end of the explicit grid, so the item spans every column however many there are. This is why it works as a "full width" value across breakpoints without editing.
Only the properties that change are repeated inside the media query. The .card rule at the small size still applies to everything else about the card.
What happens above the container's maximum width
The .page container has max-width: 72rem. Once the viewport is wider than that, the container stops growing and margin-inline: auto splits the leftover space equally on both sides. The grid inside keeps the same size. This means you rarely need a separate breakpoint for very large screens. The container handles it.
At widths between the small breakpoint and the maximum, the 12 columns use 1fr, so they stretch with the container. The columns are fluid in that range and effectively fixed beyond it.
Changing spans at each breakpoint
Different items need different spans at each size. A common layout is a main area with a sidebar. You can stack them on phones, give them a 8/4 split on tablets and desktops, and use a different ratio on wide screens.
.main,
.sidebar {
grid-column: 1 / -1;
}
@media (min-width: 48em) {
.grid {
grid-template-columns: repeat(12, 1fr);
column-gap: 1.5rem;
}
.main {
grid-column: span 8;
}
.sidebar {
grid-column: span 4;
}
}
@media (min-width: 75em) {
.main {
grid-column: span 9;
}
.sidebar {
grid-column: span 3;
}
}Each media query overrides only the span. The grid-template-columns value is set once, at 48em, and the 75em rule inherits it. Setting it again would be redundant.
Notice how the spans always add up to the column count: 4 + 4 + 4 = 12 for cards, 8 + 4 = 12 and 9 + 3 = 12 for the main and sidebar. If they add up to more than the column count, the extra item wraps to a new row. If they add up to less, you get empty columns at the end of the row. Check the sums when you change a count.
Using custom properties to centralize values
When many rules share the same gutter or margin, repeating the numbers makes them easy to get out of sync. CSS custom properties (variables you define with a -- prefix) let you change a value in one place per breakpoint.
:root {
--columns: 4;
--gutter: 1rem;
--margin: 1rem;
}
@media (min-width: 48em) {
:root {
--columns: 12;
--gutter: 1.5rem;
--margin: 1.5rem;
}
}
.page {
max-width: 72rem;
margin-inline: auto;
padding-inline: var(--margin);
}
.grid {
display: grid;
grid-template-columns: repeat(var(--columns), 1fr);
gap: var(--gutter);
}The media query now changes three numbers and nothing else. Every rule that uses --gutter follows along. This also keeps the margin and gutter consistent, which prevents the uneven spacing covered later in this tutorial.
One limit applies: custom properties cannot be used inside the condition of a media query, such as @media (min-width: var(--bp)). The condition is evaluated before variables are resolved, so that code does nothing. Write the breakpoint value directly.
Fluid gutters and margins
Stepping between values at breakpoints produces visible jumps when you resize. If you prefer a smooth change, use clamp(), which takes a minimum, a preferred value, and a maximum.
:root {
--gutter: clamp(1rem, 0.5rem + 2vw, 2rem);
--margin: clamp(1rem, 0.25rem + 3vw, 3rem);
}The vw unit is 1% of the viewport width. The gutter will not go below 1rem or above 2rem, and between those limits it grows with the screen. Keep the rem part in the preferred value. A value made only of vw ignores the user's zoom and font settings, which can hurt accessibility.
Column count cannot be fluid in this way, because it must be a whole number. Changing the column count always needs a breakpoint, or the auto-fit technique that a later chapter covers.
Edge cases and gotchas
- Items placed on explicit lines. If you write
grid-column: 5 / span 8and then the grid drops to 4 columns, line 5 does not exist in the explicit grid. The browser creates extra implicit columns, which usually causes horizontal overflow. Reset explicit placements inside the same breakpoint structure that changes the column count. - Spans larger than the column count.
span 4on a 4-column grid is fine.span 6on a 4-column grid also creates implicit columns. Use1 / -1for "full width". - Missing viewport meta tag. Without
<meta name="viewport" content="width=device-width, initial-scale=1">in the page head, mobile browsers pretend the screen is about 980px wide and your small-screen rules never apply. - Too many breakpoints. Each one doubles the combinations you must test. Two or three, plus fluid values such as
clamp(), is usually enough. - Content that forces width. A long unbroken word, a wide table, or an image without
max-width: 100%can push a1frcolumn wider than expected, because1frmeansminmax(auto, 1fr)andautorespects the content's minimum size. Addminmax(0, 1fr)ormin-width: 0on the item when that happens.
Checking your work
A quick checklist for each breakpoint:
- Does every row's spans add up to the column count?
- Do the gutter and margin values match the table or custom properties you defined?
- Is there any horizontal scrollbar?
- Does the reading order still make sense when the layout stacks?
The last question matters because changing spans at breakpoints is often combined with reordering. The next chapters on source order cover why visual order and source order should stay together.
Exercise
A page has a 4-column grid by default. At 48em it becomes 12 columns. A .feature item uses grid-column: 5 / span 8 and is written outside any media query. What goes wrong on a phone, and how do you fix it?
Show answer
On a phone the grid has only 4 columns, so line 5 is beyond the last explicit line. The browser adds implicit columns to hold the item, which usually makes the page scroll horizontally. Fix it by making the default rule grid-column: 1 / -1, and moving grid-column: 5 / span 8 into the @media (min-width: 48em) block, where the 12 columns exist.
Exercise
You write @media (min-width: var(--bp)) with --bp set to 48em on :root. The layout never changes. Why, and what do you do?
Show answer
Custom properties cannot be used inside media query conditions, so the condition is invalid and the block never applies. Write the value directly, as @media (min-width: 48em), and use custom properties only inside the rules.