Ionut
Ionut

Reputation: 2858

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?

Upvotes: 11

Views: 1947

Answers (3)

ThinkingStiff
ThinkingStiff

Reputation: 65341

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

Upvotes: 5

Adaz
Adaz

Reputation: 1665

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.

Upvotes: 2

Blender
Blender

Reputation: 298106

Sadly, you can't do this with CSS2/3.

There is jQuery Masonry, a fairly small script that emulates float: top; and works well.

Upvotes: 0

Related Questions