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
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    CSS Can’t center div

    Hi, I cannot get the menu to center, the css is:

    #topmenu{position:relative;width:100%; margin:0 auto;background-image:url(../gifs/menu.gif);height:30px}

    #menu{position:relative;top:0;margin:0 auto;width:100%;
    }
    #menu,#menu ul{padding:3px;margin:0 auto;list-style:none;z-index:200}

    #menu li{float:left;z-index:200}

    #menu a{display:block;padding:4px;margin-right:40px;color:#FFF;font-size:14px;font-weight:bold;text-decoration:none}
    #menu a:hover{color:#7fffd4}

    #menu li ul{position:absolute;width:13em;left:-999em;background-color:#6495ed;list-style-type:none}

    #menu li:hover ul,#menu li ul:hover{left:auto}

    the code is <div id="topmenu">

    <ul id="menu">

    <li>

    please help thanks

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,166
    Thanks
    23
    Thanked 601 Times in 600 Posts
    Stop using position:absolute or position:relative; until you understand what they do. z-index only works on position:absolute or position:relative; position:fixed;
    To center something you need to have a width on the container and the item being centered. Don't need :hover for centering.

    Remove the comments before running:
    Code:
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta charset="UTF-8" />
    <title>Title of the document</title>
    <style type="text/css">
    #topmenu{
    	width:100%; //AT 100% YOU CAN'T CENTER IT BECAUSE ITS ALL OVER THE PAGE.
    	height: 30px;
    	background-color: green;
    }
    #menu{
    	width:200px;
    	margin:0 auto; // HAS A WIDTH SO IT CAN BE CENTERED.
    	list-style:none;
    }
    #menu li{
    	float:left; // ONE WAY TO PLACE THESE IN A LINE INSTEAD OF VERTICAL
    	margin: 5px 5px 0 5px;  // LEARN THE SHORT CUTS TO SAVE FILE SIZE
    	color: blue;
    }
    </style>
    </head>
    
    <body>
    <div id="topmenu">
    	<ul id="menu" >
    		<li>Here</li>
    		<li>There</li>
    	</ul>
    </div>
    </body>
    </html>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New to the CF scene
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You have the menu spread across the page!

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,166
    Thanks
    23
    Thanked 601 Times in 600 Posts
    @Crooky, And tour point is...?
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."


  •  

    Posting Permissions

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