Sub topic 16
Testing and debugging responsive pages
This chapter assumes you know the viewport meta tag, @media syntax with min-width, and basic grid or flexbox. It covers how to check that your queries work and how to find the cause when they don't.
Start with the viewport tag
Many "my media query does nothing on my phone" bugs come from a missing viewport tag. Without it, mobile browsers lay the page out at a desktop width (often around 980px) and then shrink it. Your min-width: 40em rule then compares against that wide layout, not the real screen.
Check that the head contains this line:
<meta name="viewport" content="width=device-width, initial-scale=1">Use browser developer tools
Every major desktop browser has a device mode. In Chrome, Edge, and Firefox, open developer tools and toggle the device toolbar (Ctrl+Shift+M or Cmd+Shift+M in Firefox and Chrome on most setups). From there you can:
- Drag the viewport edge to any width and watch rules switch on and off.
- Pick a preset device to set width, pixel density, and touch input.
- Show the media query bars above the page, then click a bar to jump to that width.
- Emulate features. In the Rendering panel (Chrome and Edge) you can force
prefers-color-scheme,prefers-reduced-motion, and theprintmedia type.
Common causes of a failing query
| Symptom | Likely cause | Fix |
|---|---|---|
| Query never matches on a phone | Missing viewport tag | Add the meta tag |
| Query matches, but styles don't change | A later rule with equal or higher specificity wins | Move the query after the base rule, or raise specificity |
| Rules apply at the wrong width | Mixed units, such as `em` in queries and `px` in your head | Use one unit, preferably `em` or `rem` |
| Whole block ignored | Syntax error such as a missing `and` or parenthesis | Check the Styles panel for struck-through or dropped rules |
| Layout breaks between breakpoints | Fixed widths that don't flex | Use `max-width`, `%`, `fr`, or `clamp()` |
Order matters in mobile-first CSS. Media queries add no specificity, so a base rule written after the query overrides it:
@media (min-width: 40em) {
.cards { grid-template-columns: repeat(2, 1fr); }
}
/* Bug: this comes later, so it wins at every width. */
.cards { grid-template-columns: 1fr; }Put base styles first and queries after them.
Find overflow problems
Horizontal scrolling on small screens usually means one element is wider than the viewport. Paste this into the console to list offenders:
const vw = document.documentElement.clientWidth;
document.querySelectorAll("body *").forEach((el) => {
if (el.getBoundingClientRect().right > vw) {
console.log("Overflows:", el);
}
});Typical culprits are images without max-width: 100%, long unbroken strings, and fixed-width tables.
Test on real devices
Emulation does not reproduce everything. Real devices show true touch behavior, browser toolbars that change the viewport height, font rendering, and performance. Test at least one phone and one tablet when you can. To reach a local dev server from a phone, connect both to the same network and open your computer's local IP address. Chrome and Safari also offer remote debugging, which lets you inspect a connected phone's page from your desktop.
Also test by resizing slowly. Bugs often appear at widths between your breakpoints, not at the breakpoints themselves.
Exercise
A mobile-first stylesheet sets .nav { display: none; } inside @media (min-width: 40em) at the top of the file, but a later rule .nav { display: block; } appears below it. At a 60em viewport the nav is visible. Why, and how do you fix it?
Show answer
The query adds no specificity, and the later .nav { display: block; } has equal specificity and comes last, so it wins at every width. Move the base rule .nav { display: block; } above the media query, so the query's display: none comes later and overrides it at 40em and wider.
Exercise
Your media query works in the browser's device mode but not on your real phone. Name the first thing to check.
Show answer
Check that the page's head has <meta name="viewport" content="width=device-width, initial-scale=1">. Without it, the phone lays out at a desktop width and width-based queries compare against that value.