I'm struggling to get a div to expand fully to it's container's height.
Markup:
<div class="container">
<div class="innerOne inner"></div>
<div class="innerTwo inner"></div>
</div>
At different viewports .innerTwo
's content is taller than that of .innerOne
's but I would like it's background to be the same size as .innerTwo
's
Styles:
.container {
width: 100%;
height: auto;
background-color: yellow;
/* clearfix */
*zoom: 1;
&:before,
&:after {
display: table;
content: "";
line-height: 0;
}
&:after {
clear: both;
}
}
.inner {
float: left;
width: 50%;
height: 100%;
background-color: red;
}
But the heights wont match up. I know it can be done by giving the container a set height but I don't want to do that since it's a responsive site. Is this possible? I'd rather not use JS.
With the advent of the CSS flex model, solving the 100% height problem becomes very, very easy: use height: 100%; display: flex on the parent, and flex: 1 on the child elements. They'll automatically take up all the available space in their container.
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.
4 Answers. Show activity on this post. height: 100% gives the element 100% height of its parent container. height: auto means the element height will depend upon the height of its children.
If height: auto; the element will automatically adjust its height to allow its content to be displayed correctly. If height is set to a numeric value (like pixels, (r)em, percentages) then if the content does not fit within the specified height, it will overflow.
You can use display:table-cell property for this. Write like this:
.container{
width:100%;
border:1px solid red;
display:table;
}
.inner{
display:table-cell;
background:green;
width:50%;
}
.innerTwo{
background:blue
}
Check this http://jsfiddle.net/XXHTC/
You can find the desired solution here: http://matthewjamestaylor.com/blog/equal-height-columns-cross-browser-css-no-hacks
Good luck :)
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