Sub topic 14
Testing between the breakpoints
This chapter assumes you can write mobile-first CSS with min-width media queries. It also assumes you know what a fluid layout is and have seen the layout, navigation, type, and image patterns from earlier chapters. Here you learn to check a responsive layout at every width, not only at the widths you designed for.
Why the breakpoints are the wrong place to look
Most layouts are reviewed at three widths: a phone frame, a tablet frame, and a desktop frame. Those are the widths where you chose the design. They are also the widths where the layout is most likely to look right.
Real users do not have your frame widths. A phone in landscape, a split-screen browser window, a foldable device, a desktop window dragged narrow, and a browser zoomed to 200% all produce widths that sit between your frames. A layout can fail there even if each frame is perfect.
Typical failures between breakpoints:
- A two-column layout starts at
48emand the columns are too narrow just above that width. - A one-column layout stays stretched across a wide tablet, so lines of text become very long.
- A navigation bar switches to its desktop form at a width where the links no longer fit and wrap onto a second line.
- A fixed-width element, such as a table or an image with a set
width, causes horizontal scrolling at one narrow width.
The goal of testing is to find the widths where the content breaks, which is the same signal you use to choose breakpoints. Testing and choosing breakpoints are one loop: a failure between frames often means you need a new breakpoint, or that a rule should be fluid instead.
Resize continuously, not in jumps
Browser developer tools have a device toolbar with presets such as "iPhone" or "iPad". Presets are useful for a quick look, but they jump between a few fixed sizes. They hide exactly the problems this chapter is about.
Use the responsive mode instead. Drag the edge of the viewport slowly from about 320px up to 1600px and watch the layout. While you drag, look for these signs:
- Horizontal scrollbar appears. Something is wider than the viewport.
- Text overlaps or is clipped.
- Lines of body text become longer than about 75 characters or shorter than about 30.
- Gaps and padding look cramped or oversized.
- A control becomes too small to tap, or two controls touch.
- A layout change happens suddenly in a place you did not plan.
Then choose a handful of widths to test systematically. A useful set includes the narrowest width you support, each breakpoint, one pixel below each breakpoint, and a few widths in the middle of each range.
| Width | Why test it |
|---|---|
| 320px | Narrowest common phone width and a common minimum to support |
| 375px to 430px | Typical phone widths |
| 47.9375em (767px) | One pixel below the tablet breakpoint |
| 48em (768px) | Exactly at the tablet breakpoint |
| 1024px | Wide tablet or small laptop, often in the middle of a range |
| 79.9375em (1279px) | One pixel below the desktop breakpoint |
| 80em (1280px) | Exactly at the desktop breakpoint |
| 1920px and wider | Large monitors, where a missing `max-width` shows up |
Testing one pixel on each side of a breakpoint checks that the rules switch where you expect. It is also the quickest way to confirm that a min-width query and any max-width query you wrote do not overlap or leave a gap.
Em-based queries and zoom
Media queries written in em or rem are based on the browser's default font size, not on the root font size you set in CSS. This is why the earlier examples used 48em and 80em. When a user raises their default font size or zooms the page, the effective viewport width in CSS pixels shrinks. A query written in em then responds, so the layout moves to a simpler form when the text gets bigger.
A query written in px also responds to browser zoom, because zoom changes CSS pixel width. It does not respond to a larger default font size set in the browser's settings. So the two behave the same under zoom and differently under a font size preference.
Test this directly:
- Set the browser zoom to 200% at a desktop window width of 1280px. The effective width is 640px, so you should see your mobile or tablet layout.
- Set it to 400%. The effective width is 320px, and nothing should require scrolling in two directions, apart from content such as data tables or maps that needs it.
- In the browser settings, raise the default font size and reload. Check that text still fits its containers.
These checks relate to accessibility requirements for reflow. A layout that works at 320 CSS pixels wide works at 400% zoom on a 1280px display.
Test the layout, not only the screenshot
A visual check cannot catch everything. Add a few automated checks that run at many widths. This sketch uses Playwright to resize a page and fail when the page scrolls horizontally.
// tests/no-overflow.spec.js
// Requires: npm install -D @playwright/test
const { test, expect } = require('@playwright/test');
const widths = [320, 375, 414, 600, 767, 768, 900, 1024, 1279, 1280, 1440, 1920];
for (const width of widths) {
test(`no horizontal overflow at ${width}px`, async ({ page }) => {
await page.setViewportSize({ width, height: 900 });
await page.goto('http://localhost:3000/');
const overflow = await page.evaluate(() => {
const root = document.documentElement;
return root.scrollWidth - root.clientWidth;
});
expect(overflow).toBeLessThanOrEqual(0);
});
}Change the URL to match your own development server. The test reads scrollWidth, the full width of the content, and clientWidth, the width of the visible area. When the first is larger, something overflows.
This check finds one class of failure only. It does not tell you that text is hard to read or that a button is too small. For those, you can add checks of your own, such as the size of tap targets, or take screenshots at each width and compare them with earlier ones. Screenshot comparison is useful for catching a change you did not mean to make, but it needs a human to decide whether a difference is a bug.
Find the element that overflows
When the test fails, you need the element responsible. Paste this into the browser console at the width where the scrollbar appears.
const viewportWidth = document.documentElement.clientWidth;
const offenders = [...document.querySelectorAll('body *')].filter((el) => {
const rect = el.getBoundingClientRect();
return rect.right > viewportWidth + 0.5 || rect.left < -0.5;
});
console.table(
offenders.map((el) => ({
tag: el.tagName.toLowerCase(),
class: el.className,
right: Math.round(el.getBoundingClientRect().right),
}))
);The list often includes many children of one wide parent. Look for the outermost element in the list, since fixing it usually fixes the rest.
Common causes and fixes:
| Cause | Fix |
|---|---|
| Image or video with a fixed `width` | Use `max-width: 100%` and `height: auto` |
| Long word or URL in a narrow column | Add `overflow-wrap: anywhere` |
| Grid column that cannot shrink | Use `minmax(0, 1fr)` instead of `1fr` |
| Flex child with long content | Add `min-width: 0` to the child |
| Wide table | Wrap it in a container with `overflow-x: auto` |
| `100vw` width on an element | Use `100%`, because `100vw` includes the scrollbar width |
A grid track written as 1fr has an implicit minimum of the content's minimum size. That is why a long unbroken word can push a grid wider than the screen. Writing minmax(0, 1fr) removes that minimum.
.layout {
display: grid;
grid-template-columns: minmax(0, 1fr);
}
@media (min-width: 48em) {
.layout {
grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
}
}
.layout > * {
overflow-wrap: anywhere;
}Check the extremes of content
Layouts are tested with tidy placeholder text, and real content is rarely tidy. Repeat your resize pass with content that stresses the layout:
- A very long heading, and a very short one.
- A single long word, such as a German compound noun or a URL.
- A card with no image, and a card with a tall image.
- A list with one item, and a list with fifty.
- Right-to-left text, if your product supports it.
- A translated interface, since many languages are 30% longer than English.
These checks matter most for navigation. A bar that fits five English labels may not fit five translated ones. The point where the navigation switches from the menu button to the full bar should depend on the real labels, not on a width you picked in the design tool.
Check input and orientation
Width is not the only variable. Run these extra checks on a real device when you can, and in developer tools when you cannot.
- Rotate a phone to landscape. Its width may reach your tablet breakpoint, but its height is small. Sticky headers and full-height sections can take up most of the screen.
- Use the layout with a touch screen. Every control should be reachable with a thumb, and nothing should depend on hover. The
(hover: hover) and (pointer: fine)query from the earlier chapter keeps hover styling away from touch devices. - Use the layout with only a keyboard. Tab through the page at a narrow width and at a wide one. A menu that is hidden at mobile widths must still be reachable and must not trap focus.
- Emulate a slow connection and check that the right image from
srcsetloads at each width. The network panel shows the file name and size that was requested.
Developer tools can emulate touch and orientation, but they cannot reproduce a real mobile browser's address bar, which grows and shrinks as you scroll. If your layout uses 100vh, test on a real phone. The dvh unit tracks the visible height as the address bar changes, and you can use it where full-height sections matter.
A repeatable checklist
Run this list before you hand a layout over, and again after any change to the breakpoints.
- Drag from 320px to 1920px and note every width where something breaks.
- Test one pixel on each side of every breakpoint.
- Run the overflow test at the widths in your list.
- Zoom to 200% and 400% at desktop width.
- Try long, short, empty, and translated content.
- Rotate a phone to landscape.
- Tab through the page at a narrow width and a wide one.
- Record any new breakpoint as a design token, and update the matching design frame.
The last step keeps the design file and the CSS in agreement. If testing shows that the layout needs a breakpoint at 40em, add that value to your tokens and add a frame at that width, so the next designer sees what the code does.
Exercise
Your layout switches to two columns at min-width: 48em. At 800px wide, the right column shows a long URL that pushes the page wider than the screen, and a horizontal scrollbar appears. At 1280px wide there is no problem. Name two CSS changes that fix the cause without adding a breakpoint, and say how you would confirm the fix.
Show answer
The grid probably uses 1fr tracks, whose minimum size is the content's minimum size, so the long URL stops the column from shrinking. Change the tracks to minmax(0, 1fr) and add overflow-wrap: anywhere to the content. To confirm, resize to 800px, 767px, and 768px and check that the horizontal scrollbar is gone. Then run the overflow test at those widths, or use the console script to confirm that no element extends past clientWidth.
Exercise
A page is designed with frames at 375px, 768px, and 1280px. A user opens it in a desktop window that is 1000px wide and zoomed to 200%. What effective width does the layout see, and which layout should appear? What does this show about testing only at the frame widths?
Show answer
At 200% zoom a 1000px window gives an effective width of 500 CSS pixels. That is between the mobile and tablet frames, so the mobile-first single-column layout should appear, since the 48em query (768px) does not match. This shows that users reach widths that no frame covers, so you must check the layout across the whole range, and at zoom levels, not only at the three designed widths.