Centering Horizontal Menu with Even Widths in Fixed-Size Container

I have this menu:
<div id=“mainmenu”>
<li>Menu 1</li>
<li>Menu 2</li>
<li>Menu 3</li>
<li>Menu 4</li>
<li>Menu 5</li>
<li>Menu 6</li>

inside a container (width of 957px). What I’m trying to do is not only center the menu within the container but also trying to get the menu to fill the width of the container. I can’t seem to figure out how to do that without the menu flowing OUTSIDE or BEYOND the container width.

Also, I have it set to absolute because my next layer will be a rather large image behind the menu.


.mainmenu {
	position: absolute;
	margin-top: 7px;
	top: 0px;
	left: 0px;
 line-height: normal;
	z-index: 1000;

.mainmenu ul {
	margin: 0px;
	padding: 0px;
	text-align: center;
	font: normal 14px Georgia, "Times New Roman", Times, serif;
	background: rgba(0,0,0,.5);

.mainmenu li {
margin: 0px;
display: inline;
list-style-type: none;
padding: 10px 0px;
border-left: 1px solid #737476;

.mainmenu li.first-child {
	border-left: none;

.mainmenu li a, .mainmenu li a:link, .mainmenu li a:active, .mainmenu li a:visited {
color: #FFF;
padding: 10px 55px;
text-decoration: none;
text-transform: capitalize;

.mainmenu li a:hover {
	color: #000;
	background: #FFF;

Thank you!

Give the div the background and the ul the fixed width and margin auto.

Here are all the ways to do it.

Edit- Depending, you’ll prob want to remove that position absolute on the div too.

This is what I need it to look like


You can’t really do it automatically (not without a lot of trickery). You could use display:table and display:table-cell properties to make it fit automatically but they are not supported in ie6 and 7.

I would just float each one left and give them widths to fit exactly.

Thank Paul O’B’.

I wound up setting an em width on the line items and adding a special padding-right (just a tad) to the last line item since it wasn’t filling up to the full width. Looks good!

Thank you!