Knowing the width and height of a browser window helps a web developer to enhancing the user experience. It can help in improving browser animations and the relative positioning of divisions and containers. Javascript provides a window object that represent an open browser window. It provides the various properties which define the dimensions of your browser window. They are as follows:
- innerWidth: It returns the width of the window’s content area.
- innerHeight: It returns the height of the window’s content area.
- outerWidth: It returns the width of the browser window.
- outerHeight: It returns the height of the browser window.
Note: These dimensions are in pixels.
Since pixels are a dimensionally square unit. You cannot tell accurately how many pixels lie on the diagonal. You can approximate the diagonal by using the browser height and width and applying the Pythagoras theorem.
Pythagoras Theorem: For a right-angled triangle, hypotenuse squared is the sum of base squared and height squared.

Analogous to the above formula, Diagonal = Squared_root(Width^2 + Height^2).
Code Snippet:
<script> function myFunction() { var w = window.outerWidth; var h = window.outerHeight; var d = Math.sqrt(w*w + h*h); console.log('Width: ' + w); console.log('Height: ' + h); console.log('Diagonal: ' + Math.ceil(d)); } </script> |
The above code will print the browser height and width in the console window. The console window can be opened using the developer tools in your browser.
Note: To get the complete screen dimensions, switch your browser to fullscreen mode. Most browsers switch the fullscreen mode using the F11 key.
The below code displays the results on the browser window using the innerHTML property of Javascript.
<!DOCTYPE html> <html> <body> <p> Switch to Fullscreen Mode using the F11 key. <br>Click the button to display the dimensions of this browser window.<br> All dimensions are in pixel units. </p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> function myFunction() { var i_w = window.innerWidth; var i_h = window.innerHeight; var o_w = window.outerWidth; var o_h = window.outerHeight; var d = Math.sqrt(o_w * o_w + o_h * o_h); document.getElementById("demo").innerHTML = "Inner Width: " + i_w + "<br>Inner Height " + i_h + "<br>Outer Width: " + o_w + "<br>Outer Height: " + o_h + "<br>Diagonal: " + Math.ceil(d); } </script> </body> </html> |
Output:
- Before pressing ‘Try It’ button:

- After pressing ‘Try It’ button:

Note: If you know the linear pixel density of your monitor screen, you can divide the dimensions obtained from the above code with the density to get the dimensions in centimeters or inches.
Recommended Posts:
- How to detect the device is an Android device using JavaScript ?
- How to get the width of device screen in JavaScript ?
- How to get the height of device screen in JavaScript ?
- How to detect touch screen device using JavaScript?
- How to get the Android version of your device using JavaScript?
- What is the difference between “screen” and “only screen” in media queries?
- Detect a device is iOS or not using JavaScript
- How to detect whether a device is iOS without using Regular Expression in JavaScript?
- How to get the size of screen, current web page and browser window using jQuery?
- How to detect a mobile device using PHP?
- How to detect the user's device using jQuery ?
- How to Dim entire screen except a fixed area using JavaScript ?
- How to Create Full Screen Overlay Navigation Bar using HTML CSS and JavaScript ?
- How to detect a mobile device in jQuery?
- How to pad a string to get the determined length using JavaScript ?
- JavaScript BOM | Window Screen
- How to set full-screen iframe with height 100% in JavaScript ?
- How to Create Skeleton Screen Loading Effect using CSS ?
- How to Align navbar logo to the left screen using Bootstrap ?
- How to Display Spinner on the Screen till the data from the API loads using Angular 8 ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


