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 base…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Reza Lavarian

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)

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

console.log(window.innerHeight)

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')

And for the viewport height:

console.log('document.documentElement.clientHeight')

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.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Reza Lavarian


Print Share Comment Cite Upload Translate Updates
APA

Reza Lavarian | Sciencx (2023-02-05T15:22:16+00:00) Get viewport width and height in JavaScript (with examples). Retrieved from https://www.scien.cx/2023/02/05/get-viewport-width-and-height-in-javascript-with-examples/

MLA
" » Get viewport width and height in JavaScript (with examples)." Reza Lavarian | Sciencx - Sunday February 5, 2023, https://www.scien.cx/2023/02/05/get-viewport-width-and-height-in-javascript-with-examples/
HARVARD
Reza Lavarian | Sciencx Sunday February 5, 2023 » Get viewport width and height in JavaScript (with examples)., viewed ,<https://www.scien.cx/2023/02/05/get-viewport-width-and-height-in-javascript-with-examples/>
VANCOUVER
Reza Lavarian | Sciencx - » Get viewport width and height in JavaScript (with examples). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/05/get-viewport-width-and-height-in-javascript-with-examples/
CHICAGO
" » Get viewport width and height in JavaScript (with examples)." Reza Lavarian | Sciencx - Accessed . https://www.scien.cx/2023/02/05/get-viewport-width-and-height-in-javascript-with-examples/
IEEE
" » Get viewport width and height in JavaScript (with examples)." Reza Lavarian | Sciencx [Online]. Available: https://www.scien.cx/2023/02/05/get-viewport-width-and-height-in-javascript-with-examples/. [Accessed: ]
rf:citation
» Get viewport width and height in JavaScript (with examples) | Reza Lavarian | Sciencx | https://www.scien.cx/2023/02/05/get-viewport-width-and-height-in-javascript-with-examples/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.