Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Top-Fixed Divs?

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;
}
like image 957
Josh Avatar asked May 15 '12 07:05

Josh


People also ask

How do you fix fixed positions?

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.

How do I fix two div overlapping?

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.

What is the difference between position fixed and sticky?

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).

How do you get a div to stay in place?

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.


1 Answers

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>

Edit

Working example: http://jsfiddle.net/Q6CWv/

Update

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/

like image 70
My Head Hurts Avatar answered Sep 20 '22 12:09

My Head Hurts