tags:

views:

7361

answers:

5

Let's say I have a parent DIV. Inside, there are three child DIVs: header, content and footer. Header is attached to the top of the parent and fills it horizontally. Footer is attached to the bottom of the parent and fills it horizontally too. Content is supposed to fill all the space between header and footer.

The parent has to have a fixed width and height. The content DIV has to fill all available space between header and footer. When the content size of the content DIV exceeds the space between header and footer, the content DIV should display scrollbars and allow appropriate scrolling so that the footer contents should never be obscured nor the footer obscure content.

Now comes the hard part: you don't know the height of the header nor footer beforehand (eg. header and footer are filled dynamically). How to position content without using JavaScript?

Example:

<div style="position : relative; width : 200px; height : 200px; background-color : #e0e0ff; overflow : hidden;">
    <div style="background-color: #80ff80; position : absolute; left : 0; right : 0; top : 0;">
    header 
    </div>
    <div style="background-color: #8080ff; overflow : auto; position : absolute;">
    content (how to position it?)
    </div>
    <div style="background-color: #ff8080; position : absolute; bottom : 0px; left :0; right : 0;">
    footer 
    </div>    
</div>

To clarify this event further - the target layout that I'm trying to achieve will be used in a business web application. The parent DIV will have a fixed, but unknown size (for instance, it will be exactly the size of the browser viewport, sizing itself along with sizing the browser window by the user). Let's call the parent DIV a "screen".
The header will contain a set of filtering controls (like textboxes, drop down lists and a "filter" button) that should wrap to the next line if there is insufficient horizontal space (so its height can change any time to accomodate line breaking). The header should always be visible and attached to the top of the "screen".
The footer will contain a set of buttons, like on a dialog window. These too can wrap to next line if there is not enough space horizontally. The footer must be attached to the bottom of the "screen" to be accessible and visible at all times.
The content will contain "screen" contents, like dialog fields etc. If there are too few fields, the rest of the content will be "blank" (in this case the footer should not begin right after the content, but still be attached to the bottom of the "screen" which is fixed size). If there are too many fields, the content DIV will provide scrollbar(s) to access the hidden controls (in this case the content DIV must not extend itself below the footer, as the scrollbar would be partially hidden).
I hope this clarifies the question a little bit further, as I have too low rep to enter comments to your repsonses.

A: 

Absolute positioning is messing you up. Try something like this:

HTML:

<div id="wrapper">
 <div id="header">
  header
 </div>
 <div id="content">
  content
 </div>
 <div id="footer">
  footer
 </div>
</div>

CSS:

#wrapper {
 width: 200px;
 height: 200px;
 overflow: visible;
 background: #e0e0ff;
}
#header {
 background: #80ff80;
}
#content {
 background: #8080ff;
}
#footer {
 background: #ff8080;
}

edit: perhaps I misunderstood, do you want everything to fit into the 200x200px box or do you want the box to increase its height to fit the content?

rpetrich
I have updated the details in the question.
qbeuek
A: 

Does the parent need to stay at a fixed height?

<div style="position : relative; width : 200px; background-color : #e0e0ff; overflow : hidden;">
<div style="float: left; clear: left; background-color: #80ff80;">
header 
</div>
<div style="float: left; clear: left; background-color: #8080ff; overflow : auto; ">
content (how to position it?)
<BR />taller
<BR />taller
<BR />taller
<BR />taller
<BR />taller
<BR />taller
<BR />taller
<BR />taller
</div>
<div style="float: left; clear: left; background-color: #ff8080;">
footer 
<BR />taller
</div>

if the height of the parent is fixed, this is the closest I'd know how to get to it offhand -- still not exactly right if those color blocks (as opposed to just text) are truly important and weren't just for illustrating the boundaries of the DIVs:

<div style="position : relative; width : 200px; height : 200px; background-color : #e0e0ff; overflow : hidden;">
<div style="float: left; clear: left; background-color: #80ff80; ">
header <BR .> taller
</div>
<div style="float: left; clear: left; background-color: #8080ff; overflow : auto; ">
content (how to position it?)<BR /> and another line
</div>
<div style="background-color: #ff8080; position : absolute; bottom : 0px; left :0; right : 0;">
footer <BR /> taller
</div>

busse
Color are important and the parent DIV has to stay at a fixed width and height. I have updated the details in the question.
qbeuek
+1  A: 

I'm going to get downmodded for this, but this sounds like a job for a table.

What you're trying to do is to set the total height of three contiguous divs as a unit, and a 1x3 table with height 100% is actually a cleaner solution.

harpo
I actually use tables probably more than I should, but I think that the argument against using tables here would mean that too much of your style (the layout) is in the HTML document and to change the layout in the future would require changing much of the "guts" of the page.
Pete
How to declare a table that accomplishes this task? Can you provide an example?
qbeuek
<table style="height:100%"><tr><td id="head" style="height:75px;"> </td></tr><tr><td id="body"> </td></tr><tr><td id="footer" style="height:75px;"> </td></tr></table>This will cause the middle row to expand and contract with the container. The header a
harpo
But that snippet sets the header and footer heights at fixed values. I need them to expand vertically along with theirs contents (see the extended description provided in the question)
qbeuek
They will expand to fit the content regardless. But if you want to ensure that the height is no more than necessary for the contents, set the height of the header and footer to 0%. You can still apply padding to ensure that they don't look "crunched."
harpo
I can't get the desired results with this table approach. The table seems to ignore the overall set height and sets it according to row heights, which is wrong (at least in IE). Also, I don't know how to make the content scrollable when too high.
qbeuek
I'm having the same problem using the table approach. I can't get scrollbars to show and the table's height which I have set to 300px seems to be getting ignored.
dgrant
A: 

Do you need to have the center div change size? If you're just trying to make sure that it appears that its background (#8080ff) appears between the header and the footer, why not just have the containing div's background be #8080ff. The header and footer background would override that, and the rest of the div's background would be correct.

John Christensen
I need the center DIV to change size.
qbeuek
+1  A: 

If you can get away with not having the main content scrollable, you might be better using the footerStickAlt method to make sure your footer stays at the bottom of the screen or the bottom of the content (if the content extends beyond the bottom of the screen).

Ian Oxley