Question
Get Screen, Page, and Browser Window Size in JavaScript
Question
How can I get the following values in JavaScript so they work reliably across major browsers?
windowWidthwindowHeightpageWidthpageHeightscreenWidthscreenHeightpageXpageYscreenXscreenY
I want a cross-browser way to measure:
- the browser window or viewport size
- the full page or document size
- the physical screen size
- the page scroll position
- the browser window position on the screen
Short Answer
By the end of this page, you will understand the difference between screen size, viewport size, document size, scroll position, and browser window position in JavaScript. You will also learn the safest cross-browser properties to use, how they differ, and how to combine them into reusable helper functions.
Concept
In browser JavaScript, size and position can mean several different things. That is why properties like window.innerWidth, document.body.scrollWidth, and screen.width are not interchangeable.
The main idea is that the browser exposes different coordinate systems:
- Screen: the user's monitor or device display
- Viewport: the visible part of the web page inside the browser window
- Document/Page: the full web page, including content outside the visible area
- Scroll position: how far the user has moved through the page
- Window position: where the browser window sits on the screen
Understanding these differences matters because real applications often need accurate measurements for:
- responsive layouts
- modal positioning
- sticky headers
- infinite scrolling
- drag-and-drop UI
- analytics and viewport tracking
Here are the most common measurements:
window.innerWidth/window.innerHeight→ viewport sizedocument.documentElement.clientWidth/clientHeight→ fallback viewport size in older situationsdocument.documentElement.scrollWidth/scrollHeightand related values → full document size
Mental Model
Think of a website as a large poster inside a movable frame sitting on a desk.
- The desk is the user's screen.
- The frame opening is the viewport.
- The full poster is the page/document.
- Sliding the poster behind the frame is scrolling.
- Moving the whole frame across the desk is the browser window position.
So:
screenWidth/screenHeight= size of the deskwindowWidth/windowHeight= size of the frame openingpageWidth/pageHeight= size of the full posterpageX/pageY= how far the poster has been slidscreenX/screenY= where the frame sits on the desk
Syntax and Examples
Core properties
// Viewport size
const windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
const windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
// Full page size
const pageWidth = Math.max(
document.body.scrollWidth,
document.body.offsetWidth,
document.documentElement.clientWidth,
document.documentElement.scrollWidth,
document.documentElement.offsetWidth
);
const pageHeight = Math.(
..,
..,
..,
..,
..
);
screenWidth = screen.;
screenHeight = screen.;
pageX = . || .. || .. || ;
pageY = . || .. || .. || ;
screenX = . !== ? . : .;
screenY = . !== ? . : .;
Step by Step Execution
Consider this example:
function getViewportSize() {
const width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
const height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
return { width, height };
}
const size = getViewportSize();
console.log(size);
Step by step:
- The function starts by calculating
width. - It first tries
window.innerWidth.- In modern browsers, this usually exists and gives the viewport width.
- If that is not available, it tries
document.documentElement.clientWidth.- This often works as a fallback.
Real World Use Cases
Responsive UI logic
JavaScript can check viewport size to decide whether to open a mobile menu, collapse a sidebar, or resize a canvas.
if (window.innerWidth < 768) {
console.log("Use mobile layout");
}
Infinite scroll and lazy loading
You need the scroll position and viewport height to know when the user is near the bottom of the page.
const nearBottom = window.innerHeight + window.pageYOffset >= document.documentElement.scrollHeight - 200;
Centering popups or dialogs
You may need viewport and scroll values to place a floating element correctly.
Analytics and user behavior tracking
Applications sometimes record viewport size to understand how users view content.
Multi-window tools
Apps that open helper windows may use screenX and screenY to position or track browser windows.
Real Codebase Usage
In real projects, developers usually wrap these browser measurements in utility functions instead of reading raw properties everywhere.
Common patterns
Guarded access
Code often uses a fallback chain because browser APIs can differ:
const scrollY = window.pageYOffset || document.documentElement.scrollTop || 0;
Reusable utility module
function getScrollPosition() {
return {
x: window.pageXOffset || document.documentElement.scrollLeft || 0,
y: window.pageYOffset || document.documentElement.scrollTop || 0
};
}
Resize and scroll listeners
Measurements are often recalculated when the user resizes or scrolls.
.(, {
.(, ., .);
});
.(, {
.(, .);
});
Common Mistakes
Confusing viewport size with page size
This is a very common mistake:
const pageHeight = window.innerHeight;
This gives the visible viewport height, not the full document height.
Use document measurements instead:
const pageHeight = Math.max(
document.body.scrollHeight,
document.documentElement.scrollHeight,
document.body.offsetHeight,
document.documentElement.offsetHeight,
document.documentElement.clientHeight
);
Confusing scroll position with window position
These are different:
pageXOffset/pageYOffset= scroll inside the pagescreenX/screenY= browser window position on the monitor
Comparisons
| Measurement | What it means | Common property | Notes |
|---|---|---|---|
| Viewport width/height | Visible area inside browser | window.innerWidth, window.innerHeight | Best for responsive UI logic |
| Document width/height | Full page including off-screen content | scrollWidth, scrollHeight with Math.max(...) | Best for full page measurement |
| Screen width/height | Physical device or monitor size | screen.width, screen.height | Not the same as browser size |
| Scroll position | How far page has been scrolled | , |
Cheat Sheet
// Viewport size
const windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
const windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
// Full document size
const pageWidth = Math.max(
document.body.scrollWidth,
document.body.offsetWidth,
document.documentElement.clientWidth,
document.documentElement.scrollWidth,
document.documentElement.offsetWidth
);
const pageHeight = Math.max(
document..,
..,
..,
..,
..
);
screenWidth = screen.;
screenHeight = screen.;
pageX = . || .. || .. || ;
pageY = . || .. || .. || ;
screenX = . !== ? . : .;
screenY = . !== ? . : .;
FAQ
What is the difference between window.innerWidth and screen.width?
window.innerWidth is the width of the visible browser viewport. screen.width is the width of the user's screen or monitor.
How do I get the full height of a web page in JavaScript?
Use a Math.max(...) combination of document measurements such as scrollHeight, offsetHeight, and clientHeight.
How do I get the current scroll position of the page?
Use window.pageYOffset for vertical scroll and window.pageXOffset for horizontal scroll, with fallbacks if needed.
What are screenX and screenY used for?
They tell you where the browser window is located on the screen, not where the page is scrolled.
Why do cross-browser examples use several fallback properties?
Different browsers and document modes have historically exposed size and scroll values through different properties.
Should I use document.body.scrollTop?
Mini Project
Description
Build a small browser metrics inspector that displays the current viewport size, full page size, screen size, scroll position, and browser window position. This is useful for learning how browser measurement APIs differ and for debugging layout issues.
Goal
Create a live JavaScript utility that reads and displays window, page, screen, and scroll measurements whenever the page loads, scrolls, or resizes.
Requirements
- Display viewport width and height
- Display full page width and height
- Display screen width and height
- Display current page scroll X and Y values
- Update the values when the user scrolls or resizes the window
Keep learning
Related questions
Can You Style Half a Character in CSS? Text Effects with CSS and JavaScript
Learn how to style half of a character using CSS and JavaScript, including overlay techniques for dynamic text effects.
Get the Selected Radio Button Value with jQuery
Learn how to find which radio button is selected in jQuery and get its value with simple examples and common mistakes.
HTML id Attribute Rules: Valid Values and Best Practices
Learn which HTML id values are valid, how browsers treat them, and the best practices for naming ids in modern HTML and CSS.