Khaled
Khaled

Reputation: 8573

Stretch child div height to fill parent that has dynamic height

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/

Upvotes: 129

Views: 208528

Answers (6)

Ashutosh Yadav
Ashutosh Yadav

Reputation: 1

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.

Upvotes: 0

HelloGello
HelloGello

Reputation: 361

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

I think you can use display as grid:

.parent { display: grid };

Upvotes: 9

Trong Lam Phan
Trong Lam Phan

Reputation: 2412

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

Upvotes: 71

Ayan
Ayan

Reputation: 8886

Add the following CSS:

For the parent div:

style="display: flex;"

For child div:

style="align-items: stretch;"

Upvotes: 33

Mr_Green
Mr_Green

Reputation: 41832

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

Upvotes: 53

Levon Matevosyan
Levon Matevosyan

Reputation: 21

You can do it easily with a bit of jQuery

$(document).ready(function(){
  var parentHeight = $("#parentDiv").parent().height();
  $("#childDiv").height(parentHeight);
});

Upvotes: -8

Related Questions