Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative div is passing over fixed div while scrolling

Tags:

css

I have a fixed positioned div on the top of the page. (e.g. Facebook) And an relative positioned div in the page. When I scroll down, relative div is passing over fixed div. I want it to pass under fixed div. Is there any idea to handle this ?

enter image description here

#navcontainer
{
    position:fixed;
}

.city
{
    position:relative;
    float:left; 
}

.city .text 
{
    position:absolute;
    top:100px; 
    left:15px;
}

In this css I have a city div and absolute text is sitting on the image which is in relative div (.city)

like image 863
Mtok Avatar asked Apr 24 '12 18:04

Mtok


2 Answers

Hi I think you should do this:

css

#navcontainer
{
    position:fixed;
    background:red;
    left:0;
    right:0;
    top:0;
    height:100px;
    z-index:3;
}

.city
{
    position:relative; 
    background:green;
    left:0;
    right:0;
    padding:10px;
    top:100px;
    z-index:2;
}

.city .text 
{
    padding:10px;
    background:yellow;
}​

HTML

<div id="navcontainer">This is navigation</div>

<div class="city">
    <div class="text">here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br />here text <br /></div>
</div>
​

Live demo http://jsfiddle.net/xLnKN/1/

like image 74
Rohit Azad Malik Avatar answered Nov 16 '22 01:11

Rohit Azad Malik


I think what you are looking for is the CSS property z-index. See here for documentation: http://reference.sitepoint.com/css/z-index

I threw together a small example showing how to use it with your CSS: http://jsfiddle.net/B63Km/

The basic idea is the higher the z-index, the closer the element is to you.

like image 23
Rusty Fausak Avatar answered Nov 16 '22 03:11

Rusty Fausak