3

I wrote this code.

When I added scroll-bar, I got 403 as clientWidth, and this seems like true: 403px for inner content + 17px for scroll-bar. And then I got 403 as offsetWidth, but offsetWidth must include scroll-bar width. Therefore I should get 403+17, not 403.

Why do I get 403 as result in (*)?

let html = document.documentElement
console.log(window.innerWidth) // 420 on my test
console.log(html.clientWidth) // 420
console.log(html.offsetWidth) // 420

html.style.overflowY = 'scroll' // add scroll-bar
console.log('Window: ' + window.innerWidth) // Window: 420
console.log('clientWidth: ' + html.clientWidth) // clientWidth: 403
console.log('offsetWidth: ' + html.offsetWidth) // offsetWidth: 403 (*)

4
  • Maybe on the document element it doesn’t include the scroll bar? Commented Jul 27, 2021 at 12:52
  • I made a snippet and changed one 403 to 420
    – mplungjan
    Commented Jul 27, 2021 at 12:55
  • What browsers have you tested this in? They all render the same value in Mac Firefox. Commented Jul 27, 2021 at 13:24
  • I used Google Chrome browser.
    – dedavarera
    Commented Jul 27, 2021 at 13:33

1 Answer 1

2

You almost got us! But there it is...

Take a closer look at the documentElement.

Layout of documentElement

The scrollbar is not included! Therefore the only way to get the site width including scrollbars is window.innerWidth.

The read-only Window property innerWidth returns the interior width of the window in pixels. This includes the width of the vertical scroll bar, if one is present.

The value of offsetWidth will include scrollbars for all elements except the documentElement.
offsetWidth of documentElement includes scrollbars, but it simply doesn't have any. Which means that offsetWidth and clientWidth are the same.

12
  • Thanks, bro! You helped me a lot.
    – dedavarera
    Commented Jul 27, 2021 at 17:42
  • I must say I didn't know this either. So thank you too! If you wonder where I got the image, you can inspect everything yourself! Commented Jul 27, 2021 at 18:17
  • I don't see where in your links or quote it is said that offsetWidth should not take the vertical scrollbar into account for document.documentElement. There is a special case for this documentElement (or body in Quirks mode) in the specs for clientWidth, where they state it should match the viewport's width minus the scrollbar, but I can't see anything in offsetWidth. Could you pin-point and quote exactly where it is said it should behave like this? Currently your answer is just stating what the question was already stating. The question is Why does it behaves like that.
    – Kaiido
    Commented Jul 28, 2021 at 0:34
  • For sure, you can read about this at Windows sizes and scrolling. They don't mention offsetWidth but simply teach to use window.innerWidth for this case. The question is not why it behaves like that, but why the value is incorrect. And the value is correct. The scrollbar is not placed on the documentElement, but on the window. Commented Jul 28, 2021 at 7:02
  • 3
    @Kaiido there is the overflow propagation (drafts.csswg.org/css-overflow-3/#overflow-propagation) that play a role here but don't have more accurate information. See also: stackoverflow.com/questions/68424361/… Commented Jul 28, 2021 at 14:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.