Created: 2022-11-27
position: relative;
Positioning allows you to move an element from where it would otherwise be placed in normal flow over to another location.
Positioning isn't a method for creating the main layouts of a page;
It's more about managing and fine-tuning the position of specific items on a page.
Understanding positioning also helps in understanding normal flow, and what it means to move an item out of the normal flow.
There are five types of positioning you should know about:

top, bottom, left, right.positioned {
position: relative;
top: 30px;
left: 30px;
}
These serve to move the affected element down and to the right. (COMPLETE OPPOSITE)
Think of it as the element being pushed on its left and top sides, which moves it right and down.
position: absolute;

An absolutely positioned element no longer exists in the normal document flow.
Thus, there's no gap
Rather than positioning the element based on its relative position within the normal document flow,
They specify the distance the element should be from each of the containing element's sides.
So in this case, we are saying that the absolutely positioned element should sit 30px from the top of the "containing element" and 30px from the left.
(In this case, the "containing element" is the initial containing block.
Which element is the "containing element" of an absolutely positioned element?
This is very much dependent on the position property of the ancestors of the positioned element
(See Identifying the containing block).
IF no ancestor elements have their position property explicitly defined,
THEN by default all ancestor elements will have a static position.
The result of this is
-> The absolutely positioned element will be contained in the initial containing block.
The initial containing block has the
<html> element.<html> element We can change the positioning context,
-> (which element the absolutely positioned element is positioned relative to).
This is done by setting positioning on one of the element's ancestors:
to one of the elements it's nested inside of
(you can't position it relative to an element it's not nested inside of).
To see this, add the following declaration to your body rule:
body {
position: relative;
}
The positioned element now sits relative to the <body> element.

<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>
completely remove an element from the normal flow
is very similar to absolute positioning
The item remains fixed in relation to viewport (scrolling doesn't affect the position of it)
Can create useful UI items that are fixed in place:
h1 {
position: fixed;
top: 0;
width: 500px;
margin-top: 0;
background: white;
padding: 10px;
}
The top: 0; is required to make it stick to the top of the screen.
We give the heading the same width as the content column
and then a white background
and some padding / margin
-> so the content won't be visible underneath it.
makes an element act like position: relative
it'll scroll in normal flow until it hits offsets from the viewport that we have defined.
At that point, it becomes "stuck" as if it had position: fixed applied
Example:
Scrolling Index Page:
https://mdn.github.io/learning-area/css/css-layout/positioning/7_sticky-positioning.html