Div 100% height works on Firefox but not in IE
Asked Answered
W

8

46

I have a container div that holds two internal divs; both should take 100% width and 100% height within the container.

I set both internal divs to 100% height. That works fine in Firefox, however in IE the divs do not stretch to 100% height but only the height of the text inside them.

The following is a simplified version of my style sheet.

#container
{
   height: auto;
   width: 100%;
}

#container #mainContentsWrapper
{
   float: left;

   height: 100%;
   width: 70%;
   margin: 0;
   padding: 0;
}


#container #sidebarWrapper
{      
   float: right;

   height: 100%;
   width: 29.7%;
   margin: 0;
   padding: 0;
}

Is there something I am doing wrong? Or any Firefox/IE quirks I am missing out?

Waterhouse answered 6/10, 2008 at 0:40 Comment(0)
R
74

I think "works fine in Firefox" is in the Quirks mode rendering only. In the Standard mode rendering, that might not work fine in Firefox too.

percentage depends on "containing block", instead of viewport.

CSS Specification says

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'.

so

#container { height: auto; }
#container #mainContentsWrapper { height: n%; }
#container #sidebarWrapper { height: n%; }

means

#container { height: auto; }
#container #mainContentsWrapper { height: auto; }
#container #sidebarWrapper { height: auto; }

To stretch to 100% height of viewport, you need to specify the height of the containing block (in this case, it's #container). Moreover, you also need to specify the height to body and html, because initial Containing Block is "UA-dependent".

All you need is...

html, body { height:100%; }
#container { height:100%; }
Restrainer answered 6/10, 2008 at 22:26 Comment(1)
great explanation dudePresocratic
M
2

Its hard to give you a good answer, without seeing the html that you are actually using.

Are you outputting a doctype / using standards mode rendering? Without actually being able to look into a html repro, that would be my first guess for a html interpretation difference between firefox and internet explorer.

Mistook answered 6/10, 2008 at 1:36 Comment(0)
O
2

I'm not sure what problem you are solving, but when I have two side by side containers that need to be the same height, I run a little javascript on page load that finds the maximum height of the two and explicitly sets the other to the same height. It seems to me that height: 100% might just mean "make it the size needed to fully contain the content" when what you really want is "make both the size of the largest content."

Note: you'll need to resize them again if anything happens on the page to change their height -- like a validation summary being made visible or a collapsible menu opening.

Osmanli answered 6/10, 2008 at 1:46 Comment(2)
++ You will spend much less time getting it right with javascript than you will trying to do it with css. I know it's "bad practice" in the IT world to do it this way...but in the business world, "bad practice" is spending 10 hours doing something in css that could be done in 5 mins with javascript.Lyssa
@Mr.JavaScript yes, but with a grain of salt: then you need to think about the window resizing, or collapsible items toggling or... "quick fixes" are often not quick.Wildfowl
D
2

I've been successful in getting this to work when I set the margins of the container to 0:

#container
{
   margin: 0 px;
}

in addition to all your other styles

Disapprobation answered 6/10, 2008 at 14:51 Comment(0)
J
1

You might have to put one or both of:

html { height:100%; }

or

body { height:100%; }

EDIT: Whoops, didn't notice they were floated. You just need to float the container.

Jeanettajeanette answered 6/10, 2008 at 0:43 Comment(0)
A
1

I've done something very similar to what 'tvanfosson' said, that is, actually using JavaScript to constantly monitor the available height in the window via events like onresize, and use that information to change the container size accordingly (as pixels rather than percentage).

Keep in mind, this does mean a JavaScript dependency, and it isn't as smooth as a CSS solution. You'd also need to ensure that the JavaScript function is capable of correctly returning the window dimensions across all major browsers.

Let us know if one of the previously mentioned CSS solutions work, as it sounds like a better way to fix the problem.

Audwen answered 6/10, 2008 at 13:25 Comment(0)
B
0

I don't think IE supports the use of auto for setting height / width, so you could try giving this a numeric value (like Jarett suggests).

Also, it doesn't look like you are clearing your floats properly. Try adding this to your CSS for #container:

#container {
    height:100%;
    width:100%;
    overflow:hidden;
    /* for IE */
    zoom:1;
}
Borlase answered 6/10, 2008 at 13:10 Comment(0)
C
0

Try this..

#container
{
   height: auto;
   min-height:100%;
   width: 100%;
}

#container #mainContentsWrapper
{
   float: left;

   height: auto;
   min-height:100%
   width: 70%;
   margin: 0;
   padding: 0;
}


#container #sidebarWrapper
{      
   float: right;

   height: auto;
   min-height:100%
   width: 29.7%;
   margin: 0;
   padding: 0;
}
Calore answered 15/12, 2009 at 11:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.