Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break out of position:relative *without* changing structure

Tags:

html

css

position

A simplified example:

HTML:

<div id="A">
    <div id="B"></div>
    <div id="C"></div>
    <div id="D"></div>
</div>

CSS:

#A,#B,#C,#D{width:100px;height:100px}
#A{position:relative;width:220px;top:20px;left:20px;background:#FF0000}
#B{position:absolute;top:0;left:0;background:#FFFF00}
#C{position:absolute;top:10px;left:80px;background:#00FF00}
#D{position:absolute;background:#00FFFF;top:0;right:0}

As a fiddle: http://jsfiddle.net/h6BNz/

OK so C is in front of B and behind D, and positioned relative to A. I would like to position it relative to the document, but keep it between B and D (in both z-index and tab order). If C's position is changed to position:fixed, it does exactly what I want except for (obviously) not scrolling with the page.

I've seen plenty of solutions which involve breaking the div out of its parent to accomplish this, but that would require setting z-indices and tab order which seems like a nightmare to manage (this is a plugin, so the surrounding code is outside my control).

How can I give C a truly absolute position without breaking B or D, or changing the structure? JavaScript is fine for setting this up, but I need the final page position to be perfectly rounded (see some of my other questions if you're interested in why), so I don't think I can use an absolutePosition - absolutePositionOfContainer method.

like image 237
Dave Avatar asked Apr 18 '13 16:04

Dave


People also ask

How do I turn off relative position?

To turn off the relative positioning of an element in CSS, you can set the position property of that element to static , this will attach that element back to the whole document flow.

How do you move an element with a relative position?

Relative Positioning You can use two values top and left along with the position property to move an HTML element anywhere in the HTML document. Move Left - Use a negative value for left. Move Right - Use a positive value for left. Move Up - Use a negative value for top.

When using position fixed What will the element always be positioned relative to?

Fixed positioning This can be used to create a "floating" element that stays in the same position regardless of scrolling. In the example below, box "One" is fixed at 80 pixels from the top of the page and 10 pixels from the left. Even after scrolling, it remains in the same place relative to the viewport.

How does absolute and relative positioning work together?

position: relative places an element relative to its current position without changing the layout around it, whereas position: absolute places an element relative to its parent's position and changing the layout around it.


1 Answers

If #A has position: relative anything inside of it will be positioned relative to #A.

So #B #C and #D will all be contained by #A. You can't position it relative to the document if it is inside something with a position.

You can use negative margins to position it outside of #A, however, provided #A has overflow: visible.

like image 165
nullability Avatar answered Sep 27 '22 15:09

nullability