Sub topic 10
Changing the Grid at Different Screen Sizes
This chapter assumes you know basic HTML and CSS, the box model, and how to build a 12-column grid with display: grid, repeat(), and grid-column: span N. Here you will change the number of columns, the gutters, and the margins as the screen width changes.
Why one grid does not fit every screen
A 12-column grid with 1.5rem gutters works well on a laptop. On a phone 360px wide, the same grid leaves each column only a few pixels of content width. Text becomes unreadable.
Responsive grids solve this by changing three values at certain screen widths:
- The number of columns. Phones often use 4, tablets 8, and desktops 12.
- The gutter size. Smaller gutters on small screens save space.
- The margin size. Side margins grow as the screen grows.
The screen widths where the layout changes are called breakpoints. A breakpoint is written as a media query, a CSS rule that applies only when a condition such as min-width is true.
Start with the smallest screen
Write the narrow layout first, with no media query. Then add min-width queries that adjust the grid for larger screens. This is called mobile-first. Each larger layout only has to describe what changes.
<div class="page">
<div class="grid">
<article class="card">One</article>
<article class="card">Two</article>
<article class="card">Three</article>
</div>
</div>.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: 2rem;
}
.grid {
grid-template-columns: repeat(12, 1fr);
gap: 1.5rem;
}
.card {
grid-column: span 4;
}
}Below 48em (768px at the default font size), the grid has four columns and a 1rem gutter. Each card uses grid-column: 1 / -1. This means "start at the first line and end at the last line", so the card fills the full width and the cards stack.
From 48em upward, the grid switches to twelve columns with a 1.5rem gutter. Each card now spans 4 columns, so three cards sit in a row. The page padding also grows from 1rem to 2rem.
How the span interacts with the column count
The span value must make sense for the current column count. In the example, span 4 would fill the whole grid on a four-column layout. That is why the narrow layout uses 1 / -1 instead. Using -1 means "the last line" whatever the column count, so it keeps working if you later change the number of columns.
Choosing breakpoints
Pick breakpoints where your content breaks, not where specific devices happen to be. Resize the browser and watch for lines of text that become too long or too short, or columns that feel cramped. Add a breakpoint at that width.
Use em units in media queries. They respond to the user's font size settings, so people who enlarge their text get the wider layout sooner.
Most pages need only two or three breakpoints. Each extra one is another layout to test.
Common errors
- Forgetting the viewport meta tag. Without
<meta name="viewport" content="width=device-width, initial-scale=1">in the page<head>, phones render the page at a desktop width and your media queries do not fire as expected. - Spans larger than the column count. A
span 8on a four-column grid creates extra implicit columns and causes horizontal overflow. - Using
max-widthqueries mixed withmin-widthqueries. The two styles overlap and make the cascade hard to follow. Choose one direction, preferablymin-width. - Changing gutters without changing spans. A new column count usually needs new span values on the items.
Exercise
A grid uses grid-template-columns: repeat(4, 1fr) below 48em and repeat(12, 1fr) from 48em up. A .sidebar has grid-column: span 4. What does it look like on each side of the breakpoint, and what would you change so it fills the full width on narrow screens?
Show answer
Below 48em, span 4 fills all four columns, so the sidebar is full width. From 48em up, it covers four of twelve columns, one third of the width. It already fills the width on narrow screens, but it depends on the column count being 4. A safer rule is grid-column: 1 / -1 for the narrow layout, with span 4 set inside the min-width: 48em query.