CSS Position Property
CSS Position Property
Position Property in
Position Values:
1. static (default):
- The default positioning for all HTML elements.
- Elements are positioned according to the normal document flow
- top, right, bottom, left, and z-index properties do not apply.
Example:
div {
position: static;
}
2. relative:
Example:
div {
position: relative;
top: 20px;
left: 10px;
}
In this example, the div is shifted 20px down and 10px to the right from
where it would normally be positioned.
3. absolute:
- The element is removed from the normal document flow and positioned
relative to its nearest positioned ancestor (i.e., an ancestor with a
relative, absolute, or fixed position).
- If no such ancestor exists, it is positioned relative to the initial
containing block (usually the viewport).
- top, right, bottom, and left properties are used to position theelement.
Example:
div {
position: absolute;
top: 50px;
left: 30px;
}
In this example, the div is positioned 50px from the top and 30px from
the left of its nearest positioned ancestor.
4. fixed:
- The element is removed from the normal document flow and positioned
relative to the viewport.
- The element stays fixed in position even when the page is scrolled.
- top, right, bottom, and left properties are used to position theelement.
Example:
div {
position: fixed;
top: 10px;
right: 10px;
}
In this example, the div remains fixed 10px from the top and right of the
viewport, even when the page is scrolled.
5. sticky:
Example:
div {
position: sticky;
top: 100px;
}
Example:
div {
position: absolute;
z-index: 1;
}