CSS Positioning Explained: static to sticky
Static, relative, absolute, fixed and sticky each position an element differently. Learn the containing block and the common traps.
By Velloreth
The five values
.box { position: static; } /* default */
.box { position: relative; }
.box { position: absolute; }
.box { position: fixed; }
.box { position: sticky; }
static
The default. The element is in the normal flow, and top, right, bottom, and left have no effect.
.box {
position: static;
top: 100px; /* ignored */
}
You rarely set it explicitly, except to override another rule.
relative
The element stays in the flow, and the offsets move it from its normal position.
.box {
position: relative;
top: 10px;
left: 20px;
}
The space it occupied is preserved, so the surrounding elements do not move. The element is visually offset, which can overlap a neighbour.
The most common use is not the offset. It is to establish a containing block for an absolutely positioned child.
.card { position: relative; }
.card .badge { position: absolute; top: 0; right: 0; }
The badge is positioned against the card, not the page.
absolute
The element is removed from the flow and positioned against its containing block.
.tooltip {
position: absolute;
top: 100%;
left: 0;
}
The containing block is the nearest ancestor with a position other than static. If there is none, it is the initial containing block, which is the viewport.
This is the most common source of confusion. An absolutely positioned element that appears in the wrong place usually has an ancestor with a position that was not intended.
The element is out of the flow, so the surrounding elements behave as if it were not there. A parent with only absolutely positioned children has a height of zero.
fixed
The element is positioned against the viewport and does not scroll.
.header {
position: fixed;
top: 0;
left: 0;
right: 0;
}
Use it for a persistent header, a floating action button, or a modal overlay.
The trap: a fixed element is removed from the flow, so the content underneath it is hidden. Add a padding to the body equal to the header’s height.
body { padding-top: 60px; }
A fixed element inside a transformed ancestor is positioned against that ancestor rather than the viewport, because a transform creates a containing block. This is a subtle and surprising behaviour.
sticky
Sticky is a hybrid: the element scrolls normally until it reaches a threshold, then sticks.
.sidebar {
position: sticky;
top: 20px;
}
The element sticks when its top edge reaches 20px from the top of the viewport, and it stays there while its parent is in view.
The requirements:
- A
top,bottom,left, orrightvalue. Without one, it does nothing. - A parent with enough height to scroll within. A sticky element in a parent the same height as itself has nothing to stick to.
- No ancestor with
overflow: hidden,auto, orscroll, which breaks it.
The third requirement is the most common cause of a sticky element that does not stick. An overflow on any ancestor creates a scroll container, and the sticky element sticks within that rather than the viewport.
z-index and stacking
z-index controls the paint order of positioned elements.
.modal { position: fixed; z-index: 1000; }
It only applies to a positioned element (or a flex or grid child). A z-index on a static element has no effect.
A stacking context is created by a positioned element with a z-index, a transform, an opacity below 1, a filter, and several other properties.
Within a stacking context, the z-index values are local. A child with z-index: 9999 cannot escape its parent’s context, which is why a modal sometimes appears behind an element with a lower z-index.
The fix: move the modal to the end of the body, or raise the ancestor’s z-index.
Centring
With absolute positioning:
.centred {
position: absolute;
inset: 0;
margin: auto;
width: 200px;
height: 100px;
}
With a transform:
.centred {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
The modern answer is flexbox or grid, which centres without a positioning trick.
.parent {
display: grid;
place-items: center;
}
Choosing a positioning value
The choice between the five values follows from a single question: should the element affect the layout of its neighbours? If the answer is yes, the value is static or relative, because both keep the element in the flow. If the answer is no, the value is absolute or fixed, because both remove it. The second question is what the element should be positioned against: the nearest positioned ancestor for absolute, or the viewport for fixed. Sticky is the special case where the element is in the flow until a threshold, and then behaves like fixed within its parent. Most layout bugs with positioning come from a mismatch between the intended containing block and the actual one, which is why the first debugging step is to check whether an ancestor has a position that was not intended. A single unexpected position: relative on a wrapper is enough to move an absolutely positioned child to the wrong place, and it is invisible in the CSS unless you look for it.
Practical guidance
- Use
relativeto establish a containing block. - Use
absolutefor an overlay inside a positioned parent. - Use
fixedfor a persistent header, and add a body padding. - Use
stickyfor a sidebar or a table header. - Check the ancestors when an absolute element is misplaced.
- Check for an
overflowancestor when sticky does not work. - Remember
z-indexis local to a stacking context. - Prefer flex or grid to a positioning trick for centring.
For the related guides, see css-box-model-explained and css-flexbox-complete-guide. For the broader context, css-grid-complete-guide and css-specificity-explained.
Quotable summary
Static is the default and the offsets have no effect on it, while relative offsets an element from its normal position without removing it from the flow. Absolute removes the element from the flow and positions it against the nearest positioned ancestor, which is the most common source of confusion when an element appears in the wrong place. Fixed positions against the viewport so it does not scroll with the page, and sticky is a hybrid that scrolls normally until it reaches a threshold and then sticks, requiring a threshold value, a parent with height, and no ancestor with overflow. Remember that z-index is local to a stacking context, which is why a modal sometimes appears behind an element with a lower z-index.