Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make div same height as parent (displayed as table-cell)

I got a container div containing three child divs (vary in content) - each as tall as the tallest one. I managed this by setting the container to display:table and the child divs to display:table-cell etc.

Everything worked just fine, until...

I inserted a new div inside one of the child divs and tried to make it height:100% - so it would stretch to the same height as its parents, but that did not work.

Please see my JSFiddle: http://jsfiddle.net/bkG5A/

Any help would be greatly appreciated!

HTML

<div class="container">     <div class="child">         a<br />a<br />a     </div>     <div class="child">         a<br />a<br />a<br />a<br />a<br />a<br />a     </div>     <div class="child">         <div class="content">             a<br />a<br />a         </div>     </div> </div> 

CSS

.container {     display: table; } .child {     width: 30px;     background-color: red;     display: table-cell;     vertical-align: top; } .content {     height: 100%;     background-color: blue; } 
like image 738
Chris Avatar asked Mar 28 '14 12:03

Chris


People also ask

How do you make a div fit its parent container?

If you want the child divs to fit the parent size, you should put a margin at least of the size of the child borders on the child divs ( child. margin >= child. bordersize ).

How do you make a div full height of a parent?

Answer: Set the 100% height for parents too And we all know that the default value of the height property is auto , so if we also set the height of <body> and <html> elements to 100%, the resulting height of the container div becomes equal the 100% height of the browser window.

How can I make Div same height as Div?

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. Usage: $(object).


2 Answers

Another option is to set your child div to display: inline-block;

.content {     display: inline-block;     height: 100%;     width: 100%;     background-color: blue; } 

.container {    display: table;  }  .child {    width: 30px;    background-color: red;    display: table-cell;    vertical-align: top;  }  .content {    display: inline-block;    height: 100%;    width: 100%;    background-color: blue;  }
<div class="container">    <div class="child">      a      <br />a      <br />a    </div>    <div class="child">      a      <br />a      <br />a      <br />a      <br />a      <br />a      <br />a    </div>    <div class="child">      <div class="content">        a        <br />a        <br />a      </div>    </div>  </div>

JSFiddle Demo

like image 113
reinder Avatar answered Sep 23 '22 17:09

reinder


You have to set the height for the parents (container and child) explicitly, here is another work-around (if you don't want to set that height explicitly):

.child {   width: 30px;   background-color: red;   display: table-cell;   vertical-align: top;   position:relative; }  .content {   position:absolute;   top:0;   bottom:0;   width:100%;   background-color: blue; } 

Fiddle

like image 32
King King Avatar answered Sep 23 '22 17:09

King King