Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the left and top values of an element and its child element if both have position:absolute

I have this html, where element's position is absolute, but left & top are auto:

<div style="position:absolute; width:100px; height:100px;">
    <span style="position:absolute;">Test child</span>
</div>

I feel that the left & top for the span would be 0 px, but couldn't surely decipher from specs or other posts.

So, if I didn't specify the left & top for absolutely positioned element whose parent is also absolutely positioned & has no margin or padding, the left and top would be 0px, Is that correct as per css specs?

Also, in same case as above, but under writing mode top-to- bottom & left-to-right writing mode, the top & right would be 0px, right?

Edit: To make it more clear, I meant the left & top would be 0px, 0px relative to the parent div. Or Is above equivalent to :

<div style="position:absolute; width:100px; height:100px;">
    <span style="position:absolute;left:0px;top:0px">Test child</span>
</div>

Thanks

like image 439
Nitesh Avatar asked Dec 17 '15 08:12

Nitesh


People also ask

What happens when an element is positioned absolutely?

Setting position: absolute An absolutely positioned element no longer exists in the normal document flow. Instead, it sits on its own layer separate from everything else. This is very useful: it means that we can create isolated UI features that don't interfere with the layout of other elements on the page.

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.

What is absolute positioning?

Absolute positioning In contrast, an element that is absolutely positioned is taken out of the flow; thus, other elements are positioned as if it did not exist. The absolutely positioned element is positioned relative to its nearest positioned ancestor (i.e., the nearest ancestor that is not static ).


1 Answers

First, you should note that the initial values for top, left, right and bottom are auto and not 0. See section 9.3.2 of the specs

So to the question:

if I didn't specify the left & top for absolutely positioned element whose parent is also absolutely positioned & has no margin or padding, the left and top would be 0px, Is that correct as per css specs?

The answer is no this isn't correct. In your example, it happens to be true because the child element is positioned there in the flow of the document (even without any positining).

As you can see in this example :

<div style="position:absolute; width:100px; height:100px;">
  Some text
  <span style="position:absolute;">Test child</span>
</div>

The only effect absolute positioning has in this case is to take the element out of the flow but it remains in its original position.

like image 124
web-tiki Avatar answered Nov 15 '22 07:11

web-tiki