I have a div and a ul that I’m trying to float to create two columns. The items within the ul are also floated so they will expand horizontally before wrapping vertically:
<style type="text/css">
ul {list-style-type: none;}
li {float:left; width:275px; min-height: 50px; padding: 12px; border-radius: 4px; border: 2px outset #eee;}
.float-right {width: 300px; float: right; margin-left: 25px;}
.float-left {float: left;}
</style>
<div class="float-right">
This content should float to the right
</div>
<ul class="float-left">
<li>Item 1</li><li>Item 2</li><li>Item 3</li><li>Item 4</li>
<li>Item 5</li><li>Item 6</li><li>Item 7</li><li>Item 8</li>
</ul>
If I remove the float from the list items, then everything works as expected, however, when the li elements have a float applied, the ul element seems to ‘lose’ it’s float.
Is there a way to force the ul to float to the left of the div, while allowing the contents of the ul to float?
Note: I need the width of the ul to be dynamic, so I can’t set an explicit width to it.
Thanks!!!
Update
I’m trying to achieve something like this:

The text on the right can have a static width, but the ul containing the boxes should not have an explicit width (so that if the width of the browser is increased, then the “Test 3” box will move to the first row).
The problem I’m having is that if I don’t set a width on the ul, then the text content is moved above the ul:

Is there a way to position the div first, then restrict the ul to only use the remaining space?
To Achieve your goal:
1. All
liinside theulmust be floated horizontally on 1 line.When this is achieved:
2. The
ulmust find enough space beside thedivto float beside it.You must give the
ulamax-width, to force it not to expand and take space as much as it wants, make sure its enough so that all of its childrenliare floated beside one another.If the
max-widthisn’t enough, then theliwill have no strength to say NO!, and they will simply take new lines below each other.Check it out : http://jsfiddle.net/AliBassam/3EmdM/
Why is this happening?
When you are telling the
litofloat:left;it is as if you’re telling them : Try your best to float left, take every space you can so you can float left, cry and complain to your Mum (ul) and tell her that you MUST float left! All of us on the same line!.When the
ulnoticed that not all its childrenlihave floated on the same line, it takes a new line below thedivso it can achieve that.Here’s another example, notice that when the 2
lihave floated beside each other (1), and when there’s enough space for the wholeulto be beside thediv(2), it will float beside it.Here’s another example with 2
divs, same result, the seconddivwill not float until all its childrendivhave floated inside of it (1), and enough space have been found beside the otherdiv(2).UPDATE
What you need is to have both the
divand theulinside a Parent Div, thisdivwill have aposition:relatve;andmin-width, giving it a minimum width will allow it to expand when you maximize the browser, and it will allow it to become smaller but only to a limit.Then give the child
divthe staticwidth, let’s say,250px, and give theulaposition:absolute;withright:250pxor a little bit more (consider it as amargin-right).Check it out : http://jsfiddle.net/AliBassam/FFrev/