Screen size, viewport size and devicePixelRatio explained
Your screen has one size in physical dots and another in the CSS pixels that web pages use. The box below reads the values your browser reports right now, and the guide explains what each number means.
Your browser reports
These values update when you resize the window or rotate the device. Nothing is sent anywhere.
Three different sizes
| Value | JavaScript | What it measures |
|---|---|---|
| Viewport | window.innerWidth, innerHeight | The visible page area in CSS pixels, including a vertical scrollbar if one shows |
| Layout width | document.documentElement.clientWidth | The viewport minus the scrollbar; what a width of 100% really gets |
| Screen | screen.width, screen.height | The whole display in CSS pixels, not just the browser window |
On a phone with the browser full screen, the viewport and screen widths are often the same. On a desktop, the viewport is whatever size the window happens to be, so the screen size tells you little about the page layout.
CSS pixels versus device pixels
A CSS pixel is a unit of layout, not a dot of light. High-density screens pack several hardware pixels into one CSS pixel so that text stays a readable size. The ratio is window.devicePixelRatio.
CSS pixels = device pixels ÷ devicePixelRatio
Worked conversions (example devices, rounded to whole pixels where needed):
- A phone panel 1170 device pixels wide with a ratio of 3 is 1170 ÷ 3 = 390 CSS pixels wide.
- A laptop panel 1920 pixels wide with Windows display scaling at 125% has a ratio of 1.25, so pages see 1920 ÷ 1.25 = 1536 CSS pixels.
- A panel 1080 pixels wide with a ratio of 2.75 gives 1080 ÷ 2.75 ≈ 392.7 CSS pixels; browsers report a whole or near-whole number, so the reverse calculation may come out one pixel off.
Browser zoom changes the ratio too. Zooming to 200% on a ratio-1 monitor makes devicePixelRatio report 2 and halves the viewport width in CSS pixels, which is why zooming can trigger a page’s mobile layout.
Why the ratio matters for images
An image shown 300 CSS pixels wide on a ratio-2 screen covers 600 device pixels. If the file is only 300 pixels wide, the browser stretches it and it looks soft. The usual fix is to offer several sizes and let the browser choose:
<img src="/art/photo-600.jpg"
srcset="/art/photo-300.jpg 300w, /art/photo-600.jpg 600w, /art/photo-900.jpg 900w"
sizes="(max-width: 640px) 100vw, 300px"
alt="Describe the photo here" width="300" height="200">
The sizes attribute says how wide the image will display, and the browser multiplies by the ratio to pick a file. Sending a large file to every visitor wastes data on low-ratio screens, so offer a range.
How to check the values by hand
- Open DevTools (F12, or Cmd + Option + I on a Mac) and go to the Console.
- Type
innerWidth,screen.widthanddevicePixelRatioone at a time and press Enter. - Resize the window and run
innerWidthagain. Only the viewport changes. - Press Ctrl + + to zoom and check
devicePixelRatioagain.
Common misunderstandings
- “My phone is 1170 pixels wide, so my layout needs 1170px.” Pages lay out in CSS pixels. That phone is about 390 CSS pixels wide.
- Using screen size to choose a layout. Media queries and container queries respond to the space the page actually has, which is what matters.
- Treating a ratio as fixed per device. Zoom and operating system scaling both change it while the page is open.
Questions
Why does my viewport height change while scrolling on a phone?
Mobile browsers hide and show their address bar as you scroll, which changes the visible height. CSS offers the svh, lvh and dvh units for the small, large and dynamic viewport heights.
Is screen size the same as screen resolution?
People use both phrases loosely. Resolution usually means the hardware pixel count; web pages work with the CSS pixel size, which equals the hardware count divided by the ratio.