I have written the HTML code like this:
<div id="FirstDiv"></div>
<div id="SecondDiv"></div>
<div id="TARGETDIV"></div>
And CSS code like this:
#FirstDiv {
width: 100vw;
height: 50px;
}
#SecondDiv {
width: 100vw;
height: 230px;
}
#TARGETDIV {
width: 100vw;
height: /* What code should be here? */;
}
I have already set the height of FirstDiv
and SecondDiv
, and I want to make the TARGETDIV
height as large as possible to fit the user’s screen size.
How can I write CSS code like that?
2
Answers
You can use the
vh
unit andcalc
in yourheight
setting:Meaning: Full viewport height minus heights of the other two divs:
(Note: View the snippet in full page view to see the correct result – the first two divs together are already higher than the snippet window)
The modern way would probably be to use a flexbox and put a flex-grow on the target div. Using the CSS
calc
function like others said also works, but isn’t dynamic(On StackOverflow you have to click on the "Full Page" link after pressing "Run" to be able to see it)