JS: Find Window Size

By Xah Lee. Date: . Last updated: .

How to find the viewport size?

document.documentElement.clientWidth and document.documentElement.clientHeight are viewport size.

// get viewport size
var x = document.documentElement.clientWidth;
var y = document.documentElement.clientHeight;

View port is the area for displaying the HTML page.

The result does not include scrollbar or toolbar nor status bar.

Values changes when user zooms.

How to find window size?

window.innerWidth and window.innerHeight.

The result includes the scroll bars, but does not include the URL field, tool bar, status bar, etc.

Note: when user zooms in, these values decrease. Similarly when zoom out.

How to find screen size?

Values of screen.width and screen.height.

How to find scroll amount?

window.pageXOffset and window.pageYOffset.

When no scroll, the value of window.pageYOffset is 0.

How to find HTML document's rendered size?

document.documentElement.offsetWidth and document.documentElement.offsetHeight are the html element's size.

Find Element/Viewport/Window Size

  1. CSS: Fixed Aspect Ratio
  2. JS: Find element Size
  3. JS: Find Window Size
Liket it? Put $5 at patreon.

Or, Buy JavaScript in Depth

If you have a question, put $5 at patreon and message me.

Web Dev Tutorials

  1. HTML
  2. Visual CSS
  3. JS Basics
  4. JS in Depth
  5. JS Reference
  6. DOM
  7. SVG
  8. JS Misc