22

I have the max-height of an element as 65vh. I need to convert it to pixels in my JavaScript to see whether an image can fit there or if I need to shrink/crop it. (am doing win8 App development).

Will this work?

100 vh = screen.height therefore 65vh in pixels is screen.height *0.65

Stephan Muller
  • 27,018
  • 16
  • 85
  • 126
Sudharsanan
  • 567
  • 2
  • 6
  • 11
  • 4
    You are basically answering your own question. The real question here is: How can I find the height of the viewport? – Sumurai8 Aug 22 '13 at 17:57

2 Answers2

40

Not necessarily screen.height * 0.65, but viewport.height * 0.65. Even though a Windows 8 app will always have the same height, regardless of the snapped state, this is an important difference in browser-based applications.

In JavaScript:

document.documentElement.clientHeight * 0.65;

If you're using jQuery, you can do:

$(window).height() * 0.65;
rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
  • 1
    not accurate on mobile when you haven't scrolled down to shrink the address bar where 100vh is bigger than window height – Curtis Mar 14 '18 at 01:49
  • 1
    For those trying to figure out what is viewport.height - document.documentElement.clientHeight (or clientWidth if you need). – Bojidar Stanchev Aug 07 '19 at 12:58
0

When I use $("div").height(); it actually returns the value in pixels.

Check this fiddle.

GreyRoofPigeon
  • 17,833
  • 4
  • 36
  • 59