Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make children divs the height of tallest child

Tags:

css

I have two child <div> elements inside of a parent container <div> as shown:

<div class="row">     <div class="item">         <p>Sup?</p>     </div>     <div class="item">         <p>Sup?</p>         <p>Wish that other guy was the same height as me</p>     </div> </div> 

I tried the following CSS:

.item {     background: rgba(0,0,0,0.1);     display: inline-block; }  .row {     border: 1px solid red; } 

How can I get both children (div.item) to be the height of the tallest child?

jsFiddle: http://jsfiddle.net/7m4f7/

like image 649
Don P Avatar asked May 16 '13 18:05

Don P


People also ask

How do you make a parent DIV the same height as a child?

Using height: auto or using min-height on parent element will work. And for avoiding horizontal scrollbar, which can be caused due to padding or border can be avoided by using box-sizing: border-box on child element or overflow: hidden on parent element.

How do you set height to 100% of a parent?

container div has two parent elements: the <body> and the <html> element. 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.

Why do my divs have no height?

The reason why the height or the containers is 0 is because you are floating all the content inside them and floated elements don't expand the height (or width) of their parents.


2 Answers

One solution is to change the display value from inline-block to table-cell for the descendant div.item elements:

.row {     border: 1px solid red;     display: table;     border-spacing: 20px; /* For controlling spacing between cells... */ } .item {     background: rgba(0,0,0,0.1);     display: table-cell; } 

Example

like image 175
Yana Avatar answered Oct 08 '22 02:10

Yana


Another solution is to use flexbox: http://jsfiddle.net/7m4f7/4/

.item {     background: rgba(0,0,0,0.1);     -webkit-box-flex: 1;     -moz-box-flex: 1;     -webkit-flex: 1;     -ms-flex: 1;     flex: 1; }  .row {     border: 1px solid red;     display: -webkit-box;     display: -moz-box;     display: -webkit-flexbox;     display: -ms-flexbox;     display: -webkit-flex;     display: flex; } 

However, support is limited (but getting better!) See http://caniuse.com/flexbox

Otherwise you need to use javascript to set the heights manually.

like image 26
nullability Avatar answered Oct 08 '22 01:10

nullability