Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to bypass then nearest positioned ancestor?

"The absolutely positioned element is positioned relative to nearest positioned ancestor." -MDN: position - CSS

I understood this when there was a parent who was defined as position:relative; but what I didn't realize was that position:absolute technically qualified as a "positioned ancestor".

Here is a sample: http://jsfiddle.net/MSzZG/ It would be nice if the text "At top" could have the top property apply to the window instead of the containing div but I just cannot figure out if it is possible to bypass.

<div>
 <div>Content
 </div>
</div>
<div style="position:absolute;">
 <div style="position:absolute;top:0px;">At top
 </div>
</div>

Is there any way to bypass a previously positioned absolute element (without using fixed)? A javascript solution is acceptable.

like image 552
Travis J Avatar asked Jun 13 '13 20:06

Travis J


3 Answers

You'll have to calculate the difference between its natural position and the desired position to place it absolutely in the window.

If you're using jQuery, you can let it do the math for you via the .offset(coordinates) function.

like image 177
drch Avatar answered Nov 17 '22 00:11

drch


Answer

No. You can't do that.

Reasoning

If you WERE able to do that then you could position one div completely away from the parent. This would not only make the parent "lonely" but also entirely defeat the purposes of nesting. Nesting div tags or any tags for that matter is not just done for cosmetic purposes; it is done to convey textual meaning to the user.

May i suggest reconsidering your approach? Perhaps if you explain what you would like the final result to become, we can help you. there must surely be a solution.

Possible Solutions

  • If you need the inner div be positioned at the top then take it one level out.
  • remove absolute positioning from the parent.
  • Utilzing another method of positioning other than absolute.
like image 28
ProfileTwist Avatar answered Nov 17 '22 00:11

ProfileTwist


If you want the inner position: absolute div to be positioned relative to the window (which you should specify position: relative on) you need to not nest it.

position: absolute will ALWAYS be positioned relative to its MOST IMMEDIATE positioned parent (with position: absolute or position: relative). You can't bypass this - you just have to structure your HTML better or implement the CSS differently.

So basically the answer to your question is no :( at least not without using fixed positioning which you specifically mentioned not wanting to do.

like image 1
Ennui Avatar answered Nov 16 '22 22:11

Ennui