I'd like to categorize devices by screen width in client-side JavaScript code
All devices fitting to one hand (7" less) to mobile category
Treat other devices as desktop devices
Fallback: Treat devices which do not support necessary APIs as mobile devices
Question
Firefox support is optional - if they have compatible APIs already.
Please note that this is about physical dimensions, not pixel dimensions.
The size of a 16:9 screen depends on how long the screen's diagonal is, as 16:9 is merely the ratio of the screen's width to its height. If you have the screens diagonal, you can multiply that measurement by 0.872 to get the screen's width. You can also multiply the diagonal by 0.49 to get the screen's height.
You can use device-width which will test of the screen's width in px. That however is not entirely recommended. Use max-width and min-width (for the viewport) instead. If you are trying to GET the screen width and use it (something like content: (device-width); of some sort, that's not possible.
Window Screen Available Width The screen. availWidth property returns the width of the visitor's screen, in pixels, minus interface features like the Windows Taskbar.
There's no direct way to get the screen size in inches, but there are workarounds that use screen density to find the device size. It's not perfect, but you don't really need to know the exact size to 5 significant figures. Also, it is normally better to simply use pixel values, IMO.
Make a test div, and then see the number of pixels displayed to get the pixel density, then use that in your calculations. This should work, assuming that your browser/OS are configured properly (it didn't work in the this question but it was within half an inch on my computer).
EDIT: This is 100% wrong. The inch/cm measurements in CSS aren't based on an actual physical measurement. They're based on an exact conversion (1 inch = 96 px, 1 cm = 37.8 px). My apologies.
The best way to detect physical screen size is to use CSS media queries. The min-resolution
and max-resolution
queries can be used to get the resolution in either dpi
or dpcm
:
@media (min-resolution: 300dpi){
// styles
}
Combining it with the min-device-width
and max-device-width
queries, you get something like:
@media (resolution: 326dpi) and (device-width: 640) and (device-height: 960){
// iPhone
}
The problem is that if you want to target 7 inch devices, you'd have to have many resolutions and corresponding widths that go together, which could get complicated.
For more information:
You can use window.devicePixelRatio
to determine the screen density. From Android's WebView Reference:
The
window.devicePixelRatio
DOM property. The value of this property specifies the default scaling factor used for the current device. For example, if the value ofwindow.devicePixelRatio
is "1.0", then the device is considered a medium density (mdpi) device and default scaling is not applied to the web page; if the value is "1.5", then the device is considered a high density device (hdpi) and the page content is scaled 1.5x; if the value is "0.75", then the device is considered a low density device (ldpi) and the content is scaled 0.75x.
Then using this, you calculate the physical size by dividing this by the total number of pixels, which can be calculated with window.screen.width
and window.screen.height
(Don't use window.screen.availHeight
or window.screen.availWidth
because these only detect the available height).
For more information:
Screen.width
Screen.height
devicePixelRatio
ExplanationIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With