I have a simple menu bar inside a div and I can center the div but I want to center the menu bar inside the div.
This is the code:
CSS:
Could very well be that the width you're trying to set is too big? http://jsfiddle.net/As98Q/
width: 550px;
First off all, make sure the div is wider that the menu bar. You can try
display:inline
or if not
text-align:center;
for the div
First of all, please note that you should put the elements inside the <body>
tag.
Actually, The ul
element is aligned at the center (See demo here). But the point is that the list items are floated to the left.
There are tow options:
Using a proper fixed width (or max-width as you did) for the <ul>
element. Working Demo
Or use display: inline-block
instead of float: left
for the list items, and use text-align: center
for the ul
to align the inline elements horizontally. Working Demo.
But note that there is a white space between inline(-block) elements, which absolutely the normal behavior of inline elements in inline flow. You can refer my answer here to fix that.