Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't a 'position: absolute' element be positioned in relation to a parent that is 'position: static'?

Could someone explain the reason why a position: absolute; can only be positioned in relation to its nearest non - position: static; parent?

What is the reason the element can not be positioned in relation to its position: static; parent?

like image 642
Cu1ture Avatar asked Jul 30 '14 16:07

Cu1ture


People also ask

Is position absolute relative to parent?

In position: relative , the element is positioned relative to itself. However, an absolutely positioned element is relative to its parent. An element with position: absolute is removed from the normal document flow. It is positioned automatically to the starting point (top-left corner) of its parent element.

When you place position absolute on something what is it positioned relative to?

An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed). However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

Can an element have position absolute and relative?

Clearly, nothing can be absolute and relative at the same time. Either you want the element to position itself based on another ancestor's position or based on the page flow.

What determines the position of an element that has position absolute set?

The absolutely positioned element is positioned relative to its nearest positioned ancestor (i.e., the nearest ancestor that is not static ).


1 Answers

I believe that the reasoning behind this is so that you can position absolute elements relative to elements that are not just that element's direct parent. Since position: static is the default, it makes sense to have it be the determining factor on whether the element should position itself relative to that parent.

For example, I could use the following html to position an element relative to its grandparent:

<div id="grandparent" style="position:relative">
    <div id="parent">
       <div id="child" style="position:absolute;top:0">
       </div>
    </div>
</div>
like image 125
neatnick Avatar answered Oct 17 '22 02:10

neatnick