Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between window.innerWidth window.outerWidth?

Tags:

javascript

I examined the window object in Firebug. window.innerWidth and window.outerWidth are both 1230.

What is the difference between these two values?

like image 406
John Hoffman Avatar asked Aug 22 '12 03:08

John Hoffman


People also ask

What is the difference between innerWidth and outerWidth?

innerWidth / innerHeight - includes padding but not border. outerWidth / outerHeight - includes padding, border, and optionally margin. height / width - element height (no padding, no margin, no border)

What is window outerWidth?

Window. outerWidth read-only property returns the width of the outside of the browser window. It represents the width of the whole browser window including sidebar (if expanded), window chrome and window resizing borders/handles.

Does window innerWidth include scrollbar?

window. innerWidth/innerHeight includes the scrollbar. In most cases, we need the available window width in order to draw or position something within scrollbars (if there are any), so we should use documentElement.

Can I use window innerHeight?

Usage notesBoth innerHeight and innerWidth are available on any window or any object that behaves like a window, such as a tab or frame.


Video Answer


2 Answers

From Mozilla Developer Network:

window.outerWidth

window.outerWidth gets the width of the outside of the browser window. It represents the width of the whole browser window including sidebar (if expanded), window chrome and window resizing borders/handles.

and

window.innerWidth

Width (in pixels) of the browser window viewport including, if rendered, the vertical scrollbar.

This is ostensibly how they work. A test page, however, shows they are both the same no matter how I resize them in Firefox 14.0.1 on Ubuntu 12.04. In Chromium, they return numbers that are 8 pixels different. Doing a bit of dead reckoning, it appears the window chrome on that particular app is about 4 pixels on the left side and 4 pixels on the right side and that this difference is being correctly picked up in that browser.

Code for the test page I used:

<!DOCTYPE html> <html lang="en"> <head>     <title>12066093</title>     <meta charset="utf-8"> </head> <body> <div style="width:2000px; height: 2000px;">hi</div> <script type="text/javascript">     alert("window.innerWidth: " + window.innerWidth + "\nwindow.outerWidth: " + window.outerWidth); </script> </body> </html> 
like image 69
chucksmash Avatar answered Oct 03 '22 18:10

chucksmash


Check the Mozilla reference for window.innerWidth and window.outerWidth. If your operating system/window manager has them, window.outerWidth includes the window borders, resizing handles, and sidebars.

Try opening the bookmarks or history sidebar, then trying that again in Firebug.

like image 38
Snowball Avatar answered Oct 03 '22 17:10

Snowball