Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS auto change ul width when li increase (floating left)

I have a problem to auto fix UL`s width that LI can float straightly.

If I change css, set UL with a width like '1000px', not 'auto'; that what I want can be done.

However, is there other any settings only by CSS also can achieve same result?, because I want to auto change UL`s width as increasing number of LI.

    Now is like
    div ul-------------------------------div ul
            |   ///////      ////////       |
            |   /////// li   ///////  li    | 
            ---------------------------------
               ///////     ////////
               ////// li   ///////  li

   I want to be like this  
        div                                 div
          ul-------------------------------------------------------------ul
            |   ///////      ////////       |  ///////      ////////    |
            |   /////// li   ///////  li    | /////// li   ///////  li  |
            -------------------------------------------------------------

http://jsfiddle.net/KgyHZ/21/

HTML

<div class='test'>
    <ul>
        <li></li>
        <li></li> 
        <li></li>
        <li></li>
    </ul>
</div>

CSS

.test{
    width: 500px;
    height: 100px;
    overflow: hidden;
}
.test > ul{
    list-style:none;
    width: auto;
    height: 100%;
    background-color:#999;
    display: block;
    padding: 0px;
    margin: 0px;
}
.test > ul > li{
    float: left;
    display: block;
    height: 100%;
    width: 180px;   
    background-color:#99c;
    margin-right:10px;
}

Thank you very much for your advice.

like image 557
Micah Avatar asked Jun 13 '13 20:06

Micah


People also ask

How do you change the width of UL?

You just put width property to ul element.

Can width be auto in CSS?

Width: autoWhen an element has auto as a value for width, it can have margin, padding, and border without becoming bigger than its parent element. The width of its content box will be the content itself with the subtraction of margin, padding, and border.

How do I set auto width in CSS?

Using width, max-width and margin: auto; Then, you can set the margins to auto, to horizontally center the element within its container. The element will take up the specified width, and the remaining space will be split equally between the two margins: This <div> element has a width of 500px, and margin set to auto.

How do you make a full width UL?

Yes, ul is a block level element so it takes full width. If you want to specify element width you can use inline-block.


1 Answers

You could achieve this, by setting your 'li' to display as inline-block in stead of floating them. You should then set the white-spaceon the ul to nowrap to force them all on the same line. Something like this:

ul{
    list-style:none;
    white-space: nowrap;
}
ul > li{
    display: inline-block;
}

You can now add as many li's as you want, and your ul will keep growing.

There are a few things that you should take into account when using this technique:

  • This might produce a horizontal scrollbar, and users hate those
  • You may need to add some extra css to make the inline-block work in legacy browsers
  • Your ul width will not exceed the width of its parent as demonstrated in the fiddle example below. The li's are in fact overflowing their parent. Not realy a problem, but you may have to be creative when working with backgrounds on the ul.

For a simple fiddle that demonstrates the technique: http://jsfiddle.net/KgyHZ/213/

like image 128
Pevara Avatar answered Oct 21 '22 22:10

Pevara