views:

165

answers:

3

I have two columns and want to stack divs of different heights in order of appearance.

The divs are dynamically created.

I have tried to do this by floating the first story to the left and the second to the right, but their still seem to be some anomalies.

Have a look at this demo, it should explain it all.

http://dl.getdropbox.com/u/142343/divstack.html

A: 

Try to use "clear:left" for floated to left and "clear:right" for floated to right.

You cannot create two columns without empty space between with that HTML structure.

If you want two columns without empty space, then you need to generate different structure - place odd elements in one column and even in other.

Sasha Yanovets
there seems to be some traction to your suggestion, but they still do not neatly stack. have a look at:http://dl.getdropbox.com/u/142343/divstack.html
A: 

Put them in a table with two cells. (Yes, yes, flame away!)

Added: To clarify: what I meant was this:

<table>
    <tr>
        <td>
            <div>First DIV</div>
            <div>Third DIV</div>
            <div>Fifth DIV</div>
        </td>
        <td>
            <div>Second DIV</div>
            <div>Fourth DIV</div>
            <div>Sixth DIV</div>
        </td>
    </tr>
</table>

Throw in some width and padding settings, and you'll have two neat little stacks.

Now, looking at your examples I think I realized something else yet. You want the <div>'s to arrange automatically in such a way that both stacks are as equally sized as possible. I don't think that's possible.

Well, you might try to render them, and then check their heights with JS, and rearrange them... but this will cause some nasty flickering in the browser.

Added 2: Check the link: http://valts.21.lv/problem/DivStack.html

Vilx-
I don't mind putting it in a table but if i do they either won't stack properly or i can't keep my order.
I don't follow you... you say that the DIVs are dynamically generated (javascript). So, when you generate them, why not add an additional IF statement which determines in which column it should go? In fact, how are they sorted in the two columns anyway?
Vilx-
ok, so the content is pulled from a database, and voilla, we have 10 rows. (php)So create the table, and put the first box in the first <td> and the second box in the second <td>, then i have to create another row, and 3rd box in the first <td> etc etc.This will not allow the boxes to neatly stack
Modified my answer.
Vilx-
Have a look at the link i posted. I need the boxes to be in order horizontally.. so box 1 at the top of first column, box 2 at the top of the second column. Your example puts box 2 under box 1 in first column
Gah, use that round object between your ears! Those are your DIVs, you can arrange them in any way you like! OK, here, I modified the example. Does it make sense now?
Vilx-
LOL, I don't think you get what I'm saying!
Apparently. Can you elaborate?
Vilx-
Added a link. Isn't that just like your example?
Vilx-
Yes it is. However, you have changed the flow of the html. The html is output as box 1, then box 2, box 3, box 4.I'm thinking I could do it your way, by looping twice, the first time loop through the odd entries, then the even entries (which is a halfway solution)but optimal would be that i loop once and the css takes care of positioning? //I hope that makes it clearer?
Ahh, so that's what you want! Well then, I'm afraid I can't help you. Although - what's the problem looping through it twice? And if you *really* don't want to loop through it twice - well, loop through it once, and add the even-entry HTML to one string, and odd-entry HTML to another string. And then render the first string in the left cell, and the second string in the right cell. There are many ways of achieving this result.
Vilx-
+1  A: 

I'm not sure if this is the desired effect you are going for, but try using

.left {
  float:left;
  clear:both;
}
.right {
  float:left;
}

as your CSS for left and right.

tschaible
if I add what you suggest, the boxes in each column do not stack properly. They have massive vertical spaces between them.
+1 that should do it. @IbnDrupal have you modified your example page so that we can see it?
jeroen
It's not entirely clear to me what you're expected result is. Alternatively, you can switch the clear:both to a clear:left and the .right to a float:right for a slightly different stacking behavior with less vertical space (but don't always align on top). If you can provide more details as to how you would like it to stack, I can probably help further.
tschaible