CSS Position: What Is Document Normal Flow?
CSS Position: What Is Document Normal Flow?
1 Static position
This is the default CSS position applicable to all HTML elements.
This position place the HTML elements based on normal document
flow. Using this position, top/right/bottom/left properties can
not be applicable to elements (ie., static position elements don’t
obey top/right/bottom/left properties).
Note: Page scrolling does affect this position.
.p1 {
width:50px;
height:50px;
background-color:cornflowerblue;
position:static;
}
.p2 {
width:50px;
height:50px;
background-color:sandybrown;
}
<body>
<div class="p1">
</div>
<div class="p2">
</div>
</body>
2 Relative position
This position places the element relative to its normal position.
This position is relative to normal document flow. Here
top/right/bottom/left properties can be applied to elements.
Note:
.p1 {
width:50px;
height:50px;
background-color:sandybrown;
}
.p2 {
width:50px;
height:50px;
background-color:lightgreen;
position:relative;
left:60px;
top:20px;
}
<body>
<div class="p1">
</div>
<div class="p2">
</div>
</body>
3 Absolute position
This position places the element at the exact position specified.
This position is relative to
• its parent element position when parent element position is
relative/absolute.
• document body (browser viewport) when parent element position
is static.
• document body (browser viewport) when there is no parent
element.
Note:
.p1 {
width:50px;
height:50px;
background-color:cornflowerblue;
}
.p2 {
width:50px;
height:50px;
background-color:sandybrown;
}
.p3 {
width:50px;
height:50px;
background-color:lightgreen;
position:absolute;
left:60px;
top:20px;
}
<body>
<div class="p1">
</div>
<div class="p2">
</div>
<div class="p3">
</div>
</body>
4 Fixed position
This position places the element at a fixed place relative to the
viewport. Page scrolling does not affect this position.
Note: This position element is completely removed from normal
document flow.
.p1 {
width:50px;
height:50px;
background-color:lightgreen;
position:fixed;
left:40px;
top:40px;
}
<body>
<div class="p1">
</div>
</body>
5 Sticky position
Sticky position is a combination of relative and fixed. It is
relative until it crosses a specified threshold, at which point it
is treated as static position.
Many browsers don’t support Sticky position partially/fully.
Following image (from https://caniuse.com/#feat=css-sticky) shows
Sticky position supporting browsers and its versions.
Note: This position element is completely removed from normal
document flow.
.p1 {
width:50px;
height:50px;
background-color:sandybrown;
position:relative;
left:50px;
top:50px;
}
.p2 {
width:50px;
height:50px;
background-color:lightgreen;
position:absolute;
right:50px;
top:50px;
}
<body>
<div class="p1">
</div>
<br/>
<div class="p1">
</div>
<br />
<div class="p1">
</div>
<div class="p2">
</div>
<br />
<div class="p2">
</div>
<br />
<div class="p2">
</div>
</body>
.parent {
width:150px;
height:150px;
background-color:sandybrown;
position:static;
}
.child {
width:50px;
height:50px;
background-color:lightgreen;
position:relative;
left:160px;
top:50px;
}
<body>
<div class="parent">
</div>
<br/>
<div class="parent">
<div class="child">
</div>
</div>
</body>
.parent {
width:150px;
height:150px;
background-color:sandybrown;
position:relative;
left:50px;
top:60px;
}
.child {
width:50px;
height:50px;
background-color:lightgreen;
position:static;
}
<body>
<div class="parent">
<div class="child">
</div>
</div>
</body>
.parent {
width:150px;
height:150px;
background-color:sandybrown;
position:absolute;
left:160px;
top:50px;
}
.child {
width:50px;
height:50px;
background-color:lightgreen;
position:relative;
left:25px;
top:30px;
}
<body>
<div class="parent">
<div class="child">
</div>
</div>
</body>