tags:

views:

54

answers:

4

I want to place a variable number of HTML lists side by side inside a fixed-sized <div> element like the following:

+--------------------------------+
| LIST 1     LIST 2     LIST 3   |
| - xxx      - xxx      - xxx    |
| - xxx      - xxx               |
|            - xxx               |     I need this line break –
|                                | <-- I don't want LIST 4 to be
| LIST 4                         |     directly beneath LIST 1!
| - xxx                          |
| - xxx                          |
+--------------------------------+

I floated the lists (float: left;) to get them positioned side by side but I don't know how to avoid that LIST 4 moves up as far as possible (until it is directly beneath LIST 1).

How do I get the vertical space between LIST 1 and LIST 4? Is there any way to use the clear property elegantly to solve this problem?

A: 

I haven't tried this, but I'd imagine that you could put a margin-bottom on the lists, then the fourth list should clear the longest of the first-row lists, plus the margin. But I haven't tried this, so I could well be wrong.

David Thomas
This does not work - the list floats up as much as it can without contradicting the margin set ...
Marius Schulz
@Marius Schulz: ah, I must confess that I wrote that without stopping to think it through properly. I'll see if I can think of a more useful means to help. Does it have to be pure css, or would js/jQuery be an option?
David Thomas
I'm curious what browser you're using that isn't respecting the margin bottom. Works for me in Firefox/Chrome/IE/Opera. http://jsfiddle.net/XDUER/
Robert
@David Thomas: That's the annoying thing about that - it needs to be pure CSS :|.
Marius Schulz
@Robert: I'm using Firefox 3.6.8 that displayes the specified `margin-bottom` *correctly* but not *as intended* :).
Marius Schulz
What is your intent? I'm understanding that you want spacing between any list that is forced to a newline.
Robert
A: 

What you need to do is clear the left and right floats in between list 3 and list4.

Use the clear:both CSS property to, in effect, create a line break in between floated elements.

EDIT I attached jQuery to show you the most elegant way to dynamically produce the effect you are looking for.

<style type="text/css">
    ul {float:left;}
    .clear {clear:both;}
</style>

<script type="text/javascript" src="localjQuery.js"></script>

<script type="text/javascript">
 $('ul').last().prepend('<div class="clear"></div>')
</script>   
Moses
It becomes tricky because I don't know how many lists there are - maybe 5 lists fit side by side, maybe four, so I cannot decide which list is the last one of the first row ... It would be easy if the list HTML was static :|.
Marius Schulz
In that case, all you need to do is use JavaScript to determine the number of UL nodes and then prepend a clearer div to the last UL node.
Moses
This is pretty much the solution I was going to suggest given your original question. There is no css-only solution for a variable number of lists in the div. You could assign a width to the <ul>; then you'd know how many would fit.
Traingamer
A: 

You might try putting lists 1 - 3 in their own div, and 4 within another, this way the two parents float underneath each other. Div positioning like this gets kind of funky, and I've encountered this kind of problem a lot. A different option would be to query the size of list 1 - 3 after the document is loaded, and set their heights to the same, maximum value so that list 4 would be positioned below them correctly.

GrandmasterB
ULs and DIVs are both block level elements, so in effect nothing happens when you enclose a floated UL inside a separate DIV.
Moses
@Moses: if you apply `overflow: hidden` to the container div it does contain the floats as you'd expect.
David Thomas
A: 

Sorry. There is no simple CSS-only solution if the number of lists and the items in each list varies.

Larsenal