开发者

How can I make a DIV take up the rest of the height of a container div?

开发者 https://www.devze.com 2022-12-15 15:18 出处:网络
I have a div with two nested divs. The first child has varying height depending on its content, I want the the 2nd divs height to be whatever is left over from the parent.

I have a div with two nested divs. The first child has varying height depending on its content, I want the the 2nd divs height to be whatever is left over from the parent.

<div开发者_运维问答 style="height:500px;">
   <div>Some Content Here</div>
   <div>This div needs to take up the rest of the space of its parent</div>
</div>

How can I do this?

Thanks, ~ck in San Diego


It requires some javascript. I see you're using jQuery, so this should work:

Give some id to your parent div:

<div style="height:500px;" id="parent">
    <div>Some Content Here</div>
    <div>This div needs to take up the rest of the space of its parent</div>
</div>

Then in jQuery:

$('div#parent div:last').each(function() {
    var p = $(this).parent();
    $(this).height(p.height() - ($(this).offset().top - p.offset().top));
});


I think I've got a correct way to do it without Javascript:

<div style="height:500px; background:pink; overflow: hidden">
   <div style="background: yellow">stuff</div>
   <div style="height: 100%; background: red;">This div needs to take up the rest of the space</div>
</div>

The key being "overflow:hidden" on the main div, as setting the height of the 2nd div to 100% makes it 500 pixels tall.

0

精彩评论

暂无评论...
验证码 换一张
取 消