Vertically aligning floated DIVs with varying heights?

对着背影说爱祢 提交于 2019-12-04 16:06:36

Is this what you had in mind? It does use jQuery and adds a bit of extra markup - not sure if that's acceptable.

http://jsfiddle.net/Awg3u/7/

I've only tested in Chrome/FF so far, and haven't checked in IE yet.

You can use display:inline-block

http://jsfiddle.net/Awg3u/1/

You basically want to find the maximum height for each box and then take the largest and set every other box to that height.

There should be any number of jQuery plugins that can do this or you can roll your own

Not sure if I understand what you want correctly.

If not correct me please.

If you want the all the first divs of the rows to have the same height (based on the first div) you could do something like:

(function() {
    var first = true;
    var height = 0;
    var pos;
    $('.box').each(function() {
        if (first) { // do we have the first div?
            height = $(this).height(); // set height and pos from left of first div to be used by other divs
            pos = $(this).position();
        }
        first = false;

        var current_pos = $(this).position();
        if (current_pos.left == pos.left) { // are we the first div in a row, e.g. the most left div?
            $(this).height(height); // set height to match the first div
        }
    });
})(jQuery);

http://jsfiddle.net/Awg3u/3/

You can add clearing div after every 2 blocks as follows:

<div class="box"></div>
<div class="box"></div>
<div class="clear"></div>
<div class="box"></div>
<div class="box"></div>
<div..................>

Or you can wrap your rows in a new DIV.

<div>
<div class="box"></div>
<div class="box"></div>
</div>
<div>
<div class="box"></div>
<div class="box"></div>
</div>
.......etc.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!