position
Widely availableSince 201597% of users
Chooses how an element is placed. static and relative keep it in the normal flow. absolute and fixed lift it out, and sticky holds it in view as you scroll.
What's modern
From web-features, MDN's browser data and our snippets, checked dailyClassic, with newer partsIt has worked everywhere since July 2015, and it keeps getting newer values.
Newer parts of position
stickyBaseline Sept 2019, Widely Mar 2022Table elements as sticky positioning containersBaseline Mar 2018, Widely Sept 2020
See how modern your own CSS is.Check your CSS →
Quick facts
- Initial value
static- Inherited
- No
- Applies to
- all elements except table-column-group and table-column
- Animation type
- discrete
Quick example
.card {
position: relative;
}
.card .badge {
position: absolute;
top: 0.5rem;
right: 0.5rem;
}Values
staticrelativeabsolutefixedstickyExamples
Sticky header
.site-header {
position: sticky;
top: 0;
}The header scrolls with the page until it reaches the top, then stays there. sticky needs at least one inset, like top, to do anything.
Full-screen overlay
.overlay {
position: fixed;
inset: 0;
background: rgb(0 0 0 / 50%);
}fixed places the box against the viewport, so it stays put when the page scrolls. inset: 0 stretches it to every edge.
Nudge without moving neighbors
.icon {
position: relative;
top: 2px;
}relative shifts the icon 2px down from where it would sit. The elements around it don't move to fill the gap.
Common pitfalls
- Insets do nothing on statictop, right, bottom and left don't apply when position is static, which is the default.
- A transform on an ancestor traps fixed elementsAn ancestor with a transform, and some values of will-change or contain, becomes the containing block for fixed children. The fixed element then moves with that ancestor instead of staying on screen.
- sticky stays inside its parent and its scrollerA sticky element can only move within its containing block, so it stops at the end of its parent. If an ancestor has overflow: hidden, scroll or auto, it sticks inside that box instead of the page.
Related snippet
Sticky headers without JavaScript scroll listenersLayout →position browser support
BaselineWidely availableThis feature is well established and works across many devices and browser versions. It has been available across browsers since 2015.Safe to use without fallbacks.
Usage97%of people can use this3% use a browser without it.
Browsers
1+
1+
1+
12+Mobile
18+Android
4+Android
1+iOS