javascript - scrollHeight and clientHeight inside overflow:autoscroll are equal - Stack Overflow

I know that there are hundreds of questions about scrollHeight and clientHeight, but I didn't see

I know that there are hundreds of questions about scrollHeight and clientHeight, but I didn't see one that answered my question so here it is:

I have a page with html,body height of 100%, and in the body a container DIV that is also stretched to the entirety of the document height. This container has overflow.

Inside of the container, there are two DIVs side by side (float left and right) and they are being scrolled inside of the container DIV.

This came up as I was trying to figure out what is the visible height of the divs inside of the scroll area. I assumed that clientHeight is the actual visible part, but apparently this is not the case.

What is the explanation, and how would I go about getting the height of the inner DIVs as they are displayed in the browser, without explicitly taking the height of the parent DIV?

Here's the piece of layout/code I was playing with: CodePen:

This is the layout:

<div id="container"> <!-- height: 100%, overflow: auto -->
  <div id="left-div"> <!-- float:left -->
    <div class="content">....lots of content....</div>
  </div>
  <div id="right-div"> <!-- float:right -->
    <div class="content">....lots of content....</div>
  </div>
</div>

Thanks.

I know that there are hundreds of questions about scrollHeight and clientHeight, but I didn't see one that answered my question so here it is:

I have a page with html,body height of 100%, and in the body a container DIV that is also stretched to the entirety of the document height. This container has overflow.

Inside of the container, there are two DIVs side by side (float left and right) and they are being scrolled inside of the container DIV.

This came up as I was trying to figure out what is the visible height of the divs inside of the scroll area. I assumed that clientHeight is the actual visible part, but apparently this is not the case.

What is the explanation, and how would I go about getting the height of the inner DIVs as they are displayed in the browser, without explicitly taking the height of the parent DIV?

Here's the piece of layout/code I was playing with: CodePen: http://codepen.io/nomaed/pen/qaqRgv

This is the layout:

<div id="container"> <!-- height: 100%, overflow: auto -->
  <div id="left-div"> <!-- float:left -->
    <div class="content">....lots of content....</div>
  </div>
  <div id="right-div"> <!-- float:right -->
    <div class="content">....lots of content....</div>
  </div>
</div>

Thanks.

Share Improve this question edited Sep 20, 2016 at 21:35 Alex Cushing 2861 silver badge17 bronze badges asked Sep 20, 2016 at 19:17 nomædnomæd 4705 silver badges13 bronze badges 1
  • Add height: 100% to left-div (remove overflow:hidden, no idea why u put it there) then clientHeight is correct. – Bojidar Stanchev Commented Oct 25, 2019 at 15:32
Add a ment  | 

1 Answer 1

Reset to default 4

overflow: auto; es into effect when a block element contains more than its available space, that is, when its height is limited. In your example, this effects #container, which gets scrollbars because the children #left-div and #right-div takes up lots of space. When this is the case, the value of scrollHeight and clientHeight will differ, as is the case for #container.

However, #left-div and #right-div do not have height limitations nor scrollbars themselves, which makes their actual height—clientHeight—to equal their scrollHeight. The fact that they are not fully visible, is because of the height limitation and overflow: auto of their parent, #container.

I assumed that clientHeight is the actual visible part, but apparently this is not the case.

The visible height of #left-div and #right-div is restricted by the visible height of #container, which you only get from #container.clientHeight.

For more info on this, check MDN: Determining the dimensions of elements, where you can read more about clientHeight and scrollHeight.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745270189a4619697.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信