Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Ignoring divs height when floating

I'm trying to display some pictures. All of them have the same width but different height. I'm trying to do something like: correct image

Every picture's class name is pic

<img class = "pic" src = .... />

In the stylesheet I specified the float:left attribute:

.pic{
    float:left
}

The end result isn't the one expected while every row is vertically aligned after the highest div from the row before: wrong

Is there a way to solve my problem in pure cross browser css?

like image 210
Ionut Avatar asked Jan 12 '12 22:01

Ionut


2 Answers

You can do this with no extra markup with CSS3 column-count, assuming you at least have a single containing element.

Demo: http://jsfiddle.net/ThinkingStiff/NcxPr/

HTML:

<div id="container">
<img class="image" src="http://farm1.staticflickr.com/205/494701000_744cc3a83a_z.jpg" />
<img class="image" src="http://farm5.staticflickr.com/4028/4287569889_f6a4fca31b_z.jpg" />
<img class="image" src="http://farm3.staticflickr.com/2340/2421926504_d8509d0a98_z.jpg" />
<img class="image" src="http://farm1.staticflickr.com/197/503792921_fedf8ba47e_z.jpg" />
<img class="image" src="http://farm2.staticflickr.com/1153/741035029_f394e11a1f_z.jpg" />
<img class="image" src="http://farm7.staticflickr.com/6213/6243090894_8b8dd862cd_z.jpg" />
<img class="image" src="http://farm2.staticflickr.com/1339/1157653249_dbcc93c158_z.jpg?zz=1" />
<img class="image" src="http://farm3.staticflickr.com/2570/4220856234_029e5b8348_z.jpg?zz=1" />
</div>

CSS:

#container {
    column-count: 3;
    column-fill: balance;
    column-gap: 10px;
    width: 330px;
}

.image { 
    display: block;
    margin-bottom: 10px;
    width: 100px;
} 

Output:

enter image description here

like image 158
ThinkingStiff Avatar answered Nov 01 '22 21:11

ThinkingStiff


Are you able to add some extra markup?

If yes, you could make 3 columns with floating divs:

<div class="column">
    <img src="1.jpg" />
    <img src="4.jpg" />
</div>

<div class="column">
    <img src="2.jpg" />
    <img src="5.jpg" />
</div>

<div class="column">
    <img src="3.jpg" />
    <img src="6.jpg" />
</div>

Or just use jQuery to do that for you.

like image 22
Adaz Avatar answered Nov 01 '22 21:11

Adaz