I need to center align a horizontal menu.
I've tried various solutions, including the mix of inline-block
/ block
/ center-align
etc., but haven't succeeded.
Here is my code:
<div class="topmenu-design">
<!-- Top menu content: START -->
<ul id="topmenu firstlevel">
<li class="firstli" id="node_id_64"><div><a href="#"><span>Om kampanjen</span></a></div></li>
<li id="node_id_65"><div><a href="#"><span>Fakta om inneklima</span></a></div></li>
<li class="lastli" id="node_id_66"><div><a href="#"><span>Statistikk</span></a></div></li>
</ul>
<!-- Top menu content: END -->
</div>
UPDATE
I know how to center align the ul
within the div
. That can be accomplished using Sarfraz's suggestion.
But the list items are still floated left within the ul
.
Do I need Javascript to accomplish this?
Generally speaking the way to center a black level element (like a
<ul>
) is using themargin:auto;
property.To align text and inline level elements within a block level element use
text-align:center;
. So all together something like...... should work.
The fringe case is Internet Explorer6... or even other IEs when not using a
<!DOCTYPE>
. IE6 incorrectly aligns block level elemnts usingtext-align
. So if you're looking to support IE6 (or not using a<!DOCTYPE>
) your full solution is...As a footnote, I think
id="topmenu firstlevel"
is invalid as anid
attribute can't contain spaces... ? Indeed the w3c recommendation defines theid
attribute as a 'name' type...That all!
Like so many of you, I've been struggling with this for a while. The solution ultimately had to do with the div containing the UL. All suggestions on altering padding, width, etc. of the UL had no effect, but the following did.
It's all about the
margin:0 auto;
on the containing div. I hope this helps some people, and thanks to everyone else who already suggested this in combination with other things.i use jquery code for this. (Alternative solution)
Is a decent approximation on big screens. Its not good, but a good dirty fix.
Try this: