Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #2
    Regular Coder
    Join Date
    Feb 2007
    Location
    Canada
    Posts
    924
    Thanks
    10
    Thanked 56 Times in 55 Posts

  • #3
    Senior Coder jerry62704's Avatar
    Join Date
    Oct 2007
    Location
    Springfield, IL
    Posts
    1,100
    Thanks
    13
    Thanked 87 Times in 87 Posts
    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

  • #4
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,748
    Thanks
    22
    Thanked 1,838 Times in 1,822 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •