Make Div Occupy Remaining Parent Height Without Absolute Positioning
In HTML/CSS, you often encounter a scenario where a child div needs to occupy the remaining height of its parent container without explicitly setting a specific height. Let's delve into the problem and its possible solutions.
Consider the following HTML/CSS code:
<div>
#container { width: 300px; height: 300px; border:1px solid red;} #up { background: green; } #down { background:pink;}
Here, the "down" div should occupy the whitespace below the "up" div.
Solution
There are several methods to achieve this:
Grid:
.container { display: grid; grid-template-rows: 100px; }
Flexbox:
.container { display: flex; flex-direction: column; } .container .down { flex-grow: 1; }
Calculation:
.container .up { height: 100px; } .container .down { height: calc(100% - 100px); }
Overflow:
.container { overflow: hidden; } .container .down { height: 100%; }
Note:
The above is the detailed content of How to Make a Div Occupy Remaining Parent Height Without Absolute Positioning?. For more information, please follow other related articles on the PHP Chinese website!