Sub topic 14
Testing between the breakpoints
This chapter assumes you can write mobile-first CSS with min-width media queries, that you know what a breakpoint is, and that you have seen the layout patterns and navigation changes from earlier chapters. Here you check that the result holds up at every width, not only at the widths you designed for.
Why the breakpoints are not enough
Design frames show the layout at three widths, such as 375, 768 and 1280 pixels. Real screens fall everywhere in between. Most layout bugs show up at those in-between widths, because a rule written for one frame is stretched or squeezed past the point where it works.
Common failures include:
- A two-column layout that starts at 48em but looks cramped at 770px, because the columns are still too narrow for their content.
- A navigation bar that fits at 1280px but wraps onto two lines at 900px.
- Text that overflows its card at a width just above a breakpoint.
- Horizontal scrolling caused by one element with a fixed width.
Sweep the width
The most useful test is to resize the viewport slowly from narrow to wide and watch for the first thing that breaks. In browser developer tools, open responsive mode and drag the edge of the viewport. Do not jump between preset device sizes.
- Start at 320px, the narrowest width most layouts should still support.
- Drag the width up slowly to about 1600px.
- Pause where something looks wrong, such as wrapped labels, tiny columns or large empty gaps.
- Note the width. Decide whether to fix it with a fluid rule or with a new breakpoint, using the content-first approach from the earlier chapter.
Catch overflow automatically
Horizontal overflow is easy to miss by eye. This script lists elements that stick out past the right edge of the viewport. Paste it into the browser console at any width.
const viewportWidth = document.documentElement.clientWidth;
const offenders = [...document.querySelectorAll("body *")].filter((el) => {
const rect = el.getBoundingClientRect();
return rect.right > viewportWidth + 1;
});
console.log(`${offenders.length} element(s) overflow at ${viewportWidth}px`);
offenders.forEach((el) => console.log(el));Run it at several widths, especially just above and just below each breakpoint.
Test the edges of each breakpoint
Test one pixel on each side of every breakpoint. If your tablet query starts at 48em, which is 768px at the default font size, check 767px and 768px. Also check a width well inside each range, such as 600px and 1000px. A layout is only correct if all of these look right.
| Width | Why test it |
|---|---|
| 320px | Narrowest supported width |
| 767px | Last width before the tablet rule |
| 768px | First width with the tablet rule |
| 900px | Middle of the tablet range |
| 1279px | Last width before the desktop rule |
| 1280px | First width with the desktop rule |
| 1600px and up | Does the content stop growing? |
Test more than width
Width is one input. Users also change other settings that affect the layout.
- Text size. Raise the browser's default font size to 200%. Because
emandremmedia queries scale with it, breakpoints shift, and layouts should still work. - Zoom. Zooming to 200% on a 1280px screen gives a layout like a 640px viewport. Check that nothing is cut off.
- Orientation. Rotate a phone to landscape. A short, wide viewport can break fixed headers.
- Real devices. Emulators do not reproduce touch behavior, browser toolbars or real font rendering. Check at least one phone and one tablet.
Exercise
Your tablet breakpoint is @media (min-width: 48em). Name four widths you would test, and say what you are checking at each.
Show answer
Test 767px (the last width before the rule, which should still show the single-column layout), 768px (the first width with the rule, which should show the tablet layout), a mid-range width such as 900px (the columns should not be cramped and the navigation should fit), and 320px (the narrowest width, to confirm nothing overflows horizontally). Testing 1279px as well checks the end of the tablet range.
Exercise
After zooming a desktop browser to 200%, the layout looks like the mobile frame. Why does this happen?
Show answer
Zooming enlarges CSS pixels, so the viewport holds fewer of them. A 1280px window at 200% zoom has an effective width of 640 CSS pixels, so the media queries match as they would on a narrower screen.