A Guide to CSS Positions
A Guide to CSS Positions
A Guide to CSS
Positions
static (default)
Elements are positioned according to the
normal document flow (no positioning applied).
1 .element {
2 position: static;
3 }
CodePen Demo
relative
Moves the element relative to its original
position without affecting other elements.
thedevspace.io
TheDevSpace | Full-Stack Web Development Course
1 .element {
2 position: relative;
3 top: 10px; "# moves down "$
4 left: 20px; "# moves right "$
5 }
CodePen Demo
absolute
Positions the element relative to the nearest
positioned ancestor (not static ), or the
<html> element if none exists.
1 .parent {
2 position: relative;
3 }
4 .child {
5 position: absolute;
6 top: 0;
7 right: 0;
8 }
CodePen Demo
thedevspace.io
TheDevSpace | Full-Stack Web Development Course
fixed
Sticks the element to a specific spot on the
screen, even when scrolling.
1 .element {
2 position: fixed;
3 bottom: 0;
4 right: 0;
5 }
CodePen Demo
sticky
The element scrolls with the page until it
reaches a threshold, then “sticks” in place.
1 .element {
2 position: sticky;
3 top: 0;
4 }
thedevspace.io
TheDevSpace | Full-Stack Web Development Course
CodePen Demo
z-index
Controls the stacking order of overlapping
positioned elements.
1 .element {
2 position: relative;
3 z-index: 10;
4 }
CodePen Demo
thedevspace.io