JavaScript screen Size vs. Viewport Width

Read window.screen.width and height for the screen dimensions reported by the browser. Use window.innerWidth and innerHeight for the layout viewport, the area used to lay out the page. Base responsive layouts on the viewport.

Read screen dimensions

Screen properties are read-only. Dimensions are in CSS pixels and do not give the display’s physical resolution. Privacy settings and multi-monitor setups can affect the values the browser reports. CSSOM View · Screen

Property What it measures
width, height Full screen size reported by the browser
availWidth, availHeight Screen area reported as available to windows
colorDepth, pixelDepth Reported color bits per pixel

availWidth and availHeight can exclude areas reserved by the operating system. The difference from the full size is not a reliable measurement of the taskbar.

Distinguish the layout and visual viewports

innerWidth includes space occupied by a vertical scrollbar. In standards mode, use document.documentElement.clientWidth to exclude that space. Use CSS media queries for styling and matchMedia() when JavaScript needs to evaluate the same condition. CSSOM View · Window.innerWidth

console.log(window.screen.width);
console.log(window.innerWidth);
console.log(document.documentElement.clientWidth);
console.log(window.matchMedia('(max-width: 600px)').matches);

Pinch zoom can change the visual viewport, the part actually visible to the user. Read window.visualViewport.width where supported. The layout viewport need not change in proportion. CSSOM View · VisualViewport

Compare measurements in a standalone page

Save the code as screen-info.html in UTF-8 and open it in a browser. On the same monitor, narrow the window and compare innerWidth with screen.width. If the browser reports the display’s dimensions, screen.width can stay the same as the window shrinks.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Compare screen and viewport dimensions</title>
</head>
<body>
<button id="refresh" type="button">Read values again</button>
<pre id="result" aria-live="polite"></pre>
<script>
const result = document.querySelector('#result');
const { screen, visualViewport } = window;
const unavailable = "Unavailable";

function showSizes() {
  const values = [
    ['screen.width', screen.width],
    ['screen.height', screen.height],
    ['screen.availWidth', screen.availWidth],
    ['screen.availHeight', screen.availHeight],
    ['screen.colorDepth', screen.colorDepth],
    ['screen.pixelDepth', screen.pixelDepth],
    ['window.innerWidth', window.innerWidth],
    ['window.innerHeight', window.innerHeight],
    ['document.documentElement.clientWidth', document.documentElement.clientWidth],
    ['visualViewport.width', visualViewport?.width ?? unavailable],
    ['visualViewport.height', visualViewport?.height ?? unavailable]
  ];
  result.textContent = values.map(([name, value]) => `${name}: ${value}`).join('\n');
}

document.querySelector('#refresh').addEventListener('click', showSizes);
window.addEventListener('resize', showSizes);
visualViewport?.addEventListener('resize', showSizes);
showSizes();
</script>
</body>
</html>

The example updates when the window or visual viewport resizes. If moving to another display does not trigger an update, use the refresh button. If the visual viewport object is unavailable, the example shows that explicitly.