I want to evenly distribute 6 navigation items across a 900px container, with equal amounts of white space between each item. Here is an example:
---| 900px Container |---
---| HOME ABOUT BASIC SERVICES SPECIALTY SERVICES OUR STAFF CONTACT US |---
Currently, the method I am using for this layout is as follows:
nav ul {
width: 900px;
margin: 0 auto;
}
nav li {
line-height: 87px;
float: left;
text-align: center;
width: 150px;
}
The main ISSUE with this approach is twofold. Firstly, it does not truly justify the items but instead spreads them evenly within the ul tag, creating uneven white-space between smaller menu items like "HOME" or "ABOUT" and larger ones like "BASIC SERVICES".
The second issue arises when a navigation item exceeds 150px in width, which is the case for SPECIALTY SERVICES - even though there is ample space available in the entire nav.
Is there a solution to this problem? I have searched extensively online for solutions, but none seem to provide a satisfying answer. Preferably looking for CSS / HTML only solutions...
Thank you!
UPDATE (7/29/13): The most efficient modern way to achieve this layout is by using table-cell. Refer to felix's response below for implementation. The table cell
property is supported by 94% of current browsers. You may need to address compatibility issues with IE7 and older versions, but overall it should work fine.
UPDATE (7/30/13): Unfortunately, there is a known webkit bug that affects this layout when combined with Media Queries. As of now, it's best to avoid changing the 'display' property. Please refer to Webkit Bug for more information.
UPDATE (7/25/14): There is an improved solution provided below involving text-align: justify. This method is simpler and avoids the Webkit bug mentioned earlier.