W3cubDocs

/Web APIs

Window: innerWidth property

The read-only Window property innerWidth returns the interior width of the window in pixels (that is, the width of the window's layout viewport). That includes the width of the vertical scroll bar, if one is present.

Similarly, the interior height of the window (that is, the height of the layout viewport) can be obtained using the innerHeight property. That measurement also accounts for the height of the horizontal scroll bar, if it is visible.

Value

An integer value indicating the width of the window's layout viewport in pixels. This property is read-only, and has no default value.

To change the window's width, use one of the Window methods for resizing windows, such as resizeBy() or resizeTo().

Usage notes

If you need to obtain the width of the window minus the scrollbar and borders, use the root <html> element's clientWidth property instead.

The innerWidth property is available on any window or object that behaves like a window, such as a frame or tab.

Examples

js

// This will log the width of the viewport
console.log(window.innerWidth);

// This will log the width of the frame viewport within a frameset
console.log(self.innerWidth);

// This will log the width of the viewport of the closest frameset
console.log(parent.innerWidth);

// This will log the width of the viewport of the outermost frameset
console.log(top.innerWidth);

Demo

HTML

html

<p>Resize the browser window to fire the <code>resize</code> event.</p>
<p>Window height: <span id="height"></span></p>
<p>Window width: <span id="width"></span></p>

JavaScript

js

const heightOutput = document.querySelector("#height");
const widthOutput = document.querySelector("#width");

function updateSize() {
  heightOutput.textContent = window.innerHeight;
  widthOutput.textContent = window.innerWidth;
}

updateSize();
window.addEventListener("resize", updateSize);

Result

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
innerWidth 1 12
1From version 4 to 24, this property could give a wrong value before page load in certain circumstances (see bug 641188).
9 9 3 4.4 18
4Before version 24, this property could give a wrong value before page load in certain circumstances (see bug 641188).
10.1
1This property returns the width of the visual viewport instead of the layout viewport. See this bug for details.
1.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Window/innerWidth