Skip to content Skip to sidebar Skip to footer

How Can I Create A Horizontal Menu With Each Item Being Equal Width And Spacing Inbetween?

Here's what I've got so far: fiddle 2 problems with it though: I've hard-coded the width of each li to 33%, which I'd prefer not to do so that I can easily add more items. I want

Solution 1:

See:http://jsfiddle.net/f6qmm/

display: table is being used to evenly space a dynamic number of lis. Unfortunately, that doesn't work in IE7, so *float: left is used (for only IE7 and lower) so that at least they're all on one line - though it still looks horrendous.

padding-left: 5px is applied to every li, then li:first-child { padding-left: 0 } removes it for only the first li.

#main-nav {
    list-style: none;
    margin: 0;
    padding: 0;
    width: 100%;
    display: table;
    table-layout: fixed;
    overflow: hidden
}
#main-navli {
    display: table-cell;
    *float: left; /* improve IE7 */height: 25px;
    text-align: center;
    padding-left: 5px
}
#main-navli:first-child {
    padding-left: 0
}
#main-navlia {
    width: 100%;
    display: block;
    height: 100%;
    line-height: 25px;
    text-decoration: none;
    background-color: #e0e0f0;
    font-weight: bold;
    color: #021020;
}
#main-navlia:hover {
    background-color: #498cd5;
    color: #ddeeee;
}
<ulid="main-nav"><li><ahref="#">one</a></li><li><ahref="#">two</a></li><li><ahref="#">three</a></li></ul><hr /><ulid="main-nav"><li><ahref="#">one</a></li><li><ahref="#">two</a></li><li><ahref="#">three</a></li><li><ahref="#">four</a></li><li><ahref="#">five</a></li></ul>

Solution 2:

Try this fiddle: http://jsfiddle.net/Nk2Wy/1/.

This approach allows you adding an arbitrary number of items. If they do not fit on the line anymore, because they are so many or because the browser window is quite small, they are shown on further lines.

If you add a width: 50px to the a style, all those items have the same width. Right now, the width depends on the actual text.

See the question (and answers) How to get rid of white space between css horizontal list items? as well.

Due to this, I updated the fiddle once again: http://jsfiddle.net/Nk2Wy/3/.

Post a Comment for "How Can I Create A Horizontal Menu With Each Item Being Equal Width And Spacing Inbetween?"