The CSS position
property controls how an element is placed on a webpage. It allows elements to be positioned normally, relatively, absolutely, or fixed.
Value | Description |
---|---|
static |
Default position, follows the normal flow of the document. |
relative |
Moves element relative to its normal position. |
absolute |
Positions element relative to the nearest positioned ancestor. |
fixed |
Positions element relative to the browser window, doesn’t move when scrolling. |
sticky |
Acts like relative until a scroll position is reached, then behaves like fixed . |
position: static;
(Default Positioning)div {
position: static;
}
✔ Example:
A <div>
will appear normally, one after another in the document.
position: relative;
(Relative Positioning)top
, left
, right
, or bottom
..relative-box {
position: relative;
top: 20px;
left: 30px;
}
✔ Example:
The element moves 20px down and 30px right from where it would normally be.
position: absolute;
(Absolute Positioning)relative
, absolute
, or fixed
element)..absolute-box {
position: absolute;
top: 50px;
left: 50px;
}
✔ Example:
The element is placed exactly 50px from the top and left of its nearest positioned ancestor.
position: fixed;
(Fixed Positioning).fixed-box {
position: fixed;
top: 0;
right: 0;
background-color: red;
padding: 10px;
}
✔ Example:
Useful for sticky headers, chat buttons, or floating menus.
position: sticky;
(Sticky Positioning)relative
until the user scrolls to a specified position, then acts like fixed
..sticky-box {
position: sticky;
top: 0;
background-color: yellow;
}
✔ Example:A navbar remains visible when scrolling down.
Position | Effect | Use Case |
---|---|---|
static |
Default position (normal document flow). | Standard elements. |
relative |
Moves from its normal position but keeps its original space. | Slight adjustments in layout. |
absolute |
Moves relative to the nearest positioned ancestor. | Popups, tooltips, dropdowns. |
fixed |
Stays in place even when scrolling. | Sticky headers, floating buttons. |
sticky |
Behaves like relative until a scroll position is reached, then acts fixed . |
Sticky navigation bars. |
The CSS position
property allows precise control over element placement.
relative
for small adjustments.absolute
for floating elements inside containers.fixed
for sticky headers and buttons.sticky
for elements that follow scrolling.