DEV Community

Cover image for Get viewport width and height in JavaScript (with examples)
Reza Lavarian
Reza Lavarian

Posted on • Originally published at decodingweb.dev

Get viewport width and height in JavaScript (with examples)

Update: This post was originally published on my blog decodingweb.dev, where you can read the latest version for a 💯 user experience. ~reza

Sometimes you need to get the viewport width (or height) in JavaScript – probably to adjust an element based on the window size. The good news is, it’s easy and quick! Let’s see how.

What is the viewport size? You may ask.

Here’s an explanation:

In a web browser context, the term viewport (more precisely, the layout viewport) refers to the area of the document you’re currently viewing in the browser window; Content outside the viewport is not visible onscreen until scrolled into view. And the viewport size is the dimensions of this rectangular area.

But how do you find the width and height of a viewport in JavaScript (yes, without jQuery)?

First of all, you need to answer the following question:

Do I need a viewport width (or height) without or without the vertical scrollbar? 🤔

Let’s see how we can solve both use cases.

Get viewport width including the vertical scrollbar

To get the viewport width, including the vertical scrollbar (if there’s one), all you need is the window.innerWidth property – a read-only property that gives you the “interior” viewport width in pixels.

console.log(window.innerWidth)
Enter fullscreen mode Exit fullscreen mode

And here's how you'd do it for the layout viewport's height:

console.log(window.innerHeight)
Enter fullscreen mode Exit fullscreen mode

Please note the value of the window.innerHeight property will include the horizontal scrollbar's height.

The window.innerWidth and window.innerHeight properties are compatible with most browsers, but, of course, except for IE! Who uses IE these days anyway?

Alright, now that you know what window.innerHeight and window.innerWidth is! Let's move on to the second scenario.

Get viewport width without the vertical scrollbar

Sometimes, you don't want the vertical/horizontal scrolls to be included in the viewport height and width.

In that case, you should use the Element.clientWidth and Element.clientHeight on your document's html element.

You can access the root html element via document.documentElement.

And here's how to get the layout viewport width in JavaScript without the scrollbar:

console.log('document.documentElement.clientWidth')
Enter fullscreen mode Exit fullscreen mode

And for the viewport height:

console.log('document.documentElement.clientHeight')
Enter fullscreen mode Exit fullscreen mode

If you get a ReferenceError when accessing the document, check this quick guide on fixing document is not defined error.

Alright, I think that does it. I hope you found this quick guide helpful.

Thanks for reading.

Top comments (1)

Collapse
 
naucode profile image
Al - Naucode

Great article, you got my follow, keep writing!