Body height 100% displaying vertical scrollbar

后端 未结 13 2141
予麋鹿
予麋鹿 2020-12-07 21:48

Out of curiosity, considering the example below, why does having the margin on the #container div cause a vertical scrollbar to appear in the browser? The container is much

13条回答
  •  暖寄归人
    2020-12-07 22:29

    If you paint the backgrounds of html and body (giving each its own color), you'll quickly notice that body is being shifted down along with #container, and #container itself isn't offset from the top of body at all. This is a side effect of margin collapse, which I cover in detail here (although that answer describes a slightly different setup).

    It's this behavior that's causing the scrollbar to appear, since you've declared body to have 100% the height of html. Note that the actual height of body is unaffected, as margins are never included in height calculations.

提交回复
热议问题