How do I keep two side-by-side divs the same height?

后端 未结 22 2740
自闭症患者
自闭症患者 2020-11-21 07:56

I have two divs side by side. I\'d like the height of them to be the same, and stay the same if one of them resizes. I can\'t figure this one out though. Ideas?

To c

相关标签:
22条回答
  • 2020-11-21 08:17

    I know its been a long time but I share my solution anyway. This is a jQuery trick.

    --- HTML

    <div class="custom-column">
        <div class="column-left">
            asd
            asd<br/>
            asd<br/>
        </div>
        <div class="column-right">
            asd
        </div>
    </div>
    
    <div class="custom-column">
        <div class="column-left">
            asd
        </div>
        <div class="column-right">
            asd
            asd<br/>
            asd<br/>
        </div>
    </div>
    

    ---- CSS

    <style>
    .custom-column { margin-bottom:10px; }
    .custom-column:after { clear:both; content:""; display:block; width:100%; }
        .column-left { float:left; width:25%; background:#CCC; }
        .column-right { float:right; width:75%; background:#EEE; }
    </style>
    

    --- JQUERY

    <script src="js/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $balancer = function() {
            $('.custom-column').each(function(){
                if($('.column-left',this).height()>$('.column-right',this).height()){
                    $('.column-right',this).height($('.column-left',this).height())
                } else {
                    $('.column-left',this).height($('.column-right',this).height())
                }
    
            });
    
        }
        $balancer();
        $(window).load($balancer());
        $(window).resize($balancer());
    
    });
    </script>
    
    0 讨论(0)
  • 2020-11-21 08:17

    I recently came across this and didn't really like the solutions so I tried experimenting.

    .mydivclass {inline-block; vertical-align: middle; width: 33%;}

    0 讨论(0)
  • 2020-11-21 08:18

    Using CSS Flexbox and min-height worked for me

    Say you have a container with two divs inside and you want those two divs to have the same height.

    You would set 'display: flex' on the container as well as 'align-items: stretch'

    Then just give the child divs a 'min-height' of 100%

    See the code below

    .container {
      width: 100%;
      background: linear-gradient(red,blue);
      padding: 1em;
      /* important */
      display: flex;
      /* important */
      align-items: stretch;
      justify-content: space-around;
    }
    
    .child {
      width: 100%;
      background: white;
      color: grey;
      margin: 0 .5em;
      padding: .5em;
      /* important */
      min-height: 100%;
    }
    <div class="container">
      
      <div class="child"><p>This is some text to fill the paragraph</p></div>
      <div class="child"><p>This is a lot of text to show you that the other div will stretch to the same height as this one even though they do not have the same amount of text inside them. If you remove text from this div, it will shrink and so will the other div.</p></div>
      
    </div>

    0 讨论(0)
  • 2020-11-21 08:18
    <div>
    
    <div style="border:1px solid #cccccc; float:left; min-height:200px;">
    
    Some content!<br/>
    Some content!<br/>
    Some content!<br/>
    Some content!<br/>
    Some content!<br/>
    
    </div>
    
    <div style="border:1px solid #cccccc; float:left; min-height:200px;">
    
    Some content!
    
    </div>
    
    </div>
    

    What I did here is to change the height to min-height and gave it a fixed value. if one of them is getting resized the other one will stay the same height. not sure if this is what you want

    0 讨论(0)
  • 2020-11-21 08:19

    Just spotted this thread while searching for this very answer. I just made a small jQuery function, hope this helps, works like a charm:

    JAVASCRIPT

    var maxHeight = 0;
    $('.inner').each(function() {
        maxHeight = Math.max(maxHeight, $(this).height());
    });
    $('.lhs_content .inner, .rhs_content .inner').css({height:maxHeight + 'px'});
    

    HTML

    <div class="lhs_content">
        <div class="inner">
            Content in here
        </div>
    </div>
    <div class="rhs_content">
        <div class="inner">
            More content in here
        </div>
    </div>
    
    0 讨论(0)
  • 2020-11-21 08:19

    This is a jQuery plugin which sets the equal height for all elements on the same row(by checking the element's offset.top). So if your jQuery array contains elements from more than one row(different offset.top), each row will have a separated height, based on element with maximum height on that row.

    jQuery.fn.setEqualHeight = function(){
    
    var $elements = [], max_height = [];
    
    jQuery(this).css( 'min-height', 0 );
    
    // GROUP ELEMENTS WHICH ARE ON THE SAME ROW
    this.each(function(index, el){ 
    
        var offset_top = jQuery(el).offset().top;
        var el_height = jQuery(el).css('height');
    
        if( typeof $elements[offset_top] == "undefined" ){
            $elements[offset_top] = jQuery();
            max_height[offset_top] = 0;
        }
    
        $elements[offset_top] = $elements[offset_top].add( jQuery(el) );
    
        if( parseInt(el_height) > parseInt(max_height[offset_top]) )
            max_height[offset_top] = el_height;
    
    });
    
    // CHANGE ELEMENTS HEIGHT
    for( var offset_top in $elements ){
    
        if( jQuery($elements[offset_top]).length > 1 )
            jQuery($elements[offset_top]).css( 'min-height', max_height[offset_top] );
    
    }
    

    };

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