Razor Storm
Razor Storm

Reputation: 12336

set globally absolute position to a div

I am grabbing the position of an element using jquery's .offset() and then setting the div to be position:absolute; and setting it's left and top based off the information I got from offset().

This works as long as all the element's parents are statically positioned since position:absolute is defined as

"The element is positioned relative to its first positioned (not static) ancestor element"

So now I'm presented with 2 choices to fix this issue:

  1. Somehow force the element to be positioned relative to the document. I don't see anything in the css spec that allows me to do this. (correct me if I'm wrong).
  2. Instead of using offset(), use a function that grabs the element's position relative to its first positioned ancestor element. (I don't see any jQuery functions that do this. There is position() but that gets it relative to the element's parent).

How do I do either of these 2 options?

Upvotes: 1

Views: 2276

Answers (2)

Evan Davis
Evan Davis

Reputation: 36602

Added comment as answer so you can accept:

Remove the node and reattach it to the body, making it relative to the document. Not ideal, but it'd allow you to position it correctly.

Upvotes: 2

antjanus
antjanus

Reputation: 997

You CAN actually just set left/top because that overrides that description. If you just leave it "absolute", it'll position itself as described: relative to the first static element before it. If you set left/top, it should position itself correctly.

The only time this is not true is if the parent element has a relative positioning. Then you're kind of screwed :/

Upvotes: 1

Related Questions