-2

Hi guys I have a page that I want to set the initial zoom a little lower on smaller screen sizes, the problem is that I can't get the screen size within javascript, I need to zoom out on screens between 13 and 14 inches but there is no logical way to get that from the pixel values

I have the same problem on mobile, on certain phones the page looks good but on others it doesn't, instead of rebuilding everything I just wanna zoom out

I know it might be bad practice but if there is a way to do it that's what I wanna do

  • @bZezzz — No, it doesn't. The size of the pixel depends on the size of the screen and the resolution. – Quentin Nov 09 '21 at 20:47

1 Answers1

0

Inspired by what bZezzz wrote, but with some code to retrieve actual screen dimensions. Also note that screen.availHeight and screen.availWidth return the screen size, and might not be that helpful if your page is being rendered on only a part of the screen.

let hypotenuse = Math.sqrt(screen.availHeight**2 + screen.availWidth**2)
console.log(
  'pixels', hypotenuse,
  'inches', hypotenuse / 96 // Per spec, there are 96 pixels per inch
);
CerebralFart
  • 3,336
  • 5
  • 26
  • 29
  • "96 pixels per inch" — No. That's an default value set on some systems for software which needs to convert between physical and pixel values. It's an obsolete approach that never did a good job of reflecting reality. – Quentin Nov 09 '21 at 20:47
  • @Quentin, while I do agree this certainly isn't the right way to go about what OP wants to achieve (assuming an XY-problem here), 96 pixels is defined as one inch by the CSS standard. See [section 4.3.2 of the most recent version](https://www.w3.org/TR/CSS21/syndata.html#length-units), which defines a pixel as 3/4pt, in turn defined as 1/72th of an inch. – CerebralFart Nov 09 '21 at 20:52
  • @CerebralFart that spec is for "printers" only. For screens, it depends on the size of the screen, so just assuming there are 96 dots in an inch on a 20 inch monitor, and the exact same on a 13 inch monitor, is obviously not right – OG Sean Jul 16 '22 at 23:29