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

后端 未结 22 2752
自闭症患者
自闭症患者 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:21

    Flexbox

    With flexbox it's a single declaration:

    .row {
      display: flex; /* equal height of the children */
    }
    
    .col {
      flex: 1; /* additionally, equal width */
      
      padding: 1em;
      border: solid;
    }
    <div class="row">
      <div class="col">Lorem ipsum dolor sit amet, consectetur adipisicing elit.</div>
      <div class="col">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ad omnis quae expedita ipsum nobis praesentium velit animi minus amet perspiciatis laboriosam similique debitis iste ratione nemo ea at corporis aliquam.</div>
    </div>

    Prefixes may be required for older browsers, see browser support.

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

    you can use jQuery to achieve this easily.

    CSS

    .left, .right {border:1px solid #cccccc;}
    

    jQuery

    $(document).ready(function() {
        var leftHeight = $('.left').height();
        $('.right').css({'height':leftHeight});
    });
    

    HTML

       <div class="left">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi malesuada, lacus eu dapibus tempus, ante odio aliquet risus, ac ornare orci velit in sapien. Duis suscipit sapien vel nunc scelerisque in pretium velit mattis. Cras vitae odio sed eros mollis malesuada et eu nunc.</p>
       </div>
       <div class="right">
        <p>Lorem ipsum dolor sit amet.</p>
       </div>
    

    You'll need to include jQuery

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

    This is an area where CSS has never really had any solutions — you’re down to using <table> tags (or faking them using the CSS display:table* values), as that’s the only place where a “keep a bunch of elements the same height” was implemented.

    <div style="display: table-row;">
    
        <div style="border:1px solid #cccccc; display: table-cell;">
            Some content!<br/>
            Some content!<br/>
            Some content!<br/>
            Some content!<br/>
            Some content!<br/>
        </div>
    
        <div style="border:1px solid #cccccc;  display: table-cell;">
            Some content!
        </div>
    
    </div>
    

    This works in all versions of Firefox, Chrome and Safari, Opera from at least version 8, and in IE from version 8.

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

    You can use Jquery's Equal Heights Plugin to accomplish, this plugins makes all the div of exact same height as other. If one of them grows and other will also grow.

    Here a sample of implementation

    Usage: $(object).equalHeights([minHeight], [maxHeight]);
    
    Example 1: $(".cols").equalHeights(); 
               Sets all columns to the same height.
    
    Example 2: $(".cols").equalHeights(400); 
               Sets all cols to at least 400px tall.
    
    Example 3: $(".cols").equalHeights(100,300); 
               Cols are at least 100 but no more than 300 pixels tall. Elements with too much content will gain a scrollbar.
    

    Here is the link

    http://www.cssnewbie.com/equalheights-jquery-plugin/

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