CSS Layout
There are 5 values that you can pass in position property.
Elements are displayed on the screen as they written in the HTML document.
Consider the following piece of code:
H1, P, H3 and div are displayed on the screen in exact order as they written in the HTML file
<h1> This is h1 </h1>
<p> This is p </p>
<h3> This is h3 </h3>
<div class="box"></div>
Output:
This is p
This is h3
Let's start with position concept one by one.
Static Position
HTML elements are positioned static by default.
An element with position: static; is not positioned in any special way; it is always positioned according to the normal flow of the page.
Relative Position
Relative position is relative to itself.
For example: Consider the code and it's output. As you can see red box is shifted 100px from left because I applied left offset after giving it relative positioning.
.red {
position: relative;
left: 100px;
}
Output:
Relative Position (Cont...)
In the output below, the black dotted area would be the original position of red box if I don't apply position relative in it. As you can see it proved that relative position is relative to itself.
Relative Position (Cont...)
So now let me shift the blue box 100px towards left.
.blue {
position: relative;
right: 100px;
}
Notice here that document flow is as it is. So the relative position does not affect the document flow.
Output:
Relative Position (Cont...)
Absolute Position
For example: If I apply absolute position in the red box, then the red box will be out of the flow and hence no space will be allocated to it.
See the output, red box is out of flow and hence yellow box is at top and followed by green and blue * Yellow box is below red
Absolute Position (Cont...)
See the output, red box is out of flow and hence yellow box is at top and followed by green and blue.
Yellow box is below red.
.red {
position: absolute;
}
Output:
Absolute Position (Cont...)
<div class="parent">
<div class="child"></div>
</div>
Consider the code below, Red is the parent div and black is the child div. In this particular case, body is the parent of red div.
Absolute Position (Cont...)
Now, apply relative position to red(parent) div and absolute position to black(child) div.
As I mentioned absolute position is relative to closest ancestor having some position property.
/* RED DIV */
.parent {
position: relative;
}
/* BLACK DIV */
.child {
position: absolute;
top: 100px;
left: 100px;
}
Absolute Position (Cont...)
Output:
Absolute Position (Cont...)
Let's understand it in little more details.
.first-parent {
height: 500px;
width: 500px;
background: green;
}
.second-parent {
height: 300px;
width: 300px;
background: red;
.child {
height: 100px;
width: 100px;
background: black;
}
<div class="first-parent">
<div class="second-parent">
<div class="child"></div>
</div>
</div>
Consider this piece of code. Here green div is a parent of red and red div is a parent of black.
Absolute Position (Cont...)
Output:
Absolute Position (Cont...)
So, now apply position property in green and black. In black div we have absolute position so in that case black div will be relative to green not red, because here black's closest ancestor is green which has some position property.
.first-parent {
position: relative;
height: 500px;
width: 500px;
background: green;
}
.second-parent {
height: 300px;
width: 300px;
background: red;
.child {
position: absolute;
height: 100px;
width: 100px;
background: black;
}
Absolute Position (Cont...)
Output:
Fixed Position
Fixed position elements is always relative to the viewport. Which means it always stays in the same place even if the page is scrolled.
For example, consider this red element. It will always fixed at same place even if the page is scrolled.
Fixed element also break the document flow.
Demo:
https://stackblitz.com/edit/js-apig2c?file=style.css
Sticky Position
Sticky position is the mixture of relative and fixed position. It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).
The sticky state does not break the document flow.
Demo:
https://stackblitz.com/edit/js-apig2c?file=style.css