Looks like you can use the 'screen' DOM object in IE, its got properties for deviceXDPI, deviceYDPI, logicalXDPI, logicalYDPI.
https://www.w3schools.com/jsref/obj_screen.asp
Here's a solution from http://www.webdeveloper.com/forum/showthread.php?t=175278 (i havent tried it, seems like a total hack :) Just create something 1 inch wide and measure it in pixels!
console.log(document.getElementById("dpi").offsetHeight);
#dpi {
height: 1in;
left: -100%;
position: absolute;
top: -100%;
width: 1in;
}
<div id="dpi"></div>
Firstly, to help with the possible (and very common) confusion with the term DPI (dots per inch):
DPI isn't exactly a part of "display settings". It's (mis)used in two different contexts:
When printing an image, there are many things that affect the final dimensions of the image on paper:
The bottom line is, the image that you're printing will effectively get resampled (reduced or enlarged) to match the final DPI that's used in the print process. Any of the parties involed may be causing this (the program, the print driver, the printer).
Now, coming back to your question. No, you can't determine the DPI of the screen, because it's not in software domain. It's a hardware domain term, describing how large a monitor a user could afford to buy. Update: I initially wrote this answer back in 2009, with my understanding of the current technologies. As @thure pointed out, you can now (since 2012?) use the window.matchMedia function to determine the DPI of the screen.
If you're trying to achieve precision in printing an HTML layout, as others have suggested, your CSS should use print dimensions like em, pt or pc instead of px. However, the final outcome might still depend on the browser using. If converting your HTML to PDF (or generating PDF from scratch) is an option for you, printing a PDF would give you the truest WYSIWYG both on screen and paper.
You might use the window.devicePixelRatio
property to get the scaling ratio of the screen/page. This works well in current IE, Edge, Chrome and Firefox on the desktop (Windows), but it doesn't seem to be a current standard. It returns 1 on my desktop PC with a conventional monitor and 2 on the Surface with 200% scaling. Values should range from 1.0 to 3.0 these days. I could use this to correct dynamic image serving size to provide sharper images on high-resolution screens.
If you need some logical dpi/ppi, multiply that value with 96. It won't be the actual physical ppi though, just what the OS treats it like.
There isn't a way that I know of, however, they may be an alternative solution:
Specify your measurements in 'pt' and 'em', which are screen relative metrics.
http://www.w3schools.com/cssref/css_units.asp
https://css-tricks.com/the-lengths-of-css/
- em:
1em
is equal to the current font size.2em
means 2 times the size of the current font. E.g., if an element is displayed with a font of12 pt
, then'2em'
is24 pt
. The'em'
is a very useful unit in CSS, since it can adapt automatically to the font that the reader uses- pt:
point (1 pt
is the same as1/72 inch
)- pc:
pica (1 pc
is the same as12 points
)
If 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