I have two divs: -A header bar, which is fixed while scrolling, and stuck at the top of the page. -A notification div, which contains a message banner that will slide down if triggered.
The header bar is fixed to the top fine, but I can't seem to get the notification div to fix itself just under it. Every time I try this, this div fixes to the top of the page in-front of my header bar; seemingly replacing it. Padding doesn't seem to help.
Can anybody offer me any suggestions, please?
Here is the working div:
#header {
text-align: left;
background-image:url(../Resources/Banner2.gif);
background-repeat:no-repeat;
background-color:#00ed32;
color:#FFF;
position:fixed;
width:100%;
top:0px;
left:0px;
padding:15px;
}
Here is the div I would like to fix under it:
.notify {
background: url(../resources/gradients.png)
repeat-x 0px 0px;
top: -40px;
left: 0px;
position:fixed;
z-index: 100;
width: 100%;
overflow: hidden;
}
Set everything up as you would if you want to position: absolute inside a position: relative container, and then create a new fixed position div inside the div with position: absolute , but do not set its top and left properties. It will then be fixed wherever you want it, relative to the container.
All that is needed to fix this is “min-height” and “min-width” in your CSS. this makes a Div responsive. minimum height will prevent the Divs from overlapping on each other. setting height to “auto” should also work.
An element with position: sticky; is positioned based on the user's scroll position. A sticky element toggles between relative and fixed , depending on the scroll position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).
Use position fixed. Position fixed makes it so that an element stays at it's specified place even if the user scrolls up or down.
The easiest way to do this is to put a "holder" bar at the top of the page and then nest the "header" and "notification" elements within there.
For example:
CSS
#holder {
left: 0;
position: fixed;
right: 0;
top: 0;
}
#header, .notify{
//what ever styles you have
//position: relative or static
}
HTML
<div id="holder">
<div id="header">...</div>
<div class="notify">...</div>
</div>
Working example: http://jsfiddle.net/Q6CWv/
Adding a slide down effect on the .notify
element should be fairly straight forward if you are using JQuery:
$('.notify').slideDown();
Working example: http://jsfiddle.net/Q6CWv/1/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With