HTML / CSS centered menu
2010-01-16 @ 15:00Create a CSS menu where LI elements are centered
I found out that it wasn’t that easy to create a CSS menu with the LI elements in the center.
The first way I solved it was by float the LI-elements and change the UL element to display:table or display:inline. That worked, but not for Internet Explorer. With a little help I got it working with Internet Explorer as well.
How it looks like
No preview at the moment.
The solution
The UL element is set to text-align:center and the LI elements is set to display:inline. No floating was required. Thanks Paolo Bergantino at stackoverflow.com who helped me on this one.
I choose to make the stylesheet within the same page to make it easier to test.
HTML
<div id="menu-centered"> <ul> <li><a href="">My first item</a></li> <li><a href="">My second item</a></li> <li><a href="">My third item</a></li> <li><a href="">My fourth item</a></li> <li><a href="">My fifth item</a></li> </ul> </div>
CSS
<style type="text/css" media="screen"> body { margin: 0; } #menu-centered { background: #0075B8; padding: 10px; margin-bottom: 10px; } #menu-centered ul { margin: 0; padding: 0; text-align: center; } #menu-centered li { display: inline; list-style: none; padding: 10px 5px 10px 5px; } #menu-centered a { font: normal 12px/24px Arial; color: #fff; text-decoration: none; padding: 5px; background: #57a8d6; } #menu-centered a:hover { background: #5fb8eb; } </style>
2009-08-28 @ 12:28 e m
Cheers for the backlink, glad it helped.
2009-09-14 @ 2:22 e m
thanks, it’s very helpful
2010-10-08 @ 8:09 f m
simple and easy to understand. thanks!
2011-06-17 @ 9:19 e m
Simple and very useful. Thank’s Paolo
2011-08-21 @ 4:59 e m
Thanks, very useful and easy to learn. Works good
2011-11-11 @ 8:45 f m
How to fix if I not use CSS to get it center?
I use for table, so I can’t integrate it with CSS.
Thank you
2011-11-23 @ 11:40 f m
how do I get rid of the border on the right? the background shows up and seems to appear kludgy? please advise.
2012-01-24 @ 12:03 f m
What happens if I have a block element inside the tag ”a”?
span {
display: block;
}
Adulto
2012-09-27 @ 1:35 e m
wow thanks, its really awesome… i just apply this code and solved my problem… great… :-)
2012-10-12 @ 5:14 e m
this helps alot gonna try it at home when I get off tonight. I really hope this won’t screw it all up. i do have one question. when I hover on one of my links its supposed to have a drop down menu that appears to have gone array. i’m not home so im going to integrate this with my eportfolio website and see if it works there.
2013-01-03 @ 4:28 e m
Thank you :) u can solve my problem
2013-01-24 @ 5:53 e m
Thanks! Thanks!
2013-02-01 @ 10:39 f m
Many thanks. It worked straight out of the box. I used it in a Word Press 2012 child theme and it’s fine on PC and tablet. Needs some work for mobile compatibility. Great simple piece of code thanks….
2013-02-02 @ 10:01 f m
thanks boss.its really working
2013-02-09 @ 9:01 e m
Thaks, This tricks is working fine for me!