user123_456
user123_456

Reputation: 5795

position fixed element always inside the div

I need to be able to position fixed element always inside the div. Problem is occuring when I resize the window. Then the fixed div is always floating above all other elements. How to prevent that? I need that div to be fixed but positioned inside the div .

Here is an example:

<div id="main">
    <div id="one" style="background-color:yellow;"></div>
    <div id="two" style="background-color:black;"></div>
    <div id="three" style="background-color:yellow;">
        <div id="four"></div>
    </div>
</div>

CSS:

#main
{
    position:relative;
    width:1200px;
    top:0;
    bottom:0;
    left:100px;
}
#one,#two,#three
{
    position:relative;
    width:100px;
    height:1000px;
    float:left;
    top:0;
    bottom:0;
}
#four
{
    position:fixed;
    top:50px;
    background-color:blue;
    width:100px;
    height:200px;
}

EXAMPLE try moving horizontal scroll left and right and you will see what is happening.

Upvotes: 2

Views: 5425

Answers (2)

John
John

Reputation: 3415

Check out this answer on a similar question. The problem you're facing is something that can't be solved with CSS alone, unfortunately.

Upvotes: 2

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

Change position to relative.

Example

#four {
    background-color: blue;
    height: 200px;
    position: relative;
    top: 50px;
    width: 100px;
}

Upvotes: 2

Related Questions