Sub topic 8
Testing responsive layouts and slow connections
This chapter assumes you can read basic HTML and CSS, know what a media query is, and have used the Elements and Network panels from earlier chapters. Panel names follow Chromium-based browsers. Firefox calls its version Responsive Design Mode, and Safari has a similar responsive mode.
Why emulate instead of resizing the window
Your development machine is usually fast, on a wide screen, and on a stable connection. Many users are not. DevTools lets you approximate their conditions so you can find layout and loading problems before they do.
Two separate tools handle this:
- Device emulation changes the viewport size, pixel ratio, user agent and input type.
- Network throttling limits bandwidth and adds latency to requests.
They work independently. A phone-sized viewport on a fast connection tests layout only. Throttling on a desktop-sized viewport tests loading only. Combine them when you want to approximate a real phone.
Turning on device mode
- Open DevTools with
F12, orCtrl+Shift+I(Cmd+Option+Ion macOS). - Click the Toggle device toolbar icon, or press
Ctrl+Shift+M(Cmd+Shift+Mon macOS). - Choose a device from the Dimensions dropdown, or pick Responsive and drag the handles to any size.
The toolbar above the page shows the width and height, the zoom level, and a throttling dropdown. The three-dot menu adds options such as Show media queries, Add device pixel ratio and Add device type.
What emulation changes
- Viewport size. Width and height media queries evaluate against the emulated size. Enable Show media queries and colored bars appear above the page. Click a bar to resize the viewport to that breakpoint.
- Device pixel ratio (DPR). A ratio of 2 or 3 makes
srcsetandimage-set()choose higher-resolution images, and makesresolutionmedia queries match. - User agent. The browser sends a mobile user agent string, so server-side or script-based device detection runs its mobile branch.
- Touch. The cursor becomes a touch point, and
hover: noneandpointer: coarsemedia queries match.
You can confirm what the page sees from the Console:
console.log('width:', window.innerWidth);
console.log('pixel ratio:', window.devicePixelRatio);
console.log('narrow:', window.matchMedia('(max-width: 600px)').matches);
console.log('touch pointer:', window.matchMedia('(pointer: coarse)').matches);Reload after changing device settings. Some scripts read the user agent or viewport only once at startup, so they will not react to a change on a page that is already loaded.
Network throttling
Open the Network panel and use the No throttling dropdown. The same dropdown is in the device toolbar.
| Preset | Approximate use |
|---|---|
| Fast 4G | Typical mobile connection |
| Slow 4G | Weak signal |
| 3G | Poor or congested network |
| Offline | No connection at all |
| Custom profile | Your own download, upload and latency values |
Throttling affects requests made by the page. Latency matters as much as bandwidth. Many small requests suffer on high latency even when the download speed looks adequate, because each request waits a full round trip.
A useful workflow looks like this:
- Select Slow 4G (or a preset that matches your audience).
- Tick Disable cache so every load behaves like a first visit.
- Reload and watch the waterfall column.
- Check when the main content appears, which requests block it, and whether large images or scripts load before they are needed.
Clear the throttle when you finish. A forgotten throttle makes later work look mysteriously slow.
To see how your page behaves when a request fails, try Offline. A page that depends on an API call should show a sensible error or cached content, not a blank screen.
Common errors
- Testing only at preset sizes. Real devices have many widths. Use Responsive and drag across the whole range, watching for layout breaks between your breakpoints.
- Missing the viewport meta tag. Without
<meta name="viewport" content="width=device-width, initial-scale=1">, mobile browsers lay out the page at a wide default width and shrink it. Your media queries then seem not to fire. - Judging speed with the cache on. A cached reload hides the cost real first-time visitors pay.
- Trusting throttled timings as exact. Throttling is simulated at the browser level. Use it to compare before and after, not to predict precise load times.
- Forgetting that emulation ends when DevTools closes. The viewport and throttling settings only apply while DevTools is open.
Exercise
You enable the device toolbar and pick a phone, but your @media (max-width: 600px) rules never apply, even though the emulated width is 390 px. The page loads on a fast connection. What is the most likely cause, and how do you check it?
Show answer
The page probably lacks the viewport meta tag, so the browser lays it out at a wide default width instead of the device width. Check the <head> in the Elements panel for <meta name="viewport" content="width=device-width, initial-scale=1">, and run window.innerWidth in the Console to see the width the layout uses. Add the tag and reload.
Exercise
A page feels fine on your desktop, but users report a blank screen for several seconds on mobile. Describe the settings you would use in DevTools to reproduce this, and what you would look at.
Show answer
Turn on device mode with a phone preset, open the Network panel, choose Slow 4G, and tick Disable cache. Reload and read the waterfall to find large or blocking requests (scripts, CSS, big images) that delay the first content. Compare the load before and after any fix, using the same throttle setting.