So, I need to have a header that’s always displayed at the top of the viewport, even when the user scrolls down. That bit is fairly easy, using something like this:
body {
padding-top: 100px; /* height of the header */
}
#header {
position: fixed;
top: 0;
left: 50%;
margin-left: -500px; /* Half the width of the header */
width: 1000px;
height: 100px;
}
However, now my problem is that I don’t always know how tall the header will be, because the client wants a top banner ad to also follow along, but that might not always be there.
So, sometimes the header might look like this:
<div id="header">
<img src="#" class="top_banner" />
<div id="headerbar">
<h1>Site logo</h1>
</div>
</div>
And sometimes it might look like this:
<div id="header">
<div id="headerbar">
<h1>Site logo</h1>
</div>
</div>
The good news is that the banner ad will always have the same dimensions. So basically #header could be 100px without the banner, and 250px if the banner is there.
My question to you is: is there a nice CSS ways to solve this? If not, how could I solve it using Javascript?
this can help you