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/
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With