Center A Horizontal Css Menu
Solution 1:
You can center the navigation bar by using the following CSS rules:
nav {
margin: 0 auto;
text-align: center;
border:1px solid black;
}
navulul {
display: none;
}
navulli:hover > ul {
display: block;
}
navul {
list-style: none;
margin: 0; /* << add this */padding: 0; /* << add this */display: inline-block; /* << add this */vertical-align: top; /* << add this */
}
navulli {
float: left;
margin: 0; /* << add this */padding: 0; /* << add this */
}
navulli:hovera {
color: #000000;
}
navullia {
display: block;
padding: 10px15px;
color: #000000;
text-decoration: none;
background-color: pink; /* optional... */
}
navulul {
border-radius: 0px;
padding: 0;
position: absolute;
}
navululli {
float: none;
border-top: 1px solid #000000;
border-bottom: 1px solid #000000;
position: relative;
}
navulullia {
color: #000000;
}
navulullia:hover {
color: #666666;
}
navululul {
position: absolute;
top:0;
}
See demo at: http://jsfiddle.net/audetwebdesign/DP6Ax/
The key is to set display: inline-block
for nav ul
, which will allow your text-align: center
rule to take effect.
Make sure to zero out margins and paddings on the ul
and li
elements. Everything else that you did was more or less right, so you should be good.
Solution 2:
Instead of floating the li
, you can display them as inline-block
s.
Then, they will be centered relatively to the ul
because of text-align: center
.
Since the ul
is as wide as the nav
by default, the li
will look like centered relatively to the nav
.
nav {
text-align: center;
border: 1px solid black;
}
navul {
list-style: none;
padding: 0;
margin: 0;
}
nav > ul > li {
display: inline-block;
}
nava {
display: block;
padding: 10px15px;
color: #000000;
text-decoration: none;
}
navli:hover > ul {
display: block;
}
nav > ulul {
display: none;
position: absolute;
}
nav > ulul > li {
border-bottom: 1px solid #000000;
}
nav > ulula:hover {
color: #666666;
}
<nav><ul><li><ahref="/add_contact.php">Add Contact</a></li><li><ahref="/view_contact.php">View Contact</a></li><li><ahref="/tickets.php">Tickets</a><ul><li><a>TEST1</a></li><li><a>TEST2</a></li></ul></li><li><ahref="/invoices.php">Invoices</a></li><li><ahref="/itemised_calls.php">Itemised Calls</a></li></ul></nav>
Solution 3:
First, when you float the ul
's you have to clear the float by adding clear
div:
HTML :
<divclass="clear"></div>
CSS :
.clear{
clear:both;
}
And for centring the menu you should specify a width of the ul
as in example and randomly I have set the width to 560px
:
navul {
list-style: none;
width : 560px;
margin: 0 auto;
}
Take a Look:
Post a Comment for "Center A Horizontal Css Menu"