w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
find screen dimensions in inches not pixels using javascript

once you have got the screen resolution in pixels, you can work out the dpi with a hidden div:

<div id="dpi" style="height: 1in; width: 1in; left: 100%;
position: fixed; top: 100%;"></div>

js

var dpi_x = document.getElementById('dpi').offsetWidth;
var dpi_y = document.getElementById('dpi').offsetHeight;

then work out the resolution in inches:

var width = screen.width / dpi_x;
var height = screen.height / dpi_y;

Example





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.