How do I use CSS to position a fixed variable height header and a scrollable content box?
Assuming by "fixed" you mean position:fixed
, I don't think it's possible in pure CSS, as position:fixed
takes the element out of the document flow.
However, it should just take a line or two of JavaScript to get what you want. Something like this (untested, only for example purposes, will need syntax tweaked to actually work):
var height = document.getElementById("head").offsetHeight;
document.getElementById("content").style.marginTop = height + 'px';
Something like that should get you the rendered height of the fixed <div>
and set the content <div>
's margin accordingly. You'll also need to explicitly set a background color on the fixed <div>
, otherwise the content will appear to bleed into the fixed one when scrolling.
Here's a solution, but it's a cheat. Basically, you have a duplicate header element, to push down the content, under the fixed position one:
<div class="outer">
<div class="header">Header content goes here</div>
<div class="header-push">Header content goes here</div>
<div class="content">
...
</div>
</div>
I did a combination of both the accepted and Eric's answer. An empty div is used to push the content bellow "head". The width of this div is set by jQuery when window.onresize is fired:
function resizeHeader() {
$(".header-push").height($(".header").height());
}
$(document).ready(resizeHeader);
$(window).resize(resizeHeader);
See this jsFiddle for more info.