window.devicePixelRatio change listener
I took the IMO best answer (by @Neil) and made it a bit more human-readable:
function listenOnDevicePixelRatio() {
function onChange() {
console.log("devicePixelRatio changed: " + window.devicePixelRatio);
listenOnDevicePixelRatio();
}
matchMedia(
`(resolution: ${window.devicePixelRatio}dppx)`
).addEventListener("change", onChange, { once: true });
}
listenOnDevicePixelRatio();
No fixed boundary or variables needed.
How to simulate pixel ratio to test media queries with Google Chrome or Firefox on Windows?
about:config hack on Firefox
You actually can using Firefox:
- Go to "about:config"
- Find "layout.css.devPixelsPerPx
- Change it to your desired ratio (1 for normal, 2 for retina, etc.)
Refresh your page - boom, your media query has now kicked in! Hats off to Firefox for being awesome for web developing!
This was done on Windows 7 with Firefox 21.0.
Zooming on Firefox & Edge
Currently on Firefox and Edge, if you zoom it triggers dppx-based media queries. So this easier approach may be sufficient, but be aware that the functionality is reported as a "won't fix" bug for Firefox, so this could change.
Can devicePixelRatio be less than 1
DPR can be less than 1, if you set browser zoom to less than 100%. For a zoom factor of 50%, DPR would be 0.5.
Adjust browser zoom to below 100% and run this snippet, it will log the DPR:
console.log("devicePixelRatio:", window.devicePixelRatio);
Does devicePixelRatio change when the browser window is dragged between monitors?
Yes the devicePixelRatio changes, and you can detect it with matchMedia
.
I think that you should have a look at this topic that give a more detailled answer for detect the change:
https://stackoverflow.com/a/29653772/3914736
Related Topics
How to Add Multiple Classes in Material UI Using the Classes Props
Angular Material Side Bar with "Half" Side Mode
How to Set a CSS Border on One Side Only
Fixed Element Disappears in Chrome
Bootstrap 3 & Boostrap 4 & Bootstrap 5 - Input-Xs (Smaller Than Sm)
How to Compile a Directory Full of Less CSS Files to CSS
How to Create a Calc Mixin to Pass as an Expression to Generate Tags
-Webkit-Transform-Style: Preserve-3D Not Working
Visual Studio Code CSS Indentation and Formatting
Is It Bad to Work with Pixels in CSS
How to Gulp-Watch Multiple Files
How to Fade in Background Image by CSS3 Animation
Differencebetween ".Class Element" and "Element.Class"
Sticky Navigation Element Jumps During Scroll
How to Put a Div in Center of Browser Using CSS
Centering Fontawesome Icons Vertically and Horizontally