Bryan
Bryan

Reputation: 8697

unable to align navbar to center

I have searched lots of question in this forum and it doesnt seems to help. I'm trying to center my navigation bar however despite following most of the answers text-align = center; doesn't work for me.

HTML code in ASP.NET:

<html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
        <title>untitled</title>
        <link rel="stylesheet" href="css/style.css" type="text/css" />

        <!--[if lt IE 8]>
            <script src ="http://ie7-js.googlecode.com/svn/version/2.1(beta2)/IE8.js"></script>
        <![endif]-->    
    </head>

    <body>
        <ul id="nav">
            <li><a href="#">Home</a></li>
            <li><a href="#">Login</a></li>
            <li><a href="#">Report</a></li>
            <li><a href="#">Recent cases</a></li>   
            <li><a href="#">About</a></li>      
        </ul>

        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js" type="text/javascript" charset="utf-8"></script>    
        <script src="http://jquery-ui.googlecode.com/svn/tags/latest/ui/jquery.effects.core.js" type="text/javascript"></script>
        <script type="text/javascript" src="js/scripts.js"></script>
    </body>
</html> 

CSS Code:

body {
 font-family: helvetica, arial, sans-serif;
 background: #e3e3e3;
 text-align: center;
}

/* MENU */

#nav {
text-align: center;
 background: #e5e5e5;
 float: left;
 margin: 0; padding: 0;
 border: 1px solid white;
 border-bottom: none;
}

#nav li a, #nav li {
    text-align: center;
 float: left;
}

#nav li {
text-align: center;
 list-style: none;
 position: relative;
}

#nav li a {
text-align: center;
 padding: 1em 2em;
 text-decoration: none;
 color: white;
 background: #292929;
 background: -webkit-gradient(linear, left top, left 25, from(black), color-stop(4%, #3c3c3c), to(#292929));
 border-right: 1px solid #3c3c3c;
 border-left: 1px solid #292929;
 border-bottom: 1px solid #232323;
 border-top: 1px solid #545454;
}

#nav li a:hover {
 background: #2a0d65;
 background: -moz-linear-gradient(top, #11032e, #2a0d65);
 background: -webkit-gradient(linear, left top, left bottom, from(#11032e), to(#2a0d65));
}


/* Submenu */

.hasChildren {
position: absolute;
width: 5px; height: 5px;
background: black;
right : 0;
bottom: 0;
}    

#nav li ul {
 display: none;
 position: absolute;
 left: 0;
 top: 100%;
 padding: 0; margin: 0;
}

#nav li:hover > ul {
 display: block;
}

#nav li ul li, #nav li ul li a {
 float: none;
}

#nav li ul li {
 _display: inline; /* for IE6 */
}

#nav li ul li a {
 width: 150px;
 display: block;
}

/* SUBSUB Menu */

#nav li ul li ul {
 display: none;
}

#nav li ul li:hover ul {
 left: 100%;
 top: 0;
}


#nav li ul 

Upvotes: 0

Views: 7550

Answers (5)

vusan
vusan

Reputation: 5331

Try margin:auto with exact width and remove float:left jsfiddle link

At first float:left is needed to calculate exact width of ul. And then we need to make float:none to make use of margin:auto;

Upvotes: 0

Saucier
Saucier

Reputation: 4360

Indeed the most easiest way is to use text-align: center;. The problem you encounter is that <li> tags are block level elements. Thus it is not possible to apply text-align (applies only on inline elements) on the outer <ul> element in order to center the inner <li> elements. Therefore you have to tell the <li> tags first to behave like inline elements by applying the display property:

Markup:

<ul>
    <li>Item 1</li>
    <li>Item 2</li>
    <li>Item 3</li>
    <li>Item 4</li>    
</ul>

Style:

ul {
    margin: 0;
    padding: 0;
    text-align: center;
}

ul li {
    display: inline-block;  // or display: inline;
}

Here is my working example on jsfiddle: http://jsfiddle.net/7MKdk/

Upvotes: 2

midstack
midstack

Reputation: 2123

Try This:

#nav{
    position:absolute;
    left:30%;
}

you can remove float

Upvotes: 0

kireirina
kireirina

Reputation: 163

Is this the part you mean?

#nav li a, #nav li {
    text-align: center;
    float: left;
}

By the way, try removing the float: left part. Your code almost drove me crazy. You should add IDs and classes.

Upvotes: 0

Sree ram
Sree ram

Reputation: 379

Try this:

#nav{
margin-left:10px; (Increase the px until the nav reach center)
}

Upvotes: -1

Related Questions