π What is the position Property?
The position property in CSS controls how an element is placed in the document flow. It determines whether an element stays in the normal layout, moves freely, sticks while scrolling, or attaches relative to the viewport.
π― Available Values of position
| Value | Description |
|---|---|
| static | Default; element follows normal flow; offsets don't work |
| relative | Remains in flow but can be shifted using top/right/bottom/left |
| absolute | Removed from flow; positioned relative to nearest positioned ancestor |
| fixed | Fixed to viewport; doesnβt move when scrolling |
| sticky | Acts relative until a scroll threshold; then sticks |
π 1. static (Default Positioning)
All elements are static unless you change them. You cannot use top, left, right, bottom with static positioning.
static Example
.box {
position: static;
}Note
π 2. relative β Offset Without Leaving Flow
relative keeps the element in normal flow but allows shifting it without affecting surrounding elements.
relative Example
.box {
position: relative;
top: 10px;
left: 20px;
}The element moves visually, but its original space remains.
Note
π 3. absolute β Freed From Document Flow
An element with absolute position is removed from normal flow and positioned relative to the **nearest positioned ancestor** (relative, absolute, fixed, sticky).
absolute Example
.parent {
position: relative;
}
.child {
position: absolute;
top: 10px;
right: 20px;
}If no ancestor is positioned, it anchors to the body.
Note
π 4. fixed β Always Attached to the Viewport
fixed elements stay in the same place even when the user scrolls.
fixed Example
.navbar {
position: fixed;
top: 0;
width: 100%;
}Ideal for sticky navbars, floating chat widgets, and overlays.
Note
π 5. sticky β Hybrid of relative + fixed
sticky behaves like relative until a scroll threshold is reached, then switches to fixed behavior.
sticky Example
.header {
position: sticky;
top: 0;
background: white;
}Great for table headers, section titles, and navigation bars.
Note
π§ Offsets: top, right, bottom, left
These work when position β static.
Offsets Example
.box {
position: absolute;
top: 20px;
left: 40px;
}π§± Stacking Context & z-index
Positioned elements (except static) create stacking contexts when az-index is applied.
z-index Example
.box {
position: relative;
z-index: 10;
}Note
π§ͺ Real-World Examples
1οΈβ£ Tooltip using absolute + relative
Tooltip Example
.wrapper {
position: relative;
}
.tooltip {
position: absolute;
bottom: 100%;
left: 0;
padding: 8px;
background: black;
color: white;
}2οΈβ£ Fixed floating button
Floating Button
.fab {
position: fixed;
bottom: 20px;
right: 20px;
background: #ff4081;
padding: 15px;
border-radius: 50%;
color: white;
}3οΈβ£ Sticky table header
Sticky Header
th {
position: sticky;
top: 0;
background: #fff;
}π± Position & Responsive Design
You can modify position inside media queries:
Responsive Positioning
.sidebar {
position: fixed;
}
@media (max-width: 700px) {
.sidebar {
position: static;
}
}Sidebar becomes static on mobile for better UX.
β οΈ Common Mistakes
- Using absolute without setting position on parent β unexpected positioning
- Expecting fixed to respect parent boundaries (it never does)
- Using sticky without scrollable parent β wonβt stick
- Using z-index without a positioned element
- Forgetting absolute removes element from layout flow
Note
π₯ Summary
The position property is one of the core CSS layout tools. Whether you're building navbars, tooltips, sidebars, modals, or floating elements, picking the right positioning method ensures clean, predictable behavior across all devices.
Learn more βMDN Docs π