I ran into an issue with absolute position with a nested p
tag. This JSFiddle demonstrates the difference. Based on the description on this question and the comment by user1334007 absolute positioning is relative to the first parent. Even though w3schools does not state that, it appears to be the case for div
tags. For p
tags, it seems that absolute is relative to the page as Michael Zaporozhets states in the SO answer and w3school describes.
With all these links in mind, am I making a mistake with my styles somewhere or are these performing differently? If they perform differently, can someone offer an explanation as to why this happens please? The main reason I am asking is this is a question in the 70-480 certification tests and even though I know the answer the say is correct, I would like to be able to use positioning with confidence going forward.
Code in jsFiddle link (required to have code to submit jsfiddle link so i just put it all in)
<h2>Paragraph Position</h2>
<p class="outer">Hello Outer
<p class="inner">Hello Inner</p>
</p>
<br/>
<h2>Division Position</h2>
<div class="outer">Hello Outer
<div class="inner">Hello Inner</div>
</div>
.outer {
position: relative;
background-color: red;
height: 100px;
width: 500px;
}
.inner {
position: absolute;
top: 15px;
left: 15px;
background-color: green;
}