Sub topic 7
Choosing breakpoints from your content
A breakpoint is a viewport width at which your layout changes because a media query starts to apply. This chapter covers how to decide where those widths go. It assumes you can write a min-width media query, you know the mobile-first approach from the previous chapter, and you are comfortable with px, em, and rem.
Why device-based breakpoints fail
Many stylesheets use breakpoints copied from a list of device sizes, such as 375px for a phone, 768px for a tablet, and 1024px for a small laptop. This looks tidy, but it has three problems:
- Device sizes change every year, so the list is out of date quickly.
- Windows are not devices. A desktop browser can be 500px wide, and a tablet can show your page in a split-screen pane.
- The numbers say nothing about your page. A layout can break at 700px even though no popular device is 700px wide.
The alternative is to let the content decide. You make the viewport narrower and wider, watch where the design stops looking right, and put a breakpoint there. The widths you end up with are specific to your page, and they stay valid when new devices appear.
The resize-and-break method
The method is a loop. You start with the narrowest layout and widen the viewport slowly. When something looks wrong, you add a min-width query at that width and fix the problem inside it. Then you keep widening.
Typical signs that you need a breakpoint:
- Lines of text become too long to read comfortably. A common target is 50 to 75 characters per line.
- Whitespace between elements grows until the design looks empty.
- A row of items, such as navigation links, no longer fits and wraps awkwardly.
- A column becomes so narrow that words break or buttons squash.
- A single column could now hold two without crowding.
A worked example
Here is a page with a header navigation and an article. In a narrow viewport, the links stack vertically. The breakpoint belongs at the width where the links fit on one row.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Breakpoint demo</title>
<style>
body {
font-family: system-ui, sans-serif;
margin: 0;
padding: 1rem;
}
nav a {
display: block;
padding: 0.75rem 0;
}
article {
max-width: 100%;
}
/* Found by resizing: the five links fit on one row at about 36em. */
@media (min-width: 36em) {
nav {
display: flex;
gap: 1.5rem;
}
nav a {
padding: 0.5rem 0;
}
}
/* Found by resizing: text lines exceed ~75 characters at about 60em. */
@media (min-width: 60em) {
article {
max-width: 65ch;
margin-inline: auto;
}
}
</style>
</head>
<body>
<nav>
<a href="#">Home</a>
<a href="#">Products</a>
<a href="#">Pricing</a>
<a href="#">Docs</a>
<a href="#">Contact</a>
</nav>
<article>
<h1>Welcome</h1>
<p>Resize the window and watch where the layout stops working.
Each breakpoint above was chosen because of something visible,
not because of a device.</p>
</article>
</body>
</html>The two widths, 36em and 60em, are tied to observable problems. If you later add a sixth link, the first breakpoint has to move, and you change one number.
Use em for breakpoints
Write breakpoint widths in em instead of px. In a media query, em is relative to the browser's default font size, which is usually 16px. It does not use any font-size you set in your own CSS.
This matters for users who increase their default font size. With a 16px default, 40em equals 640px. If a user raises the default to 24px, the same query triggers at 960px. Text takes more room at that size, so the layout switches to the wider arrangement later, which is what you want. A px breakpoint ignores that preference and can squeeze large text into columns that are too narrow.
Avoid gaps and overlaps
Mixing min-width with max-width is a common source of off-by-one errors. Viewports can have fractional widths on zoomed or high-density screens, so a pair like max-width: 599px and min-width: 600px can leave a gap between 599px and 600px.
Two safer approaches:
- Stay mobile-first and use only
min-width. Each query builds on the earlier ones, so no gap is possible. - When you need a bounded range, use range syntax, with
<on the upper bound so the ranges do not overlap:
@media (30em <= width < 60em) {
.sidebar {
display: none;
}
}How many breakpoints you need
Add as few as the design requires. Every breakpoint is another layout to test and maintain. A small content site often needs two or three. If you find yourself with eight, check whether fluid techniques can replace some of them.
| Problem | Try first | Add a breakpoint when |
|---|---|---|
| Heading too large or small | `clamp()` for font size | Never, `clamp()` scales smoothly |
| Card grid column count | `repeat(auto-fit, minmax(14rem, 1fr))` | You need exact column counts |
| Text line too long | `max-width` in `ch` units | Layout needs a sidebar beside the text |
| Navigation overflows | Flex wrapping | You want a different nav pattern |
| Images overflow | `max-width: 100%` | Never |
Fluid units and intrinsic layouts handle gradual change. Breakpoints are for abrupt structural changes, such as moving from a stacked layout to side-by-side columns or from a menu button to a full navigation bar.
Keep breakpoints consistent, but not sacred
Using the same few widths across a stylesheet keeps it predictable. Plain CSS does not allow custom properties in media queries, so @media (min-width: var(--bp-md)) does not work. Write the values out and put a comment near the top of the file listing them.
Consistency should not override content. If one component needs its own breakpoint at 47em, use it. Do not stretch the component to fit a global width. When a component's layout depends on the space it is given and not on the whole viewport, a container query is a better tool, and a later chapter covers it.
Common mistakes
- Choosing widths first, then forcing the design to fit. Start with the content, then record the widths you find.
- Testing only at the breakpoint values. Check widths just below and above each one, and drag through the whole range. Problems often appear between breakpoints.
- Ignoring zoom and text size. Test with browser zoom at 200 percent and with a larger default font.
- Using
max-widthqueries to patch a desktop-first design. This leads to overrides that fight earlier rules. Prefer building up from the narrow layout. - Setting breakpoints so close together that layouts overlap in intent. If two breakpoints are 30px apart, one of them is probably compensating for a flaw that a flexible rule would fix.
Exercise
Your stylesheet has a breakpoint at 768px because "that is the tablet width." A user who sets their browser's default font size to 24px reports that the two-column layout feels cramped. Explain why this happens, and rewrite the query @media (min-width: 768px) so it responds to the user's font size.
Show answer
A px breakpoint ignores the user's font size, so at 24px text the two columns switch on at 768px, where each column is too narrow for the larger text. Using em ties the breakpoint to the browser's default font size, so @media (min-width: 48em) triggers at 768px with a 16px default but at 1152px with a 24px default. The two-column layout then appears only when there is more room. Better still, find the real width by resizing, which may be a value other than 48em.
Exercise
You resize a page from narrow to wide. At 520px the three nav links first fit on one row, and at 900px the paragraph lines pass 80 characters. Write a mobile-first stylesheet fragment with two breakpoints in em that handle these two problems. Assume 16px = 1em.
Show answer
520px is 32.5em and 900px is 56.25em. For example: @media (min-width: 32.5em) { nav { display: flex; gap: 1rem; } } @media (min-width: 56.25em) { article { max-width: 65ch; margin-inline: auto; } } Both use min-width so they build on the base styles with no gaps or overlaps.