7

If I have the following markup

<html>
<body>
<div id="wrapper">
<div id="container">
<p>Lots of pragraphs here</p>
</div>
</div>
</body>
</html>

with the following styles

html, body, #wrapper
{
width: 100%;
height: 100%;
}
#container
{
width: 960px;
margin: 0 auto;
}

why does not my html, body and wrapper elements extend to 100% height of the browser/view port in FF13. The html, body and wrapper stop vertically about some distance from the bottom when looking in Firebug. The container div extends to the full height as it's height is determined by the content.

(1263px X 558px for html, body, wrapper) and (960px X 880px for container)

Looking at default 100% the above happens as the first image below shows. But when I zoom to the last poosible zoom in, the above does not happen as the second image below shows and the html, body, wrapper extends to the full height.

(4267px X 1860px for html, body, wrapper) - (960px X 1000px for container)

enter image description here enter image description here

Jawad
  • 8,352
  • 10
  • 40
  • 45
  • 1
    'wrapper' and 'container' aren't valid HTML elements :/ – Billy Moat Aug 16 '12 at 21:06
  • @BillyMoat: it's a div not a wrapper element.
    – Jawad Aug 16 '12 at 21:09
  • @BillyMoat: changed as it was a typo. – Jawad Aug 16 '12 at 21:11
  • Wrapper and container are exactly like that on the .css? They should have a '#' identifying them as ids. – petervaz Aug 16 '12 at 21:13
  • In your real CSS file do you have hash tags before your element names? – Billy Moat Aug 16 '12 at 21:13
  • @BillyMoat: Yeah I changed that as well. – Jawad Aug 16 '12 at 21:15
  • I've read this a couple times and am still not sure what you're looking for. Could you sum it up again, please? – DACrosby Aug 20 '12 at 02:41
  • @DouglasA.Crosby: Just need the html, body and div#wrapper to extend to 100% height as they extend to 100% width when I use the css as html, body, div#wrapper {width: 100%; height: 100%;}. In this case the width extends to 100% but height stops at some distance from the bottom when zoom in. It may be something very simple and I may be ingnorant of it. – Jawad Aug 20 '12 at 20:40
  • Ohh okay. Yea, so as @caligul said, a sticky footer will be what you're looking for. If you don't want the footer to actually be "sticky", just make a pseudo 1px empty div absolutely positioned footer - it'll push the `html`, `body`, and `container` elements all the way to the bottom. – DACrosby Aug 20 '12 at 21:08
  • @DouglasA.Crosby: Well in a absolutely positioned footer, what will happen when the content extends more. The footer will overlap. – Jawad Aug 20 '12 at 21:11
  • The idea of a sticky footer is that it stays at the bottom of the page, regardless of content. Take a look at http://www.cssstickyfooter.com/. The footer stays at the bottom no matter how you zoom, and even if you use Firebug and add a ton of content to their `content` div, the footer stays at the bottom (with the `html`, `body`, and potentially an extra `container` div following suit) - no overlap. – DACrosby Aug 20 '12 at 21:19

2 Answers2

30

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):

<div id="div1">
    <div  id="div2">
        <div  id="div3">
            very much content
        </div>
    </div>
</div>

#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:

  1. it gets height: 100% for <html>. 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.
  2. then it takes height: 100% for <body>. It will set body's height to the already calculated height of the html, that is 1024px.
  3. then browser applies height:auto to the #wrapper and then the #container and the <p>. 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.
  4. 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.
  5. after that it will set height to the <p>-element so the <p> 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):

scrolled part of the body

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 <p>, #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: zoomed out page

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: same issue but with the width

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

d.k
  • 4,234
  • 2
  • 26
  • 38
  • So how to solve this. If I give a background color to the div#wrapper (div#3 in your case), the background color (say red)only extends to the same height of html & body (div#1 & div#2 in you case) and this causes the background color to NOT extend to the full height and when zoomed out only some part is red and at the bottom some part is white/transparent (color of html & body). – Jawad Aug 16 '12 at 22:13
  • @Jawad, you can attach a background to some wrapper of your content, so it will extend accordingly. Also you may want to use so called `sticky footer`, so the footer will be at the bottom of the window if there is not enough content (as at your second picture). See this http://stackoverflow.com/questions/11606856/background-repeat-with-html-body-height-to-100-with-css-sticky-footer/11606934#11606934 – d.k Aug 16 '12 at 22:18
  • I know all about sticky footers. Check my profile out. I am not asking that. Neither I want the html {height: 100%} to have a height of the inner content. It has nothing to do with inner content (div#container in my case). I want the html, body and div#wrapper to have a height of 100% as it is having the width of 100%. If it relates to parent's height, than the parent of div#wrapper is body and the parent of body is html and the parent of html is the browser window/viewport. Given a background on the wrapper (color red) and 100% height set on html, body & div#wrapper causes this problem. – Jawad Aug 16 '12 at 22:30
  • You don't need to use @ in you own answers. Only when you comment on other's answers. – Jawad Aug 16 '12 at 22:32
  • the html, body and div#wrapper all have a height of 100% as they are having the width of 100%. Issue here is that height of the content is bigger than height of the window. Try to set `#container {width: 6000px;}` and you'll see the same but now for the horizontal dimension. If you need to add some background it is only possible in such case to add some wrapper, may be set `min-width`, `min-height` for it depending what you want – d.k Aug 16 '12 at 22:37
  • actually #container does not have a height of 100% cause it hasn't set height. It has a height of the inner content – d.k Aug 16 '12 at 22:52
  • "There is nothing to solve here" and that required a big EDIT! – Jawad Aug 23 '12 at 01:18
  • Sorry, I did not understand. What did you mean? – d.k Aug 23 '12 at 05:16
1

Because you can never set the height to 100% if the element is relative to the browser window. The reason for this is that because of scrolling, your browser window could potentially be infinitely big. You will have to set a fixed height, or you will just have to set the height to expand to whatever is inside of it.

However width: 100%; is perfectly valid.

You will also need to use valid html tags. what I would do is, instead of using <wrapper> and <container>, I would make a class in your css. Class names are declared by starting them with a period.

.container{
width: 100%;
}

<div class="container"></div>

Good Luck,

-Brian

OneChillDude
  • 7,856
  • 10
  • 40
  • 79