Sub topic 6
Choosing breakpoints from your content
This chapter assumes you can write a basic min-width media query, you know the Flexbox and Grid basics, and you can resize a page in browser developer tools. You already have single-column base styles and a viewport meta tag. The remaining question is where the layout should change.
What a breakpoint is
A breakpoint is a viewport width at which your CSS changes the layout. In mobile-first CSS, each breakpoint is a min-width media query that adds rules once there is enough room for them.
/* Base: small screens */
.layout {
display: grid;
gap: 1rem;
padding: 1rem;
}
/* Wider screens: add, don't undo */
@media (min-width: 48em) {
.layout {
grid-template-columns: 2fr 1fr;
padding: 2rem;
}
}The query above says "from 48em upward, use two columns." The number 48em is a decision. This chapter is about how to make that decision well.
Why device-based breakpoints fail
A common habit is to pick widths from a list of popular devices: 375px for a phone, 768px for a tablet, 1024px for a laptop. This approach has three problems.
- The list goes stale. New phones, foldables and tablets appear every year, and their widths rarely match the numbers you chose.
- Devices do not map to widths. A desktop browser window can be 500px wide. A tablet can show your page in split-screen at 320px. A phone in landscape can be wider than an old tablet in portrait.
- It ignores your design. A breakpoint at 768px helps only if something in your layout actually needs to change at 768px.
Your page has no knowledge of devices. It knows only the width of the viewport, so the width is the only thing you should design for.
Letting the content decide
The content-driven method is to start narrow and widen the viewport until the design stops working. That point is your breakpoint.
- Open the page in developer tools with the narrowest viewport you support, usually 320px.
- Check that the base styles look good there.
- Drag the viewport wider slowly.
- When something looks wrong, stop and note the width. Typical signs are lines of text that are too long, large empty gaps, or a component that has room for a second column.
- Add a
min-widthquery at that width, and write the smallest change that fixes the problem. - Continue widening and repeat.
Here is the same loop as a diagram.
What "stops working" looks like
Watch for these signals:
- Line length. Body text becomes uncomfortable to read when lines run much past about 75 characters. A text column that is stretching too wide is a strong reason to add a breakpoint or to cap the width.
- Wasted space. Cards that are 900px wide with two words each, or a form field that fills a whole monitor.
- Cramped content. Navigation links that wrap onto three lines, or a table that forces horizontal scrolling.
- Missed opportunity. There is room for a sidebar beside the article, or for three cards in a row instead of one.
Your content also changes the answer. A page with long German labels needs a wider navigation breakpoint than the same page in English. Test with real text, not placeholder text.
Worked example: a navigation bar
Suppose you have five links in a navigation list. In the base styles they stack vertically:
.nav {
display: flex;
flex-direction: column;
gap: 0.5rem;
padding: 0;
list-style: none;
}
.nav a {
display: block;
padding: 0.75rem 1rem;
}As you widen the viewport, the stacked links look fine until the page is wide enough for all five to sit on a single row. Suppose you measure this and find that the links fit side by side at 36em. That is your breakpoint:
@media (min-width: 36em) {
.nav {
flex-direction: row;
gap: 1rem;
}
}If the marketing team later adds a sixth link, you will find the row wraps at 36em. The correct fix is to revisit the number, not to explain why 36em was chosen for a device.
Use em in media queries
Write breakpoints in em, not px. In a media query, em is relative to the browser's default font size, which is usually 16px. It is not relative to a font-size you set in your CSS. So 48em is 768px at the default setting.
The benefit appears when users change their text size or zoom in. If a user raises their default font size to 24px, 48em becomes 1152px, so the two-column layout waits until there is room for the larger text. A px breakpoint would switch to two columns while the text is too big for them.
/* Responds to the user's text size settings */
@media (min-width: 48em) {
.layout {
grid-template-columns: 2fr 1fr;
}
}
/* Ignores the user's text size settings */
@media (min-width: 768px) {
.layout {
grid-template-columns: 2fr 1fr;
}
}Browser zoom changes the effective viewport width in CSS pixels, so zooming in triggers narrow-screen layouts for both units. The difference applies to the font-size setting.
Fewer breakpoints, fewer problems
Every breakpoint adds a layout state you must test. Aim for as few as the design needs.
Many layout changes do not need a breakpoint at all. A grid can adapt on its own:
.cards {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
}This grid fits as many columns of at least 16rem as the container allows. It has no media query, so it has no breakpoint to maintain. Reserve media queries for structural changes: moving a sidebar beside the content, switching the navigation from stacked to horizontal, or changing the order of major regions.
A useful split:
| Type | What it changes | Example |
|---|---|---|
| Major breakpoint | Page structure | One column to a main column plus a sidebar |
| Minor breakpoint | A single component | Navigation links from stacked to a row |
| No breakpoint | Fluid behavior | `auto-fit` grids, `clamp()` text, `max-width: 100%` images |
Prefer the bottom row of the table whenever it works. Move down the table only when fluid behavior cannot do the job.
Organizing breakpoints in code
Group each query near the component it affects, or collect them at the end of the stylesheet. Whichever you choose, keep queries in ascending order of width. Because later rules win when specificity is equal, ascending order means a wider query correctly overrides a narrower one.
Two gotchas matter here.
Custom properties do not work in queries. This is invalid, because var() is not allowed inside a media condition:
:root {
--bp-medium: 48em;
}
/* Does not work: the query is ignored */
@media (min-width: var(--bp-medium)) {
.layout {
grid-template-columns: 2fr 1fr;
}
}If you want named breakpoints, use a comment convention, or a preprocessor or build tool that substitutes the value before the CSS reaches the browser.
Avoid mixing min-width and max-width. If one query uses max-width: 767px and another uses min-width: 768px, fractional widths from zoom or high-density screens can fall between them. Using only min-width queries removes this problem, and it matches the mobile-first model of adding rules as space grows.
Modern browsers also support the range syntax, which reads more like a comparison:
@media (width >= 48em) {
.layout {
grid-template-columns: 2fr 1fr;
}
}It means the same as min-width: 48em. Check your browser support requirements before using it, and fall back to min-width if you need older browsers.
Common mistakes
- Copying breakpoints from a framework. Frameworks choose values that suit their own components. Your content may need different ones.
- Testing only at breakpoint widths. Check in-between widths too, such as 600px, 900px and 1100px. Layouts often break just below a breakpoint, where a column is at its narrowest.
- Hiding content at small sizes. Using
display: noneto remove content on narrow screens makes mobile users second-class. If the content matters, find a compact way to present it. - Fixing a component problem with a page breakpoint. If one card looks wrong in a narrow sidebar on a wide screen, the viewport is not the issue. Container queries, covered later in this tutorial, handle this case.
Summary of the method
Start with a working narrow layout. Widen the viewport until something breaks. Add the smallest min-width query, in em, that fixes it. Prefer fluid techniques when they can do the work without a query. Keep queries in ascending order, and test between breakpoints as well as at them.
Exercise
Your two-column layout looks fine at 700px but the text column becomes cramped just before the sidebar fits comfortably. You measure that the sidebar needs at least 20em beside a main column of 30em, plus 2em of total gap and padding. At what width should the two-column query start, and why write it in em?
Show answer
The query should start at 52em (20 + 30 + 2), written as @media (min-width: 52em). Using em ties the breakpoint to the user's default font size, so a user with larger text gets the two-column layout later, when there is enough room for their larger text. A px value would switch layouts too early for them.
Exercise
A teammate proposes @media (max-width: 767px) for phone styles and @media (min-width: 768px) for tablet styles. Name one problem with this and describe the mobile-first alternative.
Show answer
Mixing max-width and min-width can leave gaps at fractional widths (for example 767.5px), where neither query matches. It also puts phone styles behind a query, which goes against the mobile-first model. The alternative is to write the phone styles as the unconditioned base and use only min-width queries to add rules as the viewport grows.