Sub topic 8
Testing responsive layouts and slow connections
This chapter covers Device Mode and network throttling in Chromium-based browsers. It assumes you can read basic HTML and CSS, know what a media query is, and have used the Network panel to read a request waterfall. Firefox calls its equivalent Responsive Design Mode, and Safari has a similar Responsive Design Mode in its Develop menu. Menu names differ, but the ideas carry over.
What emulation is, and what it is not
Device emulation changes how the browser presents a page so you can approximate a phone or tablet on a desktop machine. It does not run your page on a real phone. It adjusts a small set of signals:
- The viewport size, which drives CSS media queries and layout.
- The device pixel ratio (DPR), which is the number of physical pixels per CSS pixel.
- The user agent string and client hints sent in request headers.
- Touch input, where the mouse pointer is converted into touch events.
- Optional CPU and network throttling.
Everything else is still your desktop browser. The JavaScript engine, the font rendering, the GPU and the memory limits all belong to your computer. A layout that works in Device Mode can still fail on a real device, so treat emulation as a fast first pass and confirm on hardware before release.
Opening Device Mode
- Open DevTools (F12, or Ctrl+Shift+I on Windows and Linux, Cmd+Option+I on macOS).
- Press Ctrl+Shift+M (Cmd+Shift+M on macOS), or click the device toolbar icon at the top left of DevTools.
The page shrinks into a viewport frame with a toolbar above it. The toolbar has these controls:
| Control | What it does |
|---|---|
| Device dropdown | Chooses a preset such as a phone or tablet, or Responsive for free sizing |
| Width and height boxes | Set exact viewport dimensions in CSS pixels |
| Zoom | Scales the frame so a large viewport fits your window. It does not change layout |
| Throttling dropdown | Applies CPU and network throttling profiles |
| Rotate button | Swaps width and height to test portrait and landscape |
| More options (three dots) | Shows device pixel ratio, device type, media query bars, and a screenshot command |
Choose Responsive when you want to find where your layout breaks. Drag the handles or type widths, and watch for the point where content overflows, wraps badly or overlaps. Choose a named device when you want to reproduce a report such as "the menu is broken on a phone".
Media query bars
From the three-dot menu, choose Show media queries. A colored bar appears above the viewport with one segment for each breakpoint your CSS defines. Click a segment to resize the viewport to that breakpoint. Click the right-click menu on a segment to jump to the rule in the stylesheet. This is a quick way to see which breakpoints exist, which is useful when you inherit a stylesheet.
Zoom versus size
The zoom setting only scales the frame on screen. The viewport width in CSS pixels stays as set. If you set width to 390 and zoom to 50%, the page still lays out at 390 CSS pixels. Do not confuse this with browser zoom (Ctrl and +), which changes the effective viewport width and can trigger different breakpoints.
Device pixel ratio and device type
A device pixel ratio of 2 means the screen uses a 2 by 2 block of physical pixels for each CSS pixel. This matters for images and for thin borders. To check how your page responds:
- In Device Mode, open the three-dot menu and enable Add device pixel ratio.
- Set it to 1, 2 or 3 using the new toolbar field.
- Reload if your code reads
devicePixelRatioat load time.
You can confirm the value in the Console:
console.log('DPR:', window.devicePixelRatio);
console.log('Viewport:', window.innerWidth, 'x', window.innerHeight);
console.log('Narrow screen:', window.matchMedia('(max-width: 600px)').matches);
console.log('Hover available:', window.matchMedia('(hover: hover)').matches);The last line shows why device type matters. The Add device type option switches between Mobile, Mobile (no touch), Desktop and Desktop (touch). When the type is mobile, (hover: hover) is false and (pointer: coarse) is true. CSS that hides controls behind :hover will then be unreachable, which is a common bug. Test the pointer and hover media features, not only the width.
The viewport meta tag
If a page lacks this tag, mobile browsers lay it out as if the screen were about 980 CSS pixels wide, then shrink it:
<meta name="viewport" content="width=device-width, initial-scale=1">Device Mode reproduces this behavior when the device type is mobile. If your media queries seem to be ignored in the emulated phone, check for the meta tag first. Avoid user-scalable=no and maximum-scale=1, because they block pinch zoom and harm accessibility.
Finding layout problems
Use this routine on each width you care about:
- Drag the viewport from wide to narrow in Responsive mode.
- Look for horizontal scrolling. A page that scrolls sideways at phone width usually has one element wider than the viewport.
- Find the offender by running a script in the Console.
- Check tap targets and text size.
- Rotate the device and look again.
The script for step 3 lists elements that extend beyond the viewport:
const vw = document.documentElement.clientWidth;
const wide = [...document.querySelectorAll('body *')]
.filter(el => el.getBoundingClientRect().right > vw);
console.table(wide.map(el => ({
tag: el.tagName,
class: el.className,
right: Math.round(el.getBoundingClientRect().right)
})));Click an element in the output, or select it in the Elements panel, and inspect its box model in the Styles pane. Typical causes are fixed width values in pixels, images without max-width: 100%, long unbroken strings, and 100vw elements inside a page that has a vertical scrollbar.
Edge cases in emulated layouts
- Scrollbars. Emulated mobile viewports use overlay scrollbars, so
100vwmatches the visible width. On desktop with classic scrollbars,100vwincludes the scrollbar and causes sideways scrolling. - Dynamic toolbars. Real mobile browsers resize the viewport as the address bar hides. Emulation does not. Test
100vhlayouts on a real device, or usedvhunits. - Safe areas. Notches and rounded corners are drawn for some presets, but
env(safe-area-inset-*)values are generally not populated. Verify those on hardware. - Fonts. Your desktop may have fonts that the phone lacks, so text can wrap differently on the device.
Capturing screenshots
The three-dot menu offers Capture screenshot, Capture full size screenshot and Capture node screenshot. The full size option records the whole scrollable page, which helps with bug reports and visual comparison. These are also available in the Command Menu (Ctrl+Shift+P), by typing "screenshot".
Network throttling
A fast office connection hides problems that real users hit. Throttling slows the network inside the browser so you can see how loading behaves on poor connections.
You can set throttling in two places: the dropdown in the Device Mode toolbar, and the Throttling dropdown in the Network panel. They control the same setting.
| Preset | Approximate meaning |
|---|---|
| No throttling | Uses your real connection |
| Fast 4G / Slow 4G (labels vary by version) | Moderate bandwidth with noticeable latency |
| 3G | Low bandwidth and high latency |
| Offline | All network requests fail |
| Custom profiles | Your own download speed, upload speed and latency |
To add a custom profile, open the Throttling dropdown, choose Add under Custom, and enter download and upload speeds in kbit/s plus latency in milliseconds. Latency is the delay added to every request before data starts to flow. High latency hurts pages with many small requests more than a low bandwidth limit does.
What throttling changes
DevTools throttling is applied inside the browser, after the operating system has received the data. It delays and limits the rate at which responses are delivered to the page. This has some consequences:
- It is repeatable and easy to switch on and off, which makes before and after comparisons fair.
- It does not model packet loss, a flaky radio or TCP behavior on a real network, so it tends to be more optimistic than reality.
- It affects requests made by the page. Requests that bypass the page's network stack, such as some browser internals, are not shaped the same way.
- Cached responses are still fast. Check Disable cache in the Network panel to simulate a first visit, and uncheck it to simulate a repeat visit.
The sequence below shows where throttling applies.
Offline mode and service workers
Selecting Offline makes requests fail, which lets you test your error handling. Pages that register a service worker may still load, since the worker can answer from its cache. That is the expected result when an offline-first design works. The Application panel has its own Offline checkbox for a service worker, and it behaves the same way, so use it when you want to test what the worker serves.
CPU throttling
Phones have slower processors than development machines. In the Performance panel's settings, the CPU dropdown offers 4x slowdown and 6x slowdown options. In recent versions, a calibration preset can match the speed of a low-end or mid-tier phone. Use these when recording a performance profile, because a slow network is not the only reason a page feels slow on a phone. Long JavaScript tasks that are invisible on your laptop can freeze the interface on a budget device.
A repeatable workflow
- Open Device Mode with a mid-range phone preset.
- Set network throttling to a slow profile and tick Disable cache in the Network panel.
- Reload and watch the waterfall. Note when the largest visible element appears and which requests block rendering.
- Resize through your breakpoints and note layout failures.
- Switch to Offline and confirm that your page shows a clear error or a cached fallback.
- Repeat with cache enabled to see the repeat visit.
- Apply a fix, then repeat the same steps and compare.
Keep the conditions the same between runs. Changing the preset between measurements makes the numbers meaningless.
Common errors
- Testing only at preset sizes. Real devices come in many sizes. Drag through the full range in Responsive mode.
- Forgetting to reload. Some code reads the viewport width or user agent only at load. After changing device settings, reload.
- Leaving throttling on. A forgotten throttle makes every site feel slow. DevTools shows a warning icon beside the Network tab when throttling is active.
- Trusting emulated results as final. Use remote debugging on a real device for final checks. Chrome supports this for Android over USB through the
chrome://inspectpage, and Safari supports it for iOS through the Develop menu. - Sniffing the user agent. Code that branches on the user agent string breaks easily. Prefer feature detection and media queries, which Device Mode also lets you test.
Summary of when to use what
| Question | Tool |
|---|---|
| Does the layout hold at this width? | Responsive mode and media query bars |
| Does the page work with touch and no hover? | Device type setting |
| Are images sharp on high density screens? | Device pixel ratio setting |
| How does it load on a poor connection? | Network throttling with Disable cache |
| Does it fail gracefully offline? | Offline preset and service worker checkbox |
| Is the page slow on weak hardware? | CPU throttling in the Performance panel |
Exercise
A page looks correct in Responsive mode at 390 px wide, but a user on a real phone reports that a "Details" menu cannot be opened. The menu opens through a CSS :hover rule. Which Device Mode setting would let you reproduce the problem, and what is a sound fix?
Show answer
Set the device type to Mobile, which makes (hover: hover) false and (pointer: coarse) true, and use touch emulation. The hover-only menu will not open. A sound fix is to open the menu on click or tap using a button with JavaScript or a details element, and to keep the hover style only as an enhancement inside @media (hover: hover).
Exercise
You throttle the network to a slow profile and reload twice. The second load is much faster, so you conclude the page is well optimized for slow connections. What is wrong with this conclusion, and what should you change?
Show answer
The second load was served largely from the browser cache, which throttling does not slow down. It shows repeat-visit behavior, not first-visit behavior. Tick Disable cache in the Network panel before measuring a first visit, and measure both cases separately.
Exercise
Write a Console snippet that prints the elements causing horizontal overflow at the current viewport width.
Show answer
const vw = document.documentElement.clientWidth; console.table([...document.querySelectorAll('body *')].filter(el => el.getBoundingClientRect().right > vw).map(el => ({ tag: el.tagName, right: Math.round(el.getBoundingClientRect().right) })));