I have a container div
with two children. The first child has a given height. How can I make the second child to occupy the "free space" of the container div
without giving a specific height?
In the example, the pink div
should occupy also the white space.
Similar to this question: How to make div occupy remaining height?
But I don't want to give position absolute.
You can use floats for pushing content down:
You have a fixed size container:
#container {
width: 300px; height: 300px;
}
Content is allowed to flow next to a float. Unless we set the float to full width:
#up {
float: left;
width: 100%;
}
While #up
and #down
share the top position, #down
's content can only start after the bottom of the floated #up
:
#down {
height:100%;
}?
Unless I am misunderstanding, you can just add height: 100%;
and overflow:hidden;
to #down
.
#down {
background:pink;
height:100%;
overflow:hidden;
}?
Edit: Since you do not want to use overflow:hidden;
, you can use display: table;
for this scenario; however, it is not supported prior to IE 8. (display: table;
support)
#container {
width: 300px;
height: 300px;
border:1px solid red;
display:table;
}
#up {
background: green;
display:table-row;
height:0;
}
#down {
background:pink;
display:table-row;
}?
Note: You have said that you want the #down
height to be #container
height minus #up
height. The display:table;
solution does exactly that and this jsfiddle will portray that pretty clearly.
My answer uses only CSS, and it does not use overflow:hidden or display:table-row. It requires that the first child really does have a given height, but in your question you state that only the second child need have its height not specified, so I believe you should find this acceptable.
html
<div id="container">
<div id="up">Text<br />Text<br />Text<br /></div>
<div id="down">Text<br />Text<br />Text<br /></div>
</div>
css
#container { width: 300px; height: 300px; border:1px solid red;}
#up { background: green; height: 63px; float:left; width: 100% }
#down { background:pink; padding-top: 63px; height: 100%; box-sizing: border-box; }
I'm not sure it can be done purely with CSS, unless you're comfortable in sort of faking it with illusions. Maybe use Josh Mein's answer, and set #container
to overflow:hidden
.
For what it's worth, here's a jQuery solution:
var contH = $('#container').height(),
upH = $('#up').height();
$('#down').css('height' , contH - upH);
Its been almost two years since I asked this question. I just came up with css calc() that resolves this issue I had and thought it would be nice to add it in case someone has the same problem. (By the way I ended up using position absolute).
http://jsfiddle.net/S8g4E/955/
Here is the css
#up { height:80px;}
#down {
height: calc(100% - 80px);//The upper div needs to have a fixed height, 80px in this case.
}
And more information about it here: http://css-tricks.com/a-couple-of-use-cases-for-calc/
Browser support: http://caniuse.com/#feat=calc
check the demo - http://jsfiddle.net/S8g4E/6/
use css -
#container { width: 300px; height: 300px; border:1px solid red; display: table;}
#up { background: green; display: table-row; }
#down { background:pink; display: table-row;}
I didn't find a fully satisfying answer so I had to find it out myself.
My requirements:
calc()
should not be used as the remaining element shouldn't know anything about another element sizes;flex-grow: 1
to all direct parents with computed height (if any) and the element so they will take up all remaining space when the element content size is smaller;flex-shrink: 0
to all flex items with fixed height so they won't become smaller when the element content size is bigger than the remaining space size;overflow: hidden
to all direct parents with computed height (if any) to disable scrolling and forbid displaying overflow content;overflow: auto
to the element to enable scrolling inside it.JSFiddle (element has direct parents with computed height)
JSFiddle (simple case: no direct parents with computed height)
Source: Stackoverflow.com