MDN Web Docs

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

You can also view the results of the demo code in a separate page.

Specifications

Specification
CSSOM View Module
# dom-window-innerwidth

Browser compatibility

See also

Help improve MDN

Yes No

Learn how to contribute

This page was last modified on by MDN contributors.

Read the original on developer.mozilla.org ↗