Sub topic 14
Testing on emulators and real devices
This chapter assumes you know HTML and CSS basics, can open browser developer tools, and have read the earlier chapters on the viewport meta tag, min-width queries, fluid layouts and container queries. Here you will check that a mobile-first stylesheet works. Device emulation catches most layout bugs. Real devices catch the rest.
What each kind of test can tell you
A responsive stylesheet depends on many things at once: viewport width, pixel density, input type, user preferences, and the browser's own rendering engine. No single tool covers all of these.
| Method | Good at | Cannot show you |
|---|---|---|
| Resizing the browser window | Breakpoints, fluid layout, overflow | Touch input, real mobile browser chrome |
| Device emulation in dev tools | Viewport size, pixel ratio, touch events, throttling | Real engine differences, real performance, real keyboards |
| Real device on your network | Touch feel, true rendering, true speed | Quick iteration on many screen sizes |
| Cloud device services | Many device and browser combinations | Hands-on feel, with some delay |
Use the cheap methods often and the expensive ones at milestones.
Start by resizing the window
Before you open any device mode, drag the browser window from very narrow to very wide. This shows how the layout behaves at every width, not only at the sizes a device preset lists.
Watch for these problems:
- Horizontal scrolling at narrow widths. Something is wider than the viewport.
- Awkward layouts just before or after a breakpoint, such as a column that is too narrow or a line of text that is too long.
- Gaps where nothing changes for a very long range of widths.
Because mobile-first CSS starts with the smallest screen, begin at the narrowest width you support. A common floor is 320 CSS pixels. Then widen the window slowly. Each time the layout looks stretched or cramped, you have found a place where a breakpoint belongs. This is the content-driven approach from the breakpoints chapter, applied in practice.
Using device emulation in browser dev tools
All major desktop browsers include a device mode. In Chrome and Edge, open dev tools and press the device toolbar button, or use Ctrl+Shift+M (Cmd+Shift+M on macOS). Firefox calls it Responsive Design Mode, opened with Ctrl+Shift+M or Cmd+Option+M. Safari has a Responsive Design Mode in its Develop menu.
Device mode lets you set:
- Width and height, either by typing values or by choosing a preset such as a phone model.
- Device pixel ratio (DPR), the number of physical pixels per CSS pixel. A DPR of 2 or 3 is common on phones. This matters for the
srcsetexample from the images chapter, because the browser chooses a file using bothsizesand DPR. - Touch simulation, which changes the mouse cursor to a touch point and fires touch events.
- Network throttling and CPU throttling, to approximate a slow connection and a slow processor.
- Rotation, to swap width and height.
Check that the viewport meta tag works
If you forget the viewport meta tag, device mode usually exposes it. A page without it renders at a wide virtual viewport, usually about 980 pixels, and shrinks to fit. Your mobile base styles then look tiny and your media queries seem not to apply.
Confirm that your page has this line in the head:
<meta name="viewport" content="width=device-width, initial-scale=1">Then test with a phone preset. If the text looks tiny and zoomed out, the tag is missing or malformed. Note that a desktop browser window that you resize does not behave this way. Only device mode, or a real phone, reproduces the problem.
Testing input and preference media features
Device mode can also help with the media features covered in earlier chapters.
- Hover and pointer: With touch simulation on,
(hover: hover) and (pointer: fine)should stop matching in most browsers. Check that your hover styles disappear and that the button from the touch chapter keeps itsmin-block-size: 2.75rem. - Reduced motion: Chrome's Rendering panel has an option to emulate
prefers-reduced-motion. Firefox offers a similar setting in its Inspector. Use it to confirm that the banner animation stops. - Color scheme and contrast: The same panel emulates
prefers-color-scheme, which is useful if your stylesheet supports dark mode.
Emulating a preference is faster than changing your operating system settings, and it is easy to switch back.
Inspecting breakpoints and containers
Dev tools show media queries as colored bars above the page in device mode. Click a bar to jump the viewport to that width. This helps you check the width on each side of a breakpoint.
For container queries, inspect the container element. Chrome shows a container badge on elements with container-type set, and the Styles pane shows which @container rule is active. Resize the container, for example by moving the component into a narrow sidebar, and confirm that the layout switches based on the container, not the viewport.
Debugging common responsive bugs
Most failures in mobile-first CSS fall into a few groups. The table lists symptoms and what to check first.
| Symptom | Likely cause | What to check |
|---|---|---|
| Page scrolls sideways on a phone | An element wider than the viewport | Fixed widths, long unbroken text, images without `max-width: 100%` |
| Page looks zoomed out on a phone | Missing viewport meta tag | The `head` of the document |
| Media query never applies | Wrong unit or syntax, or overridden by a later rule | The Styles pane for crossed-out rules and source order |
| Layout breaks between breakpoints | Breakpoints chosen from device sizes | Widen the window slowly and look for stretched content |
| Layout shifts as images load | Missing `width` and `height` attributes | The `img` markup |
| Tap targets feel too small | Targets below about 44 CSS pixels | Computed height and padding of buttons and links |
Finding the element that overflows
Horizontal overflow is the most common mobile bug. Dev tools do not always point to the culprit directly. This snippet, run in the browser console, lists elements that extend beyond the right edge of the viewport:
const viewportWidth = document.documentElement.clientWidth;
const wide = [...document.querySelectorAll("body *")].filter((el) => {
const rect = el.getBoundingClientRect();
return rect.right > viewportWidth + 1;
});
console.table(
wide.map((el) => ({
tag: el.tagName.toLowerCase(),
class: el.className,
right: Math.round(el.getBoundingClientRect().right),
}))
);The + 1 ignores sub-pixel rounding. Some results are not bugs. For instance, an element inside a container with overflow-x: auto is meant to scroll. Check each result before changing anything.
A temporary outline helps too. Add this rule while debugging, then remove it:
* {
outline: 1px solid rgb(255 0 0 / 0.4);
}Outlines do not change layout, unlike borders, so they show boxes without moving them.
Testing on a real device
Emulation uses your desktop browser's engine and your computer's hardware. A real phone differs in several ways that matter.
- Rendering engine. Every browser on iOS uses WebKit, so Chrome on an iPhone behaves like Safari. Android Chrome uses Blink. Bugs and missing features can differ from your desktop browser.
- Browser interface. On phones the address bar collapses and expands as you scroll, which changes the visible height. Units such as
100vhcan misbehave. Thedvh,svhandlvhunits were added to deal with this, so test any full-height layout on a real phone. - Touch feel. You find out whether targets are easy to hit with a thumb, whether scrolling is smooth, and whether sticky elements get in the way.
- Real performance. A mid-range phone is much slower than a laptop, and throttling only approximates this.
- On-screen keyboard. It covers part of the page when a form field has focus. Emulation does not show this.
Connecting a phone to your development server
The simplest way to test is over your local network.
- Connect the phone and the computer to the same Wi-Fi network.
- Start your development server so that it listens on all network interfaces, not only
localhost. Many tools have a flag for this, such as--host. - Find your computer's local IP address.
- On the phone, open
http://<your-ip>:<port>.
Your firewall may block incoming connections. Allow the port only on private networks, and stop the server when you finish.
Remote debugging
You can inspect a real phone's page from a desktop browser.
- Android with Chrome: Enable developer options and USB debugging on the phone, connect it with a cable, and open
chrome://inspecton your desktop. You can then use the full dev tools on the phone's tab, including the live view. - iOS with Safari: On the iPhone, turn on Web Inspector in Safari's advanced settings. On a Mac, enable the Develop menu in Safari settings, then choose your device under Develop.
Remote debugging lets you read computed styles, see console errors and check network timing on the actual device.
When you cannot own the devices
You cannot buy every phone. Pick a small set that matches your audience: one recent iPhone, one mid-range Android phone, and one older device if your analytics show users on them. For wider coverage, cloud testing services let you run your page on many real devices and browsers remotely. Use them for release checks, not for everyday editing.
A short checklist
Run these checks before you call a layout done.
- At 320 px wide, nothing scrolls sideways and all text is readable.
- Widening the window slowly never produces a stretched or cramped layout.
- The viewport meta tag is present and phone presets show correct sizing.
- With touch simulation on, hover-only effects do not hide information, and targets are at least about 44 CSS pixels.
- Reduced motion, and dark mode if supported, behave correctly under emulation.
- Container-query components work in both a narrow and a wide container.
- Full-height sections and forms work on at least one real iOS device and one real Android device.
- With CPU and network throttling on, the page remains usable.
Exercises
Exercise
A page looks correct when you resize your desktop browser window to 390 px wide, but on a real phone the text is tiny and the layout looks like a zoomed-out desktop page. What is the most likely cause, and why did the window resize not show it?
Show answer
The viewport meta tag is missing or malformed. Without it, a mobile browser lays the page out at a wide virtual viewport, around 980 px, then shrinks it to fit the screen. A desktop window has no virtual viewport, so the CSS width equals the window width and media queries match as expected. Device mode in dev tools, or a real phone, reproduces the problem.
Exercise
You add the console script from this chapter and it lists a div with the class table-wrap whose right edge is beyond the viewport. The div has overflow-x: auto and contains a wide data table. Is this a bug? What would you check next?
Show answer
Probably not. The wrapper is designed to scroll horizontally inside itself, so the page itself should not scroll. Check that the div itself fits within the viewport, and that the page body does not scroll sideways. If the wrapper's own right edge is past the viewport, the wrapper is too wide, and you should look for a fixed width or a missing max-width: 100% on it or on one of its parents.
Exercise
Name two things you can only learn by testing on a real phone and not in device emulation, and say how you would connect a phone to inspect the page.
Show answer
Examples include how the collapsing address bar affects full-height layouts, how the on-screen keyboard covers form fields, true rendering differences in WebKit or Blink, touch comfort, and real performance. To inspect, for Android connect over USB with USB debugging on and open chrome://inspect in desktop Chrome. For iOS, enable Web Inspector on the iPhone and use the Develop menu in Safari on a Mac.