Forum Moderators: not2easy

Message Too Old, No Replies

Make menu bar stretch the length of the page

         

cyprex

2:43 pm on Apr 28, 2009 (gmt 0)

10+ Year Member



Hi. I'm trying to make my navigation bar run the entire way down the left hand side of the page. The bar only contains 4 or 5 links, but I want it to stretch down as far as the main content to fill the height of the page.

I've tried the min-height=100%, but since the content within the navbar has already been covered, the bar only stretches as long as the height of the browser. then stops. Any suggestions?

jadexsoln

3:42 pm on Apr 28, 2009 (gmt 0)

10+ Year Member



Are you floating the nav bar by any chance, if so the floated element has no reference to what 100% is other than it's own content?

CSS_Kidd

3:43 pm on Apr 28, 2009 (gmt 0)

10+ Year Member



Ok so there are a few solutions that I am sure that someone will post here soon. However after dealing with the 100% issues in different browsers I gave up and cheated.

I just create a background for the body (usually different color with the width of the menu). Repeat y and problem solved.

Yeah it isn't as cool as trying to get it to work just right with CSS and 100% heights, but gosh darn it gets the job done.

cyprex

3:48 pm on Apr 28, 2009 (gmt 0)

10+ Year Member



Here is my css for the side bar right now. It goes 100% of the page, then there is a gap while the rest of the main content continues.

#navbar {
position: absolute;
left: 0px;
top: 100px;
min-width:11em;
padding-top:5px;
background-color: #bfc789;
min-height: 100%;
}
html, body {
height: 100%;
}

swa66

6:25 pm on Apr 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



100% height often is misunderstood.

the height:100% takes the height of the direct parent if it is explicitly set and different from auto.

Only the root element is an exception in that is has the height of the viewport.

Hence the behavior is expected.

Now since you use absolute positioning you have some extra means:
you can set both top:0 and bottom:0: it'll give it the same height as it's reference. That reference is the closest parent that has gained position. The easiest to make a box gain position is to set position:relative on it.

IE6 will not be happy about setting both top and bottom and not specifying the height, but it can be taught with IE7.js .

CSS_Kidd

6:37 pm on Apr 28, 2009 (gmt 0)

10+ Year Member



Here is a snip-it of my 100% code that does work. I changed my sizes and color to match yours.

body, html {
height:100%;
}
#navbar {
min-width:11em;
background-color: #bfc789;
min-height: 100%;
zoom:1; /* ie fix */
position:relative;
}

Do not use top or bottom margins or padding and no absolute positioning on the div you want to be 100%. This will cause gaps and and scrolling. Remember % is the % of the users screen resolution. It works like this: If you have it set up to be 100% in height add top padding of 50px it's saying that the content is going to be 100% plus 50px.

If you need the positioning to be just right add a div inside the 100% div and give that one the padding and positioning that is needed.

Try the above and see how that works for you.

[edited by: CSS_Kidd at 6:39 pm (utc) on April 28, 2009]

cyprex

4:44 am on Apr 29, 2009 (gmt 0)

10+ Year Member



hmm...although good ideas, none of these solutions have worked. basically I need to set the height of the side bar to the length of the body.

swa66

8:34 am on Apr 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What I meant is this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>untitled</title>
<style type="text/css">
* {
margin:0;
padding:0;
}
html {
height: 100%;
background-color: yellow;
}
body {
min-height: 100%;
width: 770px;
background-color: orange;
margin: 0 auto;
position: relative;
padding-left: 200px;
}
.menu {
position: absolute;
top:0;
bottom:0;
left: 0;
width: 180px;
background-color:red;
list-style: none;
}
</style>
</head>
<body>
<ul class="menu">
<li><a href="#">one</a></li>
<li><a href="#">two</a></li>
</ul>
<p>Very long text goes here, make sure it is longer than
the viewport is tall</p>
</body>
</html>

I did center the body to add some challenge to it, if not needed: remove the width and the auto margins on the body.

How does it work:

Removing default margins and paddings (body has some of them in most browsers and they will interfere here)

html get the height of the root element (the height of the viewport)

body gets the minimal height of its direct parent (html) which does have an explicitly set height.
The relative positioning makes body gain "position": absolute children will reference the body, not the viewport anymore.
The padding is to make sure it'll not have stuff under the menu.

.menu: we do not et a height, but let the browser calculate it by using absolute positioning and setting both the top and bottom to be at the same spot as the top and bottom of the element it references (body). Essentially this makes it as tall as the body.

When will this fail ?
- in a browser that doesn't allow top and bottom to be set such as IE6 (can be fixed with IE7.js)
- if the menu would get longer than the main content and the viewport would be too small to show the menu (so it is not a generic solution for a 2 col layout)

I only tested it in FF, safari and opera, didn't bother with IE.

kingdomkid

1:35 am on Jan 29, 2010 (gmt 0)

10+ Year Member



okay, forgive me if I seem like a moron, but I'm having the same problem...unlike cyprex, I am not using position: absolute; but instead, I am floating the navigation menu to the left and the body to the right. The body height spans the length of the page, but the height of the nav menu only seems to go as far as the content inside of it. How to I get it so that both of them are the same height at all times?