html body width,css - height and width on html and body elements - Stack Overflow

Your html actually exactly extends to 100% height of your viewport cause viewport here is the browser window, not the inner content.

Consider this (jsfiddle):

very much content

#div1 {

height:300px;

overflow-y:scroll;

border: 1px solid black;

}

#div2 {

height:100%;

}

#div3 {

height:600px;

}

div1 here has the height of 300px and is scrolled. When you scroll content you simply move inner div but height remains untouched that is 300px. Exactly the same happens when you set height:100% to html. Your browser's height remains the same.

When you zoomed out your viewport then you have not scroll, so inner content's height is less than the height of viewport.

Shortly, html {height:100%} relates to parent's height not to the height of the inner content

UPDATE:

you can specify 3 types of values to the block-element's height:

length - set fixed height (i.g. '200px', '50em'). That's all, I can say nothing more about that.

percentage - from W3C spec:

The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to 'auto'. A percentage height on the root element is relative to the initial containing block.

auto - The height depends on the values of other properties. (Generally on the height of inner content: text, other inline elements, block elements etc.)

What is happening when browser shows your page:

it gets height: 100% for . That means that the resulting height is calculated with respect to the height of the generated box's (html-element in that case) containing block (initial containing block, i.e. browser window in that case). Let's say 1024px.

then it takes height: 100% for

. It will set body's height to the already calculated height of the html, that is 1024px.

then browser applies height:auto to the #wrapper and then the #container and the

. I don't know how it does that exactly but can suppose that it postpones the height setting (and respectively all other styles which depend on that i.e. backgrounds, borders etc.) and proceeds to the inner content.

next point is text content. Browser takes related properties specified or it's own, that is default styles, like font-family, font-size and the height of the text.

after that it will set height to the

-element so the

will stretch down to contain all content (the text in that case). The same then happens to the #container and the #wrapper.

If it happens that the height of the #wrapper is greater than the body's one (1024 px as it were agreed) than the overflow should be applied to the body. That is visible which is the default. Then overflow: visible is applied to the html. Then browser shows scroll for the entire window. Honestly, I don't know whether this is specified by the W3C spec, but can suppose it is.

So when you scroll the window your html and body are moved as are all the other elements. This is the same behavior as is with any other elements (like in jsfiddle I posted above):

xzKE8.jpg

Note that the background is set on the body element, but it extends to the entire canvas i.e. far beyond of the body element itself. This is towards your concern of the possible necessity of setting bg-property on the body. This is 100% compliant with the W3C spec which states (cutted):

For documents whose root element is an ... "html" element that has computed values of 'transparent' for 'background-color' and 'none' for 'background-image', user agents must instead use the computed value of the background properties from that element's first ... "body" element child when painting backgrounds for the canvas, and must not paint a background for that child element. Such backgrounds must also be anchored at the same point as they would be if they were painted only for the root element.

When you zoom out your page then browser recalculates all dimensions. Let's say, with each Ctrl + - click page shrinks, for example, for 20 %. Then all your text is reduced, cause its height depends on the font-size, which is affected by the Ctrl + - click, correspondingly

, #container and #wrapper all are reduced cause their height depends on text's height. But body and html both have height which depends on the window's height which is not affected by the Ctrl + - click. That is why you finally get this:

LH1wM.jpg

There is no difference here between width and height behavior in that case. You don't see the same issue with horizontal dimension simply because you've set width: 960px; for the #container which turned out to be less than your browser window's width, so no overflowing occurs. If the width of the #container were exceeding body's width you would see this:

3Nxb7.jpg

This all is a normal and expected behavior and there is nothing to solve here.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值