Sub topic 16
Testing and Debugging Responsive Pages
This chapter assumes you can write media queries, know the mobile-first pattern, and have used the viewport meta tag. It covers how to check that a responsive page works, and how to find the cause when it doesn't.
Why testing needs a plan
A responsive page has no single "correct" appearance. It has a range of widths, input types, and user settings, and each combination can break differently. Testing every device is impossible, so test the ranges your CSS defines. Check each breakpoint, and check the widths just below and just above it.
A practical routine has three layers:
- Resize in the browser and watch for layout breaks.
- Use developer tools to emulate devices, settings, and print.
- Confirm on at least one real phone or tablet.
Start with the viewport tag
Many "my media queries don't work on my phone" problems come from a missing viewport tag. Without it, mobile browsers lay the page out at a desktop width (often around 980px) and scale it down. Your min-width: 40em rule then never matches the way you expect.
Check that the head of the document contains this line:
<meta name="viewport" content="width=device-width, initial-scale=1">Responsive mode in browser developer tools
Chrome, Edge, Firefox, and Safari all have a responsive design mode. In Chrome and Edge, open DevTools and toggle the device toolbar (Ctrl+Shift+M, or Cmd+Shift+M on macOS). In Firefox, use Ctrl+Shift+M or Cmd+Option+M.
Useful things to do there:
- Drag the width handle slowly from narrow to wide. A layout that is correct at 375px and 1280px can still break at 700px.
- Pick a preset device to set width, height, and pixel ratio together.
- Enter exact widths to test both sides of a breakpoint, such as 639px and 640px for
40emat a 16px base size. - Show the media query bars (Chrome and Edge). They list the queries in your stylesheet as clickable bands along the top of the viewport.
Emulation is not a real device. It does not reproduce the performance, touch behavior, browser toolbar changes, or font rendering of actual hardware.
Inspecting which rules apply
When a rule seems to be ignored, open the Elements panel (or Inspector in Firefox) and select the element. Then check three things.
- Is the media query matching? Rules inside a query that does not match appear greyed out or are missing from the Styles pane.
- Is the rule overridden? A struck-through declaration lost to a rule with higher specificity or a later position in the source.
- Is the computed value what you expect? The Computed tab shows the final value and which rule produced it.
Source order is the most common cause in mobile-first code. A min-width query must come after the base rule it overrides, because both have equal specificity.
/* Broken: the base rule comes later and wins at every width */
@media (min-width: 40em) {
.nav {
flex-direction: row;
}
}
.nav {
display: flex;
flex-direction: column;
}Moving the @media block below .nav fixes it.
Emulating user preferences and media types
You do not need to change your operating system settings to test preference queries. Chrome and Edge include a Rendering panel (open the DevTools command menu with Ctrl+Shift+P and type "Show Rendering"). It lets you emulate:
prefers-color-scheme: darkorlightprefers-reduced-motion: reduce- The
printmedia type, so you can inspect print styles on screen - Forced colors and other accessibility settings
Firefox offers color scheme and print emulation in the Inspector toolbar. Safari has similar controls in its Responsive Design Mode and Elements tab.
For hover and pointer queries, emulation helps only partly. Switching to a device preset in Chrome turns on touch emulation, so (hover: none) and (pointer: coarse) can match. Confirm the real behavior on a physical touch device.
For print, also use the browser's print preview. It shows page breaks and margins that the emulated media type does not.
A debugging flow for broken layouts
When the layout looks wrong at some width, work through this order.
Syntax mistakes that silently fail
A malformed media query does not raise an error. The browser treats the whole block as non-matching and skips it. Typical causes:
- A missing
andbetween conditions:@media (min-width: 40em) (max-width: 60em)is invalid. - A missing parenthesis around a feature:
@media min-width: 40emis invalid. - A mixed range:
@media (400px <= width <= 700px)works in current browsers but not in old ones. If you must support them, usemin-widthandmax-width. - A missing unit:
min-width: 600is invalid, because only0may be unitless.
Look at the Styles pane. If the whole @media block is absent or flagged with a warning, suspect the syntax.
Unit and base font size surprises
Media queries that use em or rem measure against the browser's initial font size, not the font size you set on html or body. A user who raises their default text size to 20px gets a breakpoint of 40em at 800px. This is intentional and good for accessibility, but it means your layout changes at a different pixel width than you tested. Test by changing the default font size in browser settings.
Also, em in a media query ignores your CSS. Setting html { font-size: 62.5%; } does not change where 40em falls.
Horizontal overflow
A horizontal scrollbar on a narrow screen is the most common responsive bug. Something is wider than the viewport. To find it, run this snippet in the DevTools console:
const vw = document.documentElement.clientWidth;
document.querySelectorAll("*").forEach((el) => {
const rect = el.getBoundingClientRect();
if (rect.right > vw || rect.left < 0) {
console.log(el, Math.round(rect.left), Math.round(rect.right));
}
});Common culprits:
- Images or videos without
max-width: 100%. - Fixed widths in
pxon containers, such aswidth: 600px. - Long unbroken strings such as URLs, which need
overflow-wrap: anywhere. - Wide tables or code blocks, which need a wrapper with
overflow-x: auto. - Grid or flex children with
min-width: auto, which refuse to shrink below their content size. Settingmin-width: 0on the child often fixes this. 100vwon an element. It includes the scrollbar width on some platforms, so it can exceed the visible area.
A temporary outline helps you see the boxes:
* {
outline: 1px solid rgb(255 0 0 / 0.4);
}Remove it when you finish.
Overlapping or conflicting breakpoints
When you mix min-width and max-width queries, a gap or overlap appears at the boundary. With max-width: 40em and min-width: 40em, both match at exactly 40em. With max-width: 39.99em and min-width: 40em, fractional widths such as 39.995em on high-density screens match neither. Range syntax avoids this:
@media (width < 40em) {
.sidebar {
display: none;
}
}
@media (width >= 40em) {
.sidebar {
display: block;
}
}These two ranges cover every width exactly once.
Container queries
If a component ignores a container query, check that an ancestor has container-type set. Without one, the query has no container to measure and never matches. Also remember that a container cannot query its own size. The query applies to descendants of the container, so styles must go on a child element.
Testing on real devices
Emulation misses real-world behavior. Test on at least one physical phone when you can. Check:
- Tap targets are large enough to hit (around 44px is a common guideline).
- The address bar collapsing does not break
100vhlayouts. Consider100dvhwhere supported. - Landscape and portrait both work, and rotating does not break layout.
- Text zoom and page zoom at 200% remain usable.
- Hover-only features have a touch alternative.
To reach a local dev server from a phone, connect both to the same network and open your computer's local IP address. Chrome also supports remote debugging for Android over USB, and Safari does the same for iOS from a Mac. Both let you inspect the real device's page from desktop DevTools.
A short test checklist
| Check | How |
|---|---|
| Viewport tag present | View the page source |
| Both sides of each breakpoint | Responsive mode at exact widths |
| No horizontal scroll | Resize to 320px and scroll sideways |
| Dark mode and reduced motion | Rendering panel emulation |
| Print layout | Print emulation and print preview |
| Touch behavior | Real device |
| Larger default font | Change the browser font size setting |
| Zoom to 200% | Browser zoom |
Exercise
A page looks correct in desktop responsive mode at 375px, but on a real phone the mobile layout never appears and the desktop layout is shown, zoomed out. What is the most likely cause, and how do you fix it?
Show answer
The page is probably missing the viewport meta tag. Without it, the phone lays the page out at a desktop-like width of about 980px, so max-width and min-width queries match the wrong width. Add <meta name="viewport" content="width=device-width, initial-scale=1"> to the head.
Exercise
You wrote @media (min-width: 40em) { .nav { flex-direction: row; } } followed by .nav { display: flex; flex-direction: column; }. At 1000px wide the nav is still a column. Why, and what is the fix?
Show answer
Both rules have the same specificity, so the later one wins, and the base rule comes after the media query. Move the @media block below the .nav rule.
Exercise
Your page has a horizontal scrollbar at 320px. Name two ways to find the element that causes it and two common fixes.
Show answer
Run a console script that logs elements whose bounding rectangle extends beyond document.documentElement.clientWidth, or apply a temporary outline to all elements and look for the box that sticks out. Fixes include max-width: 100% with height: auto on images, replacing fixed pixel widths with fluid units, wrapping wide tables in an overflow-x: auto container, or setting min-width: 0 on grid or flex children.