skip to content

Centering Float Left Menus

a centered floated left menu (known width)

and....

a centered floated left menu (unknown width)


Information

I have had a few people ask if they could center a floated left menu when the width of the items in the menu is not known, and I had thought that it was not possible.

Where the width is known it is a simple matter of specifying a total width for the <ul> tag (block level) and using margin:0 auto; for non IE browsers and text-align:center; on an outer div for IE. This is demonstrated in the first example above where the overall width is 408px (400px for the list items and 8px for the borders).

However, upon further investigation, I find that it is possible in IE5.5, IE6, Firefox, Mozilla, NN, Opera and Safari to do this using 'non-standard' but validating techniques.

For Internet Explorer

What we need to do is enclose the ul tag in an outer container that will take on the width of the <ul> and then center this outer container.

After a little trial and error I have found that IE can be made to work using an outer <del> tag styled using display:inline-block; and then using text-align:center; in an outer div. To get rid of the strikethrough that is produced using <del> use text-decoration:none;

For other browsers

For Firefox, Mozilla, NN, Opera and Safari we need to use a different method of producing our horizontal unordered list. One that doesn't use 'float:left' or 'display:inline'. Again we have a validating CSS answer in 'display:table/table-cell' and this is how the example above is produced.

With the <ul> tag styled as a 'table' we can easily center it using margin:0 auto;

So, as usual, we are just left with this not working in Mac IE5.

Cascading Style Sheet

#menu1 {
  width:408px;
  padding:0; 
  margin:0 auto; 
  list-style-type:none;
  }
#menu1 li {
  float:left;
  width:100px;
  border:1px solid #fff; 
  }
#menu1 a {
  display:block;
  width:100px;
  color:#000; 
  background:#d4d4d4; 
  text-align:center; 
  padding:4px 0; 
  text-decoration:none; 
  float:left; 
  }
#menu1 a:hover {
  color:#fff; 
  background:#08c;
  }


#menu2 {
  display:table;
  padding:0; 
  margin:0 auto; 
  list-style-type:none;
  white-space:nowrap;
  }
#menu2 li {
  display:table-cell;
  }
* html #menu2 li {
  float:left;
  }
#menu2 a {
  width:auto;
  display:block;
  padding:4px 16px;
  color:#fff; 
  background:#08c; 
  border:1px solid #fff; 
  text-decoration:none;
  }
* html #menu2 a {
  float:left;
  }
#menu2 a:hover {
  color:#000; 
  background:#d4d4d4;
  }

.container {clear:both; text-decoration:none;}
* html .container {display:inline-block;}

xhtml

<ul id="menu1">
<li><a href="#nogo">Tab One</a></li>
<li><a href="#nogo">Tab Two</a></li>
<li><a href="#nogo">Tab Three</a></li>
<li><a href="#nogo">Tab Four</a></li>
</ul>

<del class="container">
<ul id="menu2">
<li><a href="#nogo">Tab One</a></li>
<li><a href="#nogo">Tab Two: Longer</a></li>
<li><a href="#nogo">Tab Three: Longest</a></li>
<li><a href="#nogo">Tab Four</a></li>
</ul>
</del>

Copyright

You may use this method on your personal 'non-profit' web site without seeking my permission. A link back to CSSplay is always appreciated.

Commercial usage is also permitted without seeking approval, but I would ask that a donation is considered to support my work on CSSPlay.

If you are having problems integrating any of my demonstrations into your website then I now offer a service to fault find and correct any errors that you may have introduced. Please email me for more information.




Recommended Sites

CSS play pages



CSS play recommend

Free, practical CSS menus, layouts, and examplesQuality UK Web HostingSitegrinderDHTML menu & Javascript menu creator - Powerful web menu solutionsSocial Media Applications On DemandPSD to HTML