stretch bottom to viewport without absolute positioning
is it possible to stretch the bottom on the "main" div to the viewport bottom without using absolute positioning? Absolute positioning works, but then won't re-position if any new elements are added.
<div id="wrapper">
<div id="header">
HEADER<br>
<input id="btnNavBar" type="button" value="add/remove nav bar"/>
</div>
<div id="main">
MAIN CONTENT NEEDS TO STRETCH TO BOTTOM OF VIEWPORT, BUT NOT BE ABSOLUTE SO THAT IT WILL AUTOMATICALLY ADJUST FOR ANY NEWLY ADDED DYNAMIC ELEMENTS ABOVE IT SINCE IT'LL STILL BE IN THE NORMAL FLOW.
</div>
</div>
CSS
html, body {
height: 100%;
margin: 0px;
}
#wrapper {
height: 100%;
background-color: black;
}
#header {
height: 50px;
text-align: center;
background-color: red;
}
#main {
background-color: gray;
height: 100%;
}
/* UNCOMMENT THIS SECTION TO MAKE THE STRETCHING WORK WITH ABSOLUTE POSITIONING,
BUT NOT FOR ANY NEWLY ADDED ELEMENTS. be sure to re-run the fiddle after uncommenting this */
/*#wrapper {
position: relative;
}
#main {
height: auto;
position: absolute;
bottom: 0px;
top: 50px;
}*/
JavaScript
$('#btnNavBar').click(function(){
var $navBar = $('#navBar');
if ($navBar.length == 0)
{
$('<div>').prop('id','navBar').css({'height':'20px', 'background-color':'yellow', 'text-align':'center'}).text('Dynamically Added Nav Bar').insertAfter('#header');
}
else
{
$navBar.remove();
}
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.