简体   繁体   English

如何使每个div具有相同的高度,以保持每行3 div对齐工作

[英]How to make each div the same height, in order to keep the 3 div per row alignment working

My site contains a series of divs (represented by green boxes), and I only need 3 per row. 我的网站包含一系列div(以绿色框表示),每行只需要3个。 But they're not all equal height, so it pushes the other divs out of alignment. 但是它们的高度并不相等,因此将其他div推离对齐状态。

See here: 看这里:

屏幕截图

and here's my demo site showing the full issue if you scroll down: http://testsite24.netai.net/public/demo.html 如果您向下滚动,这是我的演示站点,其中显示了完整的问题: http : //testsite24.netai.net/public/demo.html

Here's an example of the code for one of the divs (green boxes): 以下是其中一个div(绿色框)的代码示例:

<div class="block personal fl">

                <!-- CONTENT -->
               <div class="content">
                  <p class="price">
                    <p class="vignette" style="background-image:url()"></p>
                  </p>

               </div>

                <ul class="features">
    <li class="titlebox">59 Acre Paradise</li>
    <li>Kings County</li>
    <li>Offered at $95,000</li>
                  </ul>

</div>

Can you offer a solution to make all div heights the same height, so they stay in alignment? 您能否提供一种解决方案,使所有div高度都保持相同的高度,以使它们保持对齐? The solution must work for IE8+. 该解决方案必须适用于IE8 +。 Perhaps a script to identify the longest div in the row and make the other two the same height? 也许是一个脚本来标识行中最长的div,并使其他两个高度相同?

Previously I used this script, but FlexWrap isn't supported by IE8/IE9 so I had to scrap it. 以前我使用此脚本,但是IE8 / IE9不支持FlexWrap,因此我不得不将其废弃。 Any ideas? 有任何想法吗? Thank you!! 谢谢!!

<script>

;( function( $, window, document, undefined )
{
    'use strict';

    var s = document.body || document.documentElement, s = s.style;
    if( s.webkitFlexWrap == '' || s.msFlexWrap == '' || s.flexWrap == '' ) return true;

    var $list       = $( '.list' ),
        $items      = $list.find( '.list__item__inner' ),
        setHeights  = function()
        {
            $items.css( 'height', 'auto' );

            var perRow = Math.floor( $list.width() / $items.width() );
            if( perRow == null || perRow < 2 ) return true;

            for( var i = 0, j = $items.length; i < j; i += perRow )
            {
                var maxHeight   = 0,
                    $row        = $items.slice( i, i + perRow );

                $row.each( function()
                {
                    var itemHeight = parseInt( $( this ).outerHeight() );
                    if ( itemHeight > maxHeight ) maxHeight = itemHeight;
                });
                $row.css( 'height', maxHeight );
            }
        };

    setHeights();
    $( window ).on( 'resize', setHeights );
    $list.find( 'img' ).on( 'load', setHeights );

})( jQuery, window, document );

</script>

This seems super excessive when you can just use vertical-align:top; 当您仅可以使用vertical-align:top;时,这似乎显得过分vertical-align:top; on the items... Give them a min-height to make it a bit stronger. 在物品上...给他们一个最小的高度,使其更坚固。

use 采用

display: table
display: table-cell

Code pen: http://codepen.io/anon/pen/mypGBV 密码笔: http : //codepen.io/anon/pen/mypGBV

you can use max-height to force the div(which is causing trouble) not to get bigger than the particular value. 您可以使用max-height强制div(引起麻烦)不要大于特定值。 For Example set all 3 divs to max-height:50px. 例如,将所有3个div设置为max-height:50px。 They won't get bigger and remain aligned. 它们不会变大并保持对齐。

You don't need JS for this. 您不需要JS。 Just wrap each row of divs in a wrapper div with the style clear: both set. 只需将div的每一行都包装在具有clear: both风格的包装器div中clear: both设置了。 See this codepen: http://codepen.io/anon/pen/GgyXNp 看到此Codepen: http ://codepen.io/anon/pen/GgyXNp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM