Sub topic 7
Choosing breakpoints from your content
This chapter assumes you know how to write a @media (min-width: ...) rule and understand the mobile-first approach from the previous chapter. You should also be comfortable with em and rem units.
What a breakpoint is
A breakpoint is a viewport width at which your layout changes. In CSS it is the number inside a media query, such as 40em in @media (min-width: 40em).
The question is where to put those numbers. Two approaches are common:
- Device-based: pick widths that match popular phones, tablets, and laptops.
- Content-based: pick widths where your own layout starts to look wrong.
Content-based breakpoints hold up better. Device sizes change every year, and a layout tuned for one phone can break on the next. Your content, such as line lengths, columns, and navigation links, stays the same.
Finding breakpoints by resizing
Follow this process with a mobile-first stylesheet:
- Start with the narrowest layout, with no media queries.
- Open the page in a browser and slowly widen the window.
- Stop when something looks bad. Typical signs are text lines that are too long, large empty gaps, or cards that could fit side by side.
- Note that width and add a
min-widthquery there. - Keep widening and repeat.
Suppose a card grid looks fine in one column until the window reaches about 640px. Beyond that, each card stretches too wide. That is your first breakpoint. Using em units keeps it in step with the user's font size, so 640px becomes 40em (640 ÷ 16).
.cards {
display: grid;
gap: 1rem;
grid-template-columns: 1fr;
}
/* First breakpoint: found by widening until cards got too wide */
@media (min-width: 40em) {
.cards {
grid-template-columns: repeat(2, 1fr);
}
}
/* Second breakpoint: three columns fit comfortably */
@media (min-width: 64em) {
.cards {
grid-template-columns: repeat(3, 1fr);
}
}Keep the number of breakpoints small
Every breakpoint is a layout you must test and maintain. Many pages need only two or three. If you add one to fix a single small flaw, first check whether fluid units or clamp() could solve it without a query.
Some teams define a shared set of widths so that components agree. This is reasonable, as long as the values come from real content needs and not from a list of devices.
Common errors
- Using pixel values for everything. Pixel breakpoints ignore the user's font size settings. Prefer
em. - Mixing
min-widthandmax-widthin one stylesheet. This breaks the mobile-first flow and makes the cascade harder to follow. - Overlapping or gapped ranges. If one query uses
max-width: 40emand another usesmin-width: 40em, both apply at exactly 40em. The59.99emtrick used earlier avoids this, and range syntax handles it more cleanly. - Naming breakpoints after devices. A variable called
$tabletbecomes misleading when a tablet is wider than your "desktop" value.
Exercise
Your single-column layout has body text lines that become hard to read once the window passes 720px. Using mobile-first CSS and em units, write the media query that begins the layout change at that width.
Show answer
720 ÷ 16 = 45, so use @media (min-width: 45em) { ... }. It is a min-width query because the base styles are for narrow screens and the new rules are added as the window grows. The width comes from the content problem (long lines), not from a device.