Hi

I'm trying to create a horizontal navbar with CSS. The navbar must has a width equal to the width of the container. It has 7 list elements, and they must have the same width. But if i use a correct width (100/7) for each list element the last breaks in a new line. This is the code:

nav
{
    margin-top:25px;
    text-align: center;
}

ul.dropdown                         
{
    margin:0 auto;   
    height:38px;
    border:1px solid #fff;
    width:100%;
}

ul.dropdown li.firstlevel  
{  
    float:left;
    width:14.28%;
    white-space: nowrap;
    display:inline-block;
    background: #aebcbf; /* Old browsers */
    background: -moz-linear-gradient(top, #aebcbf 0%, #6e7774 25%, #0a0e0a 61%, #212121 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#aebcbf), color-stop(25%,#6e7774), color-stop(61%,#0a0e0a), color-stop(100%,#212121)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, #aebcbf 0%,#6e7774 25%,#0a0e0a 61%,#212121 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, #aebcbf 0%,#6e7774 25%,#0a0e0a 61%,#212121 100%); /* Opera11.10+ */
    background: -ms-linear-gradient(top, #aebcbf 0%,#6e7774 25%,#0a0e0a 61%,#212121 100%); /* IE10+ */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#aebcbf', endColorstr='#212121',GradientType=0 ); /* IE6-9 */

}

ul.dropdown li.firstlevel:hover   
{
    background: #aebcbf; /* Old browsers */
    background: -moz-linear-gradient(top, #aebcbf 0%, #6e7774 27%, #031e00 54%, #348910 98%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#aebcbf), color-stop(27%,#6e7774), color-stop(54%,#031e00), color-stop(98%,#348910)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, #aebcbf 0%,#6e7774 27%,#031e00 54%,#348910 98%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, #aebcbf 0%,#6e7774 27%,#031e00 54%,#348910 98%); /* Opera11.10+ */
    background: -ms-linear-gradient(top, #aebcbf 0%,#6e7774 27%,#031e00 54%,#348910 98%); /* IE10+ */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#aebcbf', endColorstr='#348910',GradientType=0 ); /* IE6-9 */    
}

ul.dropdown li.firstlevel:active
{
    background: #aebcbf; /* Old browsers */
    background: -moz-linear-gradient(top, #aebcbf 0%, #6e7774 27%, #031e00 54%, #3be01a 98%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#aebcbf), color-stop(27%,#6e7774), color-stop(54%,#031e00), color-stop(98%,#3be01a)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, #aebcbf 0%,#6e7774 27%,#031e00 54%,#3be01a 98%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, #aebcbf 0%,#6e7774 27%,#031e00 54%,#3be01a 98%); /* Opera11.10+ */
    background: -ms-linear-gradient(top, #aebcbf 0%,#6e7774 27%,#031e00 54%,#3be01a 98%); /* IE10+ */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#aebcbf', endColorstr='#3be01a',GradientType=0 ); /* IE6-9 */
}

ul.dropdown li.firstlevel a                   
{
    padding:10px;
    display: block; 
    color: #B5B5B5;  
}

ul.dropdown li.firstlevel:hover a                 
{
    color: #fff;
}

ul.dropdown li#first
{        
    border-right: 1px solid #353535;
    -webkit-border-radius: 7px 0 0 7px;
    -moz-border-radius: 7px 0 0 7px;
    border-radius: 7px 0 0 7px;
}

ul.dropdown li#last
{  
    display:inline-block;
    float:none;
    -webkit-border-radius: 0 7px 7px 0;
    -moz-border-radius: 0 7px 7px 0;
    border-radius: 0 7px 7px 0;
}

ul.dropdown li.middle
{    
    border-right: 1px solid #353535;
}

There's a way to set the width of the last element to fill the remaining space?

1 answer

danwellman 5600
1
point

It could be the border on the <li>s forcing the last one on to the new line?

Have you tried white-space:nowrap on the <ul> instead of the <li>s? Should stop the list items wrapping but isn't completely safe in all browsers...

Answered almost 8 years ago by danwellman
  • @danwellman will be correct. You are adding up to 100% plus the combined width of the borders (looks like 7 pixels) which will cause you to go over. Mixing percentage based and pixel based measurements is tricky at best, and impossible at worst. I would look at switch to one or the other entirely. Ktash almost 8 years ago