I have two elements, one is set as relative, and another is set as absolute
.container {
position: relative;
}
.target {
position: absolute;
}
<div class="container">
<p>Veggies es bonus vobis, proinde vos postulo essum magis kohlrabi welsh onion daikon amaranth tatsoi tomatillo melon azuki bean garlic.</p>
<div class="target">Target</div>
<p>Gumbo beet greens corn soko endive gumbo gourd. Parsley shallot courgette tatsoi pea sprouts fava bean collard greens dandelion okra wakame tomato. Dandelion cucumber earthnut pea peanut soko zucchini.</p>
</div>
Here is the result, Target overlapped with the paragraph
Question: in my understanding, a session that is set as absolute means it will relative to the closet element which is set as relative. So I understand why "Target" will just be right below "relative" elements. But why the paragraph will overlap with target
? Should it be located right below target
element
In my code above example, I expect it will show like this. So I am confused.
Edit:
Even though this question seems to be asked many times, many people are still failed to get it clearly, this proved there is still much confusion in other answers.
So, I have read many answers and still got confusion by other answers, which doesn't explain well for my experience. But I got my understanding from relative and absolute in this question.
So I try to sum up a bit, if there are others who still got confusion and at some point, read this post. Hope it helps.
I will explain it from my example.
First, aside from the understanding of removing from normal flow, one may understand absolute in a way that any elements after it will ignore it. In my example, the reason why we see "target" overlap with paragraph is that paragraph just ignore it. That's why we see this effect.
So, if relative and absolute can also be tweaked, what is the difference?
First, when we want an element to be tweaked relative to its original position. We set it as relative.
If we want to tweak it relative to the html, we set it as relative. So yes, absolute doesn't mean it wont be relative to anything. Plus, it will also be relative to its nearest relative elements.