views:

102

answers:

1

I want to position an element in normal css relative to the element before it, and then once it's been initially positioned makes its position independant of the one before it.

For example, when you click on a 'widget' in the sidebar, it collapses, and all of the 'widgets' below it move up. I don't want them to move. Thanks.

+2  A: 

You cannot do this with pure CSS, but you can do it with jQuery, like this:

var elem = $('something');
var pos = elem.position();
elem.css({ position: 'absolute', top: pos.top, left: pos.left });

Until the Javascript runs, the element must not have position: absolute.

SLaks
This works, but is there any way to do this for each element?
Tim
Yes; you can put it in a loop.
SLaks
Thanks for all of the help. Any idea as to how to put it in a loop? I've tried using an each loop, but the values of pos.top and pos.left stay the same each time through.
Tim
In the each loop, try `pos = $(this).position()`.
SLaks