Master the five position values that control element layout in CSS
The position property specifies how an element is positioned in a document.
It works with the top, right,
bottom, and left
properties to determine the final location.
static (default)
relative
absolute
fixed
sticky
position: static is the default value. Elements render in order as they appear in the document flow.
Box 1 (static)
Box 2 (static)
Box 3 (static)
position: relative positions the element relative to its normal position.
.box {
position: relative;
top: 20px;
left: 30px;
}
Box 1 (static)
Box 2 (relative)
Box 3 (static)
position: absolute removes the element from normal flow and positions it relative to its nearest positioned ancestor.
.parent {
position: relative; /* Creates positioning context */
}
.child {
position: absolute;
top: 0;
right: 0;
}
Box 1 (static)
Box 2 (absolute)
Box 3 (static)
position: fixed positions the element relative to the viewport.
.navbar {
position: fixed;
top: 0;
left: 0;
width: 100%;
}
Box 1 (static)
Fixed Box
Box 2 (static)
position: sticky is a hybrid of relative and fixed positioning.
.header {
position: sticky;
top: 0; /* Stick when scrolled to top */
}
Sticky Header
Content 1
Content 2
Content 3
| Position Value | Relative To | In Flow? | Common Uses |
|---|---|---|---|
| static | Normal flow | Yes | Default behavior |
| relative | Its normal position | Yes (space preserved) | Minor adjustments, creating positioning context |
| absolute | Nearest positioned ancestor | No | Overlays, tooltips, precise placement |
| fixed | Viewport | No | Navigation, modals, persistent elements |
| sticky | Scroll container | Yes until threshold | Sticky headers, table columns |
position: relative on parent to create a positioning context for absolute children
top: 0) to work
z-index to control overlap