Forum Moderators: open

Message Too Old, No Replies

Multiple Divs at 100% height

Trying to get 100% of two divs within another div

         

jonreynolds

12:19 pm on Apr 21, 2008 (gmt 0)

10+ Year Member



Hello All.

This is my very first post on here, so I hope I post in the appropriate manner.

I have been interested in web design writing in html for a while on and off. I do not try to be an expert by any means...more of a hobbiest or enthusiast, just interested to see what I can do and learn. So forums like this are invaluable for me.

OK onto the problem...

I am trying to make a very simple layout. Basically a title div, which spans the whole page (or in this case about 80% of the page; its within a 'whole body div' to contain everything). Then below that, two perfectly aligned (which gaps) divs, one for the navbar and one for content. Then below that, once the content has finished, a footer div, which basically mirrors the title div in construction.

Everything so far works well (In IE at this precise moment - can't check FF until I get home) The only problem I have is that I can't seem to control the height of the two divs 'navbar' and 'contents'.

At first the layout as shown below, shows the navbar and contents to be the same height, but off the bottom of the page, even though they are mostly empty.

1) How do I make these two divs remain same height and only as big as they need to be? i.e. if there is only a few lines of content, then only be high enough to cover that...then end and go to footer.

Next, if you uncomment out the extra line in the 'contents' div, this will add many extra lines of text which will push the height of the contents div up, but the navbar does not grow with it.

2) How do I make the navbar and contents div both stay the same height? i.e. which ever has the greatest content in it dictates the height of both...and of course maintains the 10px margin to the footer div.

Hope you can help! sorry for the long post.

Code following below.

Thanks for your time.

Jon.

Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>

<head>
<style type="text/css">
*
{
margin:0;
padding:0;
}

body
{
height: 100%;
}

p,h1,h2
{
padding: 0.5em 0;
}

li
{
padding: 0.15em;
margin:0;
}

div.body
{
padding: 1em;
margin: 0 auto;
width:1000px;
border:0;
height: 100%;
}

div.title
{
width: 998px;
border: 1px solid black;
padding:0.2em 0;
text-align:left;
}

div.container
{
height:100%;
}

div.navbar
{
float: left;
width:188px;
height: 100%;
text-align: left;
border: 1px solid black;
margin: 10px 0;
padding: 10px 0 10px 10px;
}

div.navbar ul
{
list-style-type: none;
margin-left:0.3em;
}

div.contents
{
float: right;
width: 778px;
height: 100%;
margin: 10px 0;
text-align: left;
border: 1px solid black;
padding: 10px 0 10px 10px;
}

div.footer
{
width: 998px;
border: 1px solid black;
padding:0.2em 0;
background-color: #dcdcdc;
}
</style>

</head>

<body>

<div class="body">

<div class="title">
Title Div
</div>

<div class="container">

<div class="navbar">
<ul>
<li>navbar
<li>navbar
<li>navbar
<li>navbar
<li>navbar
</ul>
</div>

<div class="contents">
<p> This is the Contents div. Repeat or remove lines many times to see how navbar and contents div don't stay same height</p>
<p>another line</p>
<!--uncomment out lines below to see how extended content moves the 'contents' div down along with the 'footer' div but the 'navbar' div does not follow.-->
<!--<p>another line</p><p>another line</p><p>another line</p><p>another line</p><p>another line</p>
<p>another line</p><p>another line</p><p>another line</p><p>another line</p><p>another line</p>
<p>another line</p><p>another line</p><p>another line</p><p>another line</p><p>another line</p>
<p>another line</p><p>another line</p><p>another line</p><p>another line</p><p>another line</p>
<p>another line</p><p>another line</p><p>another line</p><p>another line</p><p>another line</p>
<p>another line</p><p>another line</p><p>another line</p><p>another line</p><p>another line</p>-->
</div>

</div>

<div class="footer">
This is the Footer div.
</div>

</div>

</body>
</html>

itspook

7:21 am on Apr 22, 2008 (gmt 0)

10+ Year Member



Hi jonreynolds
your code is fine
just one mistake in it

you haven't mention float in footer div
so here is the solution

**************************************
div.footer
{
float:left;
}
**************************************

append above line and check your code
i have tested in FF, IE (6,7), Opera, NN and safari
no issue their too

jonreynolds

7:52 am on Apr 22, 2008 (gmt 0)

10+ Year Member



Hi itspook!

Sigh...makes me feel a bit silly when its such a simple fix, but hey this is how we learn.

Thank you very much for that!

...Could you explain a little why adding that left float makes both the navbar and contents div suddenly both fill full height down to the footer please? Its nice to have a simple fix,but I would like to understand why that fixed it.

Many thanks indeed!

Regards,

Jon

jonreynolds

7:54 am on Apr 22, 2008 (gmt 0)

10+ Year Member



Oh actually...sorry but I just tried uncommenting the extra lines to push the height of the contents div down and the navbar did not extend down to full height?

This was in IE6.

? hope you can help

Regards,

Jon

jonreynolds

3:23 pm on Apr 22, 2008 (gmt 0)

10+ Year Member



The more I read about this the more I find out it is probably not possible to do. One can only emmulate the effect like in SuzyUK's demo.

Ah well, I think I will just fix the height of the navbar and let the content grow and shrink as required. The footer stays attached where it should be so thats the main thing.

Regards,

Jon

itspook

12:50 pm on Apr 24, 2008 (gmt 0)

10+ Year Member



hi Jon
the reason for little fix was that
all of the DIVs in your code was float left
when you structured such kind of pages you have to specify float (left OR right) accordingly
and when you are not managing float in one of the elements of your page structure it would like a element in space, and coz of disturbing elements beneath

you can check the code again, and find float:left; in each element
if you want to study this thing in depth,
just comment float:left; element by element and check the output in various browsers,

hopefully i convey my msg regarding floating issue :S

jonreynolds

10:03 am on Apr 25, 2008 (gmt 0)

10+ Year Member



I see, thanks very much itspook!

itspook

10:51 am on Apr 25, 2008 (gmt 0)

10+ Year Member



my pleasure