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.
You just put width property to ul element.
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.
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.
Yes, ul is a block level element so it takes full width. If you want to specify element width you can use inline-block.
You could achieve this, by setting your 'li' to display
as inline-block
in stead of floating them. You should then set the white-space
on 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:
inline-block
work in legacy browsers
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With