Make children divs the height of tallest child

前端 未结 3 769
情深已故
情深已故 2020-11-29 08:10

I have two child

elements inside of a parent container
as shown:

相关标签:
3条回答
  • 2020-11-29 08:31

    Another solution is to use flexbox: http://jsfiddle.net/7m4f7/4/

    .item {
        background: rgba(0,0,0,0.1);
        -webkit-box-flex: 1;
        -moz-box-flex: 1;
        -webkit-flex: 1;
        -ms-flex: 1;
        flex: 1;
    }
    
    .row {
        border: 1px solid red;
        display: -webkit-box;
        display: -moz-box;
        display: -webkit-flexbox;
        display: -ms-flexbox;
        display: -webkit-flex;
        display: flex;
    }
    

    However, support is limited (but getting better!) See http://caniuse.com/flexbox

    Otherwise you need to use javascript to set the heights manually.

    0 讨论(0)
  • 2020-11-29 08:37

    One solution is to change the display value from inline-block to table-cell for the descendant div.item elements:

    .row {
        border: 1px solid red;
        display: table;
        border-spacing: 20px; /* For controlling spacing between cells... */
    }
    .item {
        background: rgba(0,0,0,0.1);
        display: table-cell;
    }
    

    Example

    0 讨论(0)
  • 2020-11-29 08:51

    jquery version: FIDDLE

    var maxheight = 0;
    
    $('div div.y').each(function () {
        maxheight = ($(this).height() > maxheight ? $(this).height() : maxheight); 
    });
    
    $('div div.y').height(maxheight);
    

    EDIT: Just noticed you're not dealing with the height of <div>'s, but <p>'s

    0 讨论(0)
提交回复
热议问题