127

As it can be seen in the following fiddle, I have two divs, contained in a parent div that have stretched to contain the big div, my goal is to make those child divs equal in height.

http://fiddle.jshell.net/y9bM4/

4

6 Answers 6

71

Use display: flex to stretch your divs:

div#container {
    padding:20px;
    background:#F1F1F1;
    display: flex;
}

.content {
    width:150px;
    background:#ddd;
    padding:10px;
    margin-left: 10px;
}

JSFIDDLE

0
53

The solution is to use display: table-cell to bring those elements inline instead of using display: inline-block or float: left.

div#container {
  padding: 20px;
  background: #F1F1F1
}
.content {
  width: 150px;
  background: #ddd;
  padding: 10px;
  display: table-cell;
  vertical-align: top;
}
.text {
  font-family: 12px Tahoma, Geneva, sans-serif;
  color: #555;
}
<div id="container">
  <div class="content">
    <h1>Title 1</h1>

    <div class="text">Sample Text. Sample Text. Sample Text. Sample Text. Sample Text. Sample Text. Sample Text. Sample Text. Sample Text.
      <br>Sample Text. Sample Text. Sample Text.
      <br>Sample Text.
      <br>
    </div>
  </div>
  <div class="content">
    <h1>Title 2</h1>

    <div class="text">Sample Text. Sample Text. Sample Text. Sample Text. Sample Text. Sample Text. Sample Text. Sample Text. Sample Text.</div>
  </div>
</div>

Working Fiddle

1
  • 4
    This doesn't work if the parent element is table cell though. Commented Oct 18, 2017 at 19:30
31

Add the following CSS:

For the parent div:

style="display: flex;"

For child div:

style="align-items: stretch;"
2
  • I needed to add the stretch on the children for flex to work for me.
    – BadHorsie
    Commented Feb 20, 2019 at 13:50
  • 2
    That's works for me, but for display: grid which is inside stretched element, I have to add height: 100% parameter. The grid contains dynamic height by default also.
    – Falcon
    Commented Apr 15, 2021 at 11:10
9

https://www.youtube.com/watch?v=jV8B24rSN5o

I think you can use display as grid:

.parent { display: grid };
2
  • Did you test your answer using the original poster's same code? Commented Sep 19, 2018 at 14:45
  • It works and doesn't require any CSS on the child. That's because a CSS Grid cell will have auto row and cell by default. It actually works pretty nicely with IE if you use display: -ms-grid to avoid some flexbugs, as long you only have one child.
    – ShortFuse
    Commented Feb 14, 2019 at 22:20
0

Add Display:flex to parent, and Align-items: stretch; to child, if you need to cover up the space with elements then also add justify-content: space-between.

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Aug 31, 2023 at 21:14
-8

You can do it easily with a bit of jQuery

$(document).ready(function(){
  var parentHeight = $("#parentDiv").parent().height();
  $("#childDiv").height(parentHeight);
});
1
  • 4
    Although once the browser window is resized this will break, whereas the CSS ones will still work.
    – SharpC
    Commented Nov 9, 2016 at 12:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.