The position property specifies the type of positioning method used for an element. There are five main values: static, relative, fixed, absolute, and sticky.
Elements are positioned according to the normal flow of the page. They are not affected by the top, bottom, left, and right properties.
div { position: static; }
The element is positioned relative to its normal position. Setting top: 10px moves it 10 pixels away from its original location, without affecting the position of other elements.
div {
position: relative;
left: 30px;
top: 10px;
}
The element is removed from the normal document flow and positioned relative to its nearest positioned ancestor (an ancestor with a position other than static).
.parent { position: relative; }
.child {
position: absolute;
top: 10px;
right: 10px;
}
The element is positioned relative to the viewport (the browser window). It stays in the same place even if you scroll the page.
.navbar {
position: fixed;
top: 0;
width: 100%;
}
The element oscillates between relative and fixed, depending on the user's scroll position. It 'sticks' to a specified location (e.g., top: 0) within its parent container.
Scroll down inside this box...
div {
position: sticky;
top: 0;
}
When elements are positioned, they can overlap each other. The z-index property specifies the stack order of an element (which element should be in front, and which should be behind).
.front { z-index: 10; }
.back { z-index: -1; }
position value of relative, absolute, fixed, or sticky.