In JavaScript, browser viewport, full document page, and screen measure three distinct boundaries: the visible browser window area, the entire rendered document including scrolled overflow, and the user’s physical display. Knowing which DOM or window properties map to each boundary prevents layout bugs in modals, infinite scroll, and responsive UI logic.
1. Browser Viewport and Screen Dimensions
The viewport is the currently visible browser area, whereas window.screen represents the user’s monitor or device display:
// Measure browser viewport (with and without scrollbars) and physical screen
function getViewportAndScreenSize() {
return {
viewportWithScrollbars: {
width: window.innerWidth,
height: window.innerHeight
},
viewportWithoutScrollbars: {
width: document.documentElement.clientWidth,
height: document.documentElement.clientHeight
},
screenTotal: {
width: window.screen.width,
height: window.screen.height
},
screenAvailable: {
width: window.screen.availWidth,
height: window.screen.availHeight
}
};
}
console.log(getViewportAndScreenSize());
window.innerWidth/window.innerHeight: Returns the visible viewport width and height in CSS pixels, including any vertical or horizontal scrollbar tracks.document.documentElement.clientWidth/clientHeight: Returns the inner viewport size excluding scrollbars, which is ideal for fitting fixed overlays cleanly inside visible space.window.screen.widthvs.availWidth:widthandheightreport the full display resolution, whileavailWidthandavailHeightsubtract OS taskbars or docks.
2. Full Document Page Dimensions (Including Scrolled Content)
To measure the entire page height and width—including content scrolled off-screen—compare scrollWidth/scrollHeight and offsetWidth/offsetHeight across document.documentElement and document.body:
// Measure full document page dimensions reliably across browsers
function getDocumentPageSize() {
const docEl = document.documentElement;
const body = document.body;
const width = Math.max(
docEl.scrollWidth,
docEl.offsetWidth,
docEl.clientWidth,
body ? body.scrollWidth : 0,
body ? body.offsetWidth : 0
);
const height = Math.max(
docEl.scrollHeight,
docEl.offsetHeight,
docEl.clientHeight,
body ? body.scrollHeight : 0,
body ? body.offsetHeight : 0
);
return { width, height };
}
console.log("Full document page size:", getDocumentPageSize());
Math.max(...)acrossdocumentElementandbody: Different CSS overflow rules (htmlvs.bodyscrolling) can place the true scrollable height on either node, so taking the maximum prevents under-measuring.- Dynamic resize tracking: Re-read these values inside a throttled
window.addEventListener("resize", ...)orResizeObservercallback when page content expands or collapses.
3. Common Traps to Avoid
Layout dimension properties look similar, so mixing them up causes subtle positioning and scaling issues:
- Confusing viewport height with full document height:
window.innerHeightonly reports what is visible on screen right now, whereasdocumentElement.scrollHeightincludes everything you have to scroll to reach. - Forgetting
window.devicePixelRatioon high-DPI screens: All DOM andwindow.screendimension APIs return CSS pixels; multiply bywindow.devicePixelRatiowhen sizing<canvas>buffers or checking physical pixels. - Ignoring mobile
window.visualViewport: On mobile devices, pinch-zooming or opening an on-screen keyboard changeswindow.visualViewport.heightwhilewindow.innerHeightoften remains at the layout viewport size.
TL;DR: Use document.documentElement.clientWidth/clientHeight (or window.innerWidth/innerHeight) for the viewport, Math.max(...) on scrollWidth/scrollHeight for the full document page, and window.screen for the user’s display.
Are you measuring these dimensions to position a tooltip/modal, trigger infinite scrolling, or handle mobile keyboard resize behavior?
