Despite older solutions, I found that the device ratio is the main problem in recreating a device viewport. The dev tools pixel device ratio
input doesn't seme to work as expected. For me, the only reliable method is to use viewport size instead of resolution with pixel ratio blank. The added benefit is testing when users have a bookmarks bar open in Chrome or side pane open in Safari.
Caveat: "Auto-Adjust Zoom" and "Fit to Window" won't work with these methods... if you have a large screen and are working on a small UI use the preset zoom levels 100%, 125%, etc.
The Manual Method
Here's a walkthrough I use with a macbook pro as an example (screen res 2880x1800 pixel-ratio 2).
- Get a screenshot from the user with your website open on their device (make sure page zoom is reset, e.g. cmd+0 or ctrl+0)
- Open the screenshot, measure the pixels of the webpage only. My example user had the bookmarks bar open, and I measured the viewport as 2880 by 1514.
- Divide the above by the device ratio to get the viewport. Macbook Pro is 2.
2880/2
= 1440
1514/2
= 757
- Compensate for the bookmarks bar height, add
23
pixels to the height.
- Create a custom responsive profile (width first):
- Width:
1440
- Height:
780
- Device pixel ratio: ____ (leave blank)
Now selecting that profile will default to 100% and is an exact match to the user.
The Programmatic Method (collecting user viewports)
In supporting more devices, this is a reoccurring problem for me. I ended up automating viewport measurement by adding the JS below to measure the viewport, and it gets sent back and stored in a db with the user's session.
const vw = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0);
const vh = Math.max(document.documentElement.clientHeight || 0, window.innerHeight || 0);
console.log(`ViewPortWidth=${vw}`); // 1440
console.log(`ViewPortHeight=${vh}`); // 780
// Example of how to collect analytics on user viewports
const user = Users.getActiveUser(); // pretend fn call
user.addCurrentViewPort(vw, vh);
user.persist();
Later on, query the db and see what viewports your users use... add new responsive test profiles.