tags:

views:

27

answers:

3

Say I have an absolutely positioned div inside a normal one.

<div id="wrap"> <div id="sidebar"></div> </div>

In this case, when I state top and left for the "sidebar", they are calculated inside of the "wrap", because the latter is position="relative".

However, when I change the "sidebar" to position="fixed", the coordinates are calculated relative to the <body>, not the "wrap", which results in "sidebar" jumping out of its parent, even though the coordinates stay the same.

How do I make the "sidebar" to stay inside the "wrap", yet be fixed?

Thanks.

A: 

Actually, documentation for position:fixed property is very clear: Generates an absolutely positioned element, positioned relative to the browser window.

So, coordinates are calculated relative not to the body, but to the window.

How do I make the "sidebar" to stay inside the "wrap", yet be fixed?
The question seems strange. Even if 'sidebar' stays inside 'wrap' initially, user can always scroll down: 'wrap' will move to the top of the window (and possible disappear), while 'sidebar' will be in the exact same spot on user's screen.

edit
An example of element with 'fixed' position is "Welcome to ..." top bar when you come first time to any stack exchange site (e.g., cooking).

Nikita Rybak
A: 

Give a position: absolute to "siderbar", it should do the trick.

Some info regarding position relative + position absolute.

SteD
+1  A: 

The idea, as @Nikita implies, is not to use position: fixed but instead use position: absolute.

For this to work you'll also need to position the parent element, #wrap with position: relative' in order to have the#sidebar` position relative to to the parent element not the window.

David Thomas