Sub topic 14
Testing on emulators and real devices
This chapter assumes you can open browser developer tools, read the box model, and write min-width media queries as covered earlier. It shows how to check a mobile-first stylesheet in device emulation, where emulation stops being reliable, and which problems to look for on real hardware.
What device emulation does
Every major desktop browser has a device toolbar in its developer tools. It resizes the viewport, sets a device pixel ratio, and can send touch events in place of mouse events. It also swaps the user agent string.
Emulation is fast, so use it for layout work. It does not run a mobile rendering engine. Fonts, scrolling, memory limits, and the on-screen keyboard all behave differently on a real phone.
Test across widths, not at device presets
Presets such as "iPhone" or "Pixel" are single points. Your breakpoints come from content, so test the range between them.
- Open the device toolbar and choose a responsive mode with a freely draggable width.
- Start at about 320 px and drag slowly to 1200 px.
- Watch for the moment the layout breaks: text wraps badly, a column gets too narrow, or something overflows.
- Check just below and just above each
min-widthvalue in your CSS. For48emat a 16 px root size, that is 767 px and 768 px.
The flow below shows how the checks fit together.
Find horizontal overflow
The most common mobile bug is a page that scrolls sideways. One element wider than the viewport causes it. This snippet, pasted into the console, lists the elements that extend past the viewport edge.
const viewportWidth = document.documentElement.clientWidth;
const offenders = [...document.querySelectorAll("body *")].filter(
(el) => el.getBoundingClientRect().right > viewportWidth
);
console.log(offenders);Typical causes are fixed width values in pixels, images without max-width: 100%, long unbroken strings such as URLs, and wide tables or pre blocks. Fix each at its source. Hiding the problem with overflow-x: hidden on body removes the scrollbar but still clips content.
Emulate input and preferences
The developer tools can also change the conditions your media queries respond to. Look for these controls, which are usually in the rendering panel or the device toolbar menu.
| Feature to emulate | Media query it tests |
|---|---|
| Touch input | `(hover: hover) and (pointer: fine)` |
| Reduced motion | `(prefers-reduced-motion: reduce)` |
| Dark or light scheme | `(prefers-color-scheme: dark)` |
| Device pixel ratio | `srcset` image selection |
With touch emulation on, confirm that hover-only styles do not apply and that buttons keep their 2.75rem minimum size. With reduced motion on, confirm the banner animation stops.
Zoom is another case worth testing. Set the browser text size to a larger value, or zoom to 200%. Layouts built from rem and em should reflow cleanly, and anything sized in pixels will stand out.
What emulation misses
Test on at least one real phone, ideally one iOS and one Android device, for the following.
- Browser toolbars. Mobile address bars show and hide while scrolling, which changes the visible height. Elements sized with
100vhcan be cut off. - The on-screen keyboard. It covers part of the page when a form field has focus, and some browsers resize the viewport while others do not.
- Real touch behavior. Tap accuracy, scroll momentum, and accidental taps on closely spaced links only show up with a finger.
- Performance. A mid-range phone on a slow network exposes heavy images and scripts that a fast laptop hides.
- Engine differences. Safari on iOS and Chrome on Android can render the same CSS differently.
If you have no device, cloud testing services provide real hardware remotely. For a local phone, connect it by USB and use remote debugging. Chrome offers this for Android and Safari for iOS. You can then inspect the live page from your desktop.
Common mistakes
- Testing only at a few device presets and missing breakpoints between them.
- Assuming a narrow desktop window equals a phone. It has no touch input and uses a different engine.
- Forgetting the viewport meta tag. Emulation may look fine while a real phone shows a zoomed-out page.
- Fixing overflow with
overflow-x: hiddeninstead of finding the wide element.
Exercise
Your layout looks correct in a 375 px emulated device, but on a real phone the page scrolls sideways. Name two ways to find the cause.
Show answer
Drag the responsive-mode width across a range and watch for overflow, and run a console script that lists elements whose right edge is past the viewport width. Remote debugging the real phone also lets you inspect the live page. Common causes are fixed pixel widths, images without max-width: 100%, long unbroken strings, and wide tables.
Exercise
Why test a min-width: 48em query at 767 px and 768 px instead of only at a phone preset?
Show answer
Breakpoint bugs appear at the edge where the layout changes. Checking the widths on each side of 768 px shows whether the base styles and the wider layout both work, which a single preset width cannot show.