Sticky elements keep their original space in the layout. Other content does not jump into the gap when they start sticking.
CSS Position Sticky
Learn how position: sticky works with a live scrolling demo. Adjust the top offset, test common layout problems and copy the CSS for your own page.
In normal flow. Scroll the preview to reach the sticking point.
.scroll-area {
height: 370px;
overflow: auto;
}
.card {
position: sticky;
top: 24px;
}The card sits inside a rail beside the article. The two checkboxes intentionally break its usual sticky behavior.
How position: sticky works
position: sticky keeps an element in the normal document flow, then offsets it as needed when scrolling reaches a limit such as top: 24px. It behaves like an ordinary element at first, stays at the chosen inset while there is room, and moves away when its containing block ends.
The scrolling ancestor determines which scroll movement it reacts to. The containing block determines how far it can travel. These can be different ancestors.
.sticky-element {
position: sticky;
top: 24px;
}Set a non-auto inset on the axis you want to stick to. For vertical scrolling, top is a common choice.
Sticky vs fixed vs relative
| Position | Keeps its layout space? | What happens when you scroll? |
|---|---|---|
static | Yes | Moves normally with the content. Insets have no effect. |
relative | Yes | Moves with the content, with an offset from its original position. |
sticky | Yes | Can stay at an inset within its scrolling ancestor, bounded by its containing block. |
fixed | No | Usually stays relative to the viewport. Certain ancestors, such as a transformed element, can establish a different containing block. |
Useful sticky positioning examples
A sticky header
Keep navigation visible as the page moves. Give it a background so the text below does not show through.
.site-navigation {
position: sticky;
top: 0;
z-index: 10;
background: white;
}A sticky sidebar
In a grid or flex layout, prevent the sidebar from stretching to the full height of the article when that leaves it no room to move.
.sidebar {
position: sticky;
top: 24px;
align-self: start;
}Section headings that take turns
Each heading sticks only while its own group is visible. Scroll this list to see the next heading replace it.
- Analytics
- Animations
- Accessibility
- Buttons
- Borders
- Backgrounds
- Colors
- Columns
- Containers
.list {
max-height: 240px;
overflow: auto;
}
.group-heading {
position: sticky;
top: 12px;
background: white;
}Why position: sticky is not working
- No inset: add
top,bottomor a logical inset for the scrolling axis. Leaving both ends atautogives it no sticky threshold on that axis. - An unexpected overflow ancestor: inspect parents with
overflow: hidden,autoorscroll. They can become the scrolling ancestor even if you are actually scrolling somewhere else.overflow: cliphas different behavior and does not itself create a scroll container. - No travel space: a parent that is only as tall as its sticky child cannot give that child much room to stick. A very tall sticky element can also leave little useful visible space.
- A stretched grid or flex item: use suitable alignment, often
align-self: start, or place the sticky element inside a taller wrapper. - It is hidden behind something: sticky elements create a stacking context. Set a background and a suitable
z-indexwhere overlapping content needs clear layering.
Common questions
Does sticky positioning need JavaScript?
No. CSS controls the sticking itself. This demo uses JavaScript only to change settings, report its state and copy the example code.
Why does my sticky element stop before the page ends?
It is constrained by its containing block. If that section ends before the page does, the sticky element leaves with that section. Place it in a taller suitable parent if it needs to remain visible for longer.
How do I leave room below another header?
Set the sticky element’s top inset to the header’s occupied height plus any gap you want. Check the value at mobile sizes and when text is zoomed so the two elements do not cover each other.
Can table headings be sticky?
Yes. A common approach is to give the header cells position: sticky, a top inset, a background and a suitable stacking order inside a scrolling table wrapper.