...

View Full Version : centered horizontal navigation bar: spacing/layout issues



Dan_55
02-11-2009, 08:46 PM
Hi everyone,
I have some html experience, but am very new to css. I am trying to design a horizontal navigation bar with four menu items that are evenly distributed and centered across the website's 800pixel width. I started with a navigation bar template that I found online. Basically I set up an 800 pixel wide container for the website that centers everything and set the width of the navigation list to 100%. I removed all padding, margins, and borders and set the menu items to have a width of 200 pixels each (hoping that would evenly distribute all the items across the entire width of the navigation bar). Instead, my fourth menu item is pushed to the next line for some reason even though dividing the 800 pixel wide navigation bar into 200 pixel areas should have given me 4 perfectly spaced menu items:confused: I experimented with changing the width to 150 pixels and everything stays on the same line, unfortunately there is a visible gap on the right side of the navigation bar. I have looked all over for a website that explains how to make an evenly distributed navigation bar like this, but so far I have been unsuccessful. Does anyone see a problem with my code?

HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>

<head>
<title>New Page 1</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
</head>

<body>

<div id="container">

<div id="navcontainer">
<ul id="navlist">
<li><a href="#">Home</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</div>

</div>

</body>

</html>

CSS:
#container {
width:800px;
margin:0 auto;
}

#navcontainer ul {
padding-left: 0;
margin-left: 0;
background-color: #036;
font-size:1.1em;
color: #fff;
float: left;
width:100%;
font-family: arial, helvetica, sans-serif;
text-align:center;
}

#navcontainer ul li {
display: inline;
}

#navcontainer ul li a {
width:200px;
padding: 0.2em;
background-color: #036;
color: #fff;
text-decoration: none;
float:left;
}

#navcontainer ul li a:hover {
background-color:#c0c0c0;
color: #fff;
}


Thanks,
Dan

jlhaslip
02-11-2009, 08:49 PM
http://www.cssplay.co.uk/menus/centered.html try those

jerry62704
02-11-2009, 09:57 PM
When you made the li inline, you lost the ability to have a width. Make them float left with a fixed width (800 / 4 = 200px or better yet 25%) instead.

Excavator
02-12-2009, 03:47 AM
Hello Dan_55,
Your li a has some padding that's adding to the width:
#navlist li a {
width: 200px;
padding: 0.2em;
background: #036;

See this page about the box model (http://www.w3.org/TR/CSS2/box.html).

Try your menu like 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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
html, body {
background: #FC6;
font: 1.1em arial, helvetica, sans-serif;
color: #fff;
text-align:center;
}
* {
margin: 0;
padding: 0;
border: none;
}
#container {
width: 800px;
margin: 50px auto;
padding: 10px 0 200px 0;
background: #9CC;
}
ul#navlist {
background: #036;
float: left;
}
#navlist li {
display: inline;
}
#navlist li a {
width: 200px;
height: 30px;
line-height: 30px;
color: #fff;
background: #036;
text-decoration: none;
float: left;
}
#navlist li a:hover {
background-color:#c0c0c0;
color: #00f;
}
</style>
<title>New Page 1</title>
</head>
<body>
<div id="container">
<ul id="navlist">
<li><a href="#">Home</a></li>
<li><a href="#">Portfolio</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
<!--end container--></div>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum