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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    153
    Thanks
    53
    Thanked 0 Times in 0 Posts

    css horizontal menu

    Hello, I'm trying to make a horizontal css menu, but I'm having trouble making it align horizontally. Here is the code:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
    <style type="text/css">
    #bar
    {
    width: 100%;
    background: #333;
    height: 35px;
    }
    
    #top
    {
    width: 1000px;
    margin: 0 auto;
    }
    #bar
    {
    list-style: none;
    margin: 0;
    text-align: center;
    }
    
    #bar li
    {
    list-style: none;
    margin: 0;
    background: #333;
    }
    #bar a
    {
    color: #ccc;
    text-decoration: none;
    height: 35px;
    padding: 0 25px;
    display: block;
    }
    #bar a:hover
    {
    color: #fff;
    background: #355999;
    text-decoration: none;
    }
    </style>
    
    </head>
    
    <body>
    <div id="bar">
    	<div id="top">
    		<ul id="bar">
    		<li><a href="index.php">Home</a></li>
    		<li><a href="index.php">Home</a></li>
    		<li><a href="index.php">Home</a></li>
    		<li><a href="index.php">Home</a></li>
    		</ul>
    	</div>
    </div>
    </body>
    </html>
    Can someone guide me please?
    Thanks for your time.

  • #2
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    Can't use the same id twice, Mr. Win.
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^

  • #3
    Regular Coder
    Join Date
    Jan 2010
    Posts
    153
    Thanks
    53
    Thanked 0 Times in 0 Posts
    I changed the first div to something else, but I'm still getting the same results.

  • #4
    Senior Coder
    Join Date
    Aug 2010
    Location
    High Point, NC
    Posts
    3,338
    Thanks
    5
    Thanked 363 Times in 360 Posts
    Did you float the li element left?

  • Users who have thanked teedoff for this post:

    wincode (11-14-2010)

  • #5
    Regular Coder
    Join Date
    Jan 2010
    Posts
    153
    Thanks
    53
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by teedoff View Post
    Did you float the li element left?
    That worked, but the menu is now aligned to the left (I want to center it).
    How exactly does the float work to put the list horizontally?

    Thanks

  • #6
    Master Coder Excavator's Avatar
    Join Date
    Dec 2006
    Location
    Alaska
    Posts
    9,675
    Thanks
    22
    Thanked 1,827 Times in 1,811 Posts
    Hello wincode,
    Looks like you have a little divitis too. Are all those containing elements really needed for a few menu buttons? The ul is already containing them and, if there is no layout specific reason for more containers then you can do away with them.
    Have a go with something 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" />
    <title>Untitled Document</title>
    <style type="text/css">
    html, body {
    	background: #FC6;
    	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    	font-size: 100%;
    }
    #page {
    	height: 600px; /*for demonstration only*/
    	width: 800px;
    	margin: 30px auto;
    	background: #999;
    	font-size: 0.8em;
    }
    ul#bar {
    	list-style: none;
    	padding: 0;
    	text-align: center;
    }
    ul#bar li {
    	width: 200px;
    	float: left;
    	text-align: center;
    	line-height: 35px;
    	background: #333;
    }
    ul#bar li a {
    	color: #ccc;
    	display: block;
    }
    ul#bar li a:hover {color: #fff;}
    </style>
    </head>
    <body>
        <div id="page">
            <ul id="bar">
                <li><a href="index.php">Home</a></li>
                <li><a href="index.php">Home</a></li>
                <li><a href="index.php">Home</a></li>
                <li><a href="index.php">Home</a></li>
            </ul>
        <!--end page--></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

  • Users who have thanked Excavator for this post:

    wincode (11-14-2010)

  • #7
    Senior Coder
    Join Date
    Aug 2010
    Location
    High Point, NC
    Posts
    3,338
    Thanks
    5
    Thanked 363 Times in 360 Posts
    You should do some reading about floats and divs...w3schools.com is a great place to start.

  • #8
    Regular Coder
    Join Date
    Jan 2010
    Posts
    153
    Thanks
    53
    Thanked 0 Times in 0 Posts
    Thanks I've got it to work, just need to practice now

  • #9
    Regular Coder
    Join Date
    Oct 2010
    Location
    Florence, MS
    Posts
    486
    Thanks
    10
    Thanked 33 Times in 32 Posts
    Don't forget to placed this as resolved. See steps below.

    Steps:
    edit opening post>go advanced>set the prefix before the title as resolved
    Notice: If you post a problem and it gets fixed, please remember to go back and place it as solved. ;)
    I always recommend the HEAD First series of books for learning a new coding language. ^_^


  •  

    Posting Permissions

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