Go Back   CodingForums.com > :: Client side development > HTML & CSS

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-11-2009, 08:46 PM   PM User | #1
Dan_55
New to the CF scene

 
Join Date: Feb 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Dan_55 is an unknown quantity at this point
centered horizontal navigation bar: spacing/layout issues

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 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
Dan_55 is offline   Reply With Quote
Old 02-11-2009, 08:49 PM   PM User | #2
jlhaslip
Regular Coder

 
Join Date: Feb 2007
Location: Canada
Posts: 924
Thanks: 10
Thanked 56 Times in 55 Posts
jlhaslip is on a distinguished road
http://www.cssplay.co.uk/menus/centered.html try those
jlhaslip is offline   Reply With Quote
Old 02-11-2009, 09:57 PM   PM User | #3
jerry62704
Senior Coder

 
jerry62704's Avatar
 
Join Date: Oct 2007
Location: Springfield, IL
Posts: 1,042
Thanks: 9
Thanked 81 Times in 81 Posts
jerry62704 is on a distinguished road
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.
__________________
.
.
...and gladly would he learn and gladly teach

Visit www.LiberalsWin.com for humor and the unique Bush/Obama Approval Polls
jerry62704 is offline   Reply With Quote
Old 02-12-2009, 03:47 AM   PM User | #4
Excavator
Master Coder


 
Excavator's Avatar
 
Join Date: Dec 2006
Location: Alaska
Posts: 9,410
Thanks: 22
Thanked 1,765 Times in 1,749 Posts
Excavator has a spectacular aura aboutExcavator has a spectacular aura aboutExcavator has a spectacular aura about
Hello Dan_55,
Your li a has some padding that's adding to the width:
Code:
#navlist li a {
	width: 200px;
	padding: 0.2em;
	background: #036;
See this page about the box model.

Try your menu like this:
Code:
<!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>
__________________
Validate often DURING development - Use it like a splelchecker | Debug during Development |Write it for FireFox, ignore IE
Use the right DocType | Validate your markup | Validate your CSS | Why validating is good | Why tables are bad
Excavator is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:16 AM.


Advertisement
Log in to turn off these ads.