Sub topic 6
Choosing breakpoints from your content
This chapter assumes you can write single-column base styles and add layout changes with min-width media queries. A breakpoint is the viewport width at which a media query switches on new rules. Here you will learn how to pick those widths.
Why device sizes are a poor guide
Many tutorials list breakpoints such as 375px for phones, 768px for tablets and 1024px for laptops. These numbers go stale quickly. Phones, foldables and resizable desktop windows fill the gaps between them. A layout tied to a specific device breaks on the next one.
Your content is the better guide. A breakpoint belongs at the width where the layout starts to look wrong, not at the width of a particular product.
Finding a breakpoint by resizing
Start with your base styles, which already work on a narrow screen. Then widen the viewport slowly in your browser's responsive mode and watch for problems:
- Lines of text become too long to read comfortably, often past about 75 characters.
- Whitespace grows into large empty gaps.
- A single column of cards would be better as two columns.
- A navigation list that fit now wraps awkwardly.
When you see one, note that width and add a query there. The nav example below stacks links on small screens. It puts them in a row once there is room.
.nav {
display: grid;
gap: 0.5rem;
}
.nav a {
padding: 0.75rem;
}
@media (min-width: 40em) {
.nav {
grid-auto-flow: column;
justify-content: start;
}
}Writing breakpoints in em
Use em units in media queries instead of px. In a media query, 1em equals the browser's default font size, usually 16px. It does not depend on any element's own font size. So 48em is 768px by default. If a user raises their default text size, the breakpoint moves to a wider viewport. The layout then switches to multiple columns later, which leaves room for the larger text.
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}
@media (min-width: 48em) {
.layout {
grid-template-columns: 2fr 1fr;
padding: 2rem;
}
}Keeping the set small
Most sites need two to four breakpoints. Each one adds testing and maintenance work. Before you add a query, try whether a fluid technique removes the need for it. A grid using repeat(auto-fit, minmax(min(100%, 16rem), 1fr)) changes its column count without any query.
Keep breakpoints consistent across the stylesheet where it makes sense. If the nav and the main layout both switch near 40em and 48em, the page changes in a few clear steps. Many different thresholds make the page change at too many widths, which is harder to follow.
Common errors
| Problem | Cause | Fix |
|---|---|---|
| Query never matches on a phone | Missing viewport meta tag | Add `width=device-width, initial-scale=1` |
| Layout jumps at odd widths | Breakpoints copied from a device list | Resize and place them where the content breaks |
| Breakpoint ignores user text size | Written in `px` | Use `em` |
| `var(--bp)` in a query does nothing | Custom properties are not allowed in media queries | Write the value out |
| Component breaks inside a sidebar | Query measures the viewport, not the component | Use a container query |
The last row matters. A viewport breakpoint cannot know how much room a component actually has. Container queries handle that case and are covered in a later chapter.
Exercise
Your article text becomes hard to read when the viewport passes about 45em, because the lines get too long. Write the media query that limits the article's width and centers it from that point up. Why is em preferable to px here?
Show answer
Use @media (min-width: 45em) { article { max-width: 40rem; margin-inline: auto; } }. The em unit follows the user's default font size, so people who enlarge their text reach the breakpoint at a wider viewport. The layout then keeps room for the larger text, whereas a px value ignores that setting.