Centering Float Left Menus
24th January 2009
a centered floated left menu (known width)
a centered floated left menu (unknown width)
copyright © stu nicholls - CSS play
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.
Revisited 24th January
Revisiting this to show an easier method that is cross browser friendly. Thanks to Tristan Brotherton for the idea.
For most single level menus this method will work in all the major browsers and doesn't use hack, inline-blocks or display table/table-cell.
All we need to do is enclose the ul tag in an outer container that has a width of 100% and overflow set to hidden.
The <ul> tag is then styled with a relative position and floated left with a left position of 50%.
Finally the <li> tag is also styled with a relative position, floated left but this time with a right position of 50%.
...and that as they say is all that is needed.
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.