Is it possible to simply auto-height a child div to the remaining height not being used by other component of it’s parent? For the below example, the .body would only be like 20px high, because it’s only using that much for the inner html. Is it possible for the .body to automatically consume the unused height of the .parent? e.g. .parent 200px – .head 30px – .foot 30px = .body 120px?
The sample below will display the .parent yellow box much taller than the used space. If you set .body to “height: 100%”, it’ll use the parent’s height and not respect the .head or .foot elements.
<html>
<head>
<style type="text/css">
.parent {
width: 200px;
height: 200px;
background-color: yellow;
}
.head { height: 30px; background-color: blue; }
.body { background-color: #999; }
.foot { height: 30px; background-color: green; }
</style>
</head>
<body>
<div class="parent">
<div class="head">I'm the head</div>
<div class="body">I'm the body</div>
<div class="foot">I'm the foot</div>
</div>
</body>
</html>
This is only an example. In my project the .parent height can only be reasonably set in the .parent element. Plus the .parent height is essentially dynamically set by the back-end code. The three inner div organization is because the body is collapsible and I have rounded corners for the head and foot.
Any suggestions are well appreciated!
This can easily be achieved with negative margins!
.bodyto 100% height.headand.footis known, you can add a negative top + bottom margin equal to the respective heights of.headand.foot..bodywill “cover”.head. To counter this, addposition: relativeto.head..bodydirectly. Better, add another dive inside.bodywith padding top + bottom set to desired height.Demo here
Variant of the above example:
.bodyto 100% height.headand.footis known, you can add a negative bottom margin equal to the sum of heights of.headand.foot..bodywill attempt to flow outside the parent, addoverflow: hiddento the parent.Demo here