i am trying to center my css navigation bar but cant figure out why is not working, where am i doing wrong. i want it in the top center of the page. i tried margin:0 auto but it wont work here is my code:
<style>
ul {
list-style-type: none;
padding: 0;
overflow:hidden;
}
a:link,a:visited {
margin:0 auto;
display:block;
width: 120px;
font-weight:bold;
color:#FFFFFF;
text-align:center;
padding:4px;
text-decoration:none;
text-transform:uppercase;
}
a:hover, a:active {
background-color:#7A991A;
}
li {
float: left;
}
#menu {
background-color:#98bf21;
}
</style>
<div id="menu">
<header>
<ul>
<li><a href="Home.aspx">Home</a></li>
<li><a href="News.aspx">News</a></li>
<li><a href="Articles.aspx">Articles</a></li>
<li><a href="Forum.aspx">Forum</a></li>
<li><a href="Contact.aspx">Contact</a></li>
<li><a href="About.aspx">About</a></li>
</ul>
</header>
</div>
Change your last two CSS rules to:
jsFiddle example
Add the following css:
This will make the ul fit its contents and then be centered in its container.
Use the
inline-block
css magic :)JSFiddle
I had the same problem until I read this post and decided to center the text in the "nav".
So basically your
ul
should be in anav
so you cantext-align: center
the nav area.