views:

19

answers:

1

I have a set of divs with variable heights that have been floated left. When there are too many of said divs on a single line, the next div wraps to a new row (as it should). The problem I'm having is that the new row is started at an offset x position on the new row, such that the div is beneath the last div in the previous row that has no taller divs behind it- quite often leaving a large margin on the left side.

div.entry 
{
    float: left;
    width: 180px;
    padding: 10px;
}

I essentially want it so that the last div.entry in each "row" (the one before it wraps to a new row) clears the float, so that the next row of floated divs are all aligned to the same height, and don't have a rather large gap on the left side. Illustrated in ASCII:

What I expect to happen:

+-----+ +-----+ +-----+ +-----+
|  A  | |  B  | |  C  | |  D  |
|     | |     | |     | +-----+
+-----+ |     | +-----+
        +-----+
+-----+ +-----+ +-----+ +-----+
|  E  | |  F  | |  G  | |  H  |
|     | |     | |     | +-----+
+-----+ |     | +-----+
        +-----+

What actually happens:

+-----+ +-----+ +-----+ +-----+
|  A  | |  B  | |  C  | |  D  |
|     | |     | |     | +-----+
+-----+ |     | +-----+
        +-----+ +-----+ +-----+
                |  E  | |  F  |
                |     | |     |
                +-----+ |     |
                        +-----+
+-----+ +-----+
|  G  | |  H  |
|     | +-----+
+-----+

Michael

A: 

I had this problem once, I think I got around it my setting a min-height property to the divs.

Which gave me:

+-----+ +-----+ +-----+ +-----+
|  A  | |  B  | |  C  | |  D  |
|     | |     | |     | +-----+
+-----+ |     | +-----+
        +-----+ 
+-----+ +-----+ +-----+ +-----+ 
|  E  | |  F  | |  G  | |  H  |
|     | |     | |     | +-----+
+-----+ |     | +-----+
        +-----+ 
Michael Robinson
This would work, however the divs have unknown variable height; any arbitrarily set min-height could potentially (and probably will) be exceeded, at which point it reverts to the above behavior.
Jason
Yeah I had this problem too... I had ended up detecting the max height of the divs server-side and assigning that as the min-height...
Michael Robinson
I was hoping there was some way to do this in CSS, if not I will probably go with this solution or use javascript to clear the last div in each row.
Jason
Or, if you know the width could you wrap each row of divs in their own div?
Michael Robinson
The width of each individual div is fixed, however the element they are contained in has variable width.
Jason