static, the default, follows normal document flow with no positioning offset applied at all; relative positions an element normally in flow but then allows shifting it visually via top/right/bottom/left, still leaving its original space reserved in the layout; absolute removes the element from normal flow entirely, positioning it relative to its nearest positioned ancestor, any ancestor with a position other than static, or the initial containing block if none exists; fixed positions relative to the browser viewport itself, staying in place even as the page scrolls; and sticky behaves like relative until a scroll threshold is crossed, then switches to behaving like fixed within its containing block's bounds.
1Understanding Position
static, the default, follows normal document flow with no positioning offset applied at all; relative positions an element normally in flow but then allows shifting it visually via top/right/bottom/left, still leaving its original space reserved in the layout; absolute removes the element from normal flow entirely, positioning it relative to its nearest positioned ancestor, any ancestor with a position other than static, or the initial containing block if none exists; fixed positions relative to the browser viewport itself, staying in place even as the page scrolls; and sticky behaves like relative until a scroll threshold is crossed, then switches to behaving like fixed within its containing block's bounds.
An absolutely positioned element positions itself relative to its nearest ancestor with a position other than static — if no ancestor has been given an explicit position, it falls back to positioning relative to the entire page, which is why adding position: relative to an intended parent container is a critical, easy-to-forget step.
.container {
position: relative;
}
.badge {
position: absolute;
top: 8px;
right: 8px;
}2Practical Example
Here is a real-world application of Position showing how it is used in production CSS code.
.navbar {
position: sticky;
top: 0;
}3Best Practices
Follow these guidelines when working with Position:
1. Set position: relative on an intended parent container specifically to establish it as the positioning context for a child using position: absolute
2. Use position: sticky for elements like a header that should scroll normally at first, then stick in place once reaching a certain scroll point, within its container's bounds
3. Use position: fixed sparingly, and be mindful it removes the element from normal flow entirely, meaning it takes up no space among its siblings and needs its own explicit positioning
Tip: An absolutely positioned element positions itself relative to its nearest ancestor with a position other than static — if no ancestor has been given an explicit position, it falls back to positioning relative to the entire page, which is why adding position: relative to an intended parent container is a critical, easy-to-forget step.
.container {
position: relative;
}
.badge {
position: absolute;
top: 8px;
right: 8px;
}