Why CSS position: sticky Doesn’t Work and How to Fix It
When you add position: sticky and the element scrolls away instead of staying put, the cause falls into three groups. You did not write an offset such as top, an ancestor's overflow changes the boundary the element sticks to, or the parent has no room left for the element to stay stuck. If a header, a table-of-contents sidebar, or a table heading does not stay on screen, you can paste the function in this article into the developer tools console to find out which of these applies.
The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I built a page with an 800px by 600px viewport, a 120px-high band, and a 40px-high heading below it, followed by a 2000px-high body. I added position: sticky and top: 0 to the heading, scrolled the page by 600px, and measured the top edge of the heading with getBoundingClientRect(). I did not test Firefox, Safari, or mobile browsers.
- 1. Cases that do not stick, and how to fix them
- 2. A function to paste into the console and narrow down the cause
- 3. No top or bottom written
- 4. An ancestor's overflow changes the sticking boundary
- 5. The parent has no room left to stay stuck
- 6. bottom: 0 only sticks while the element is below the bottom of the screen
- 7. An ancestor's transform does not affect sticky
Cases that do not stick, and how to fix them
If the heading sticks, its top edge is at 0px after scrolling 600px. If it does not stick, it moves up by 600px from its initial top edge of 120px, ending at -480px, and goes off screen. In every case below, the computed position was still sticky.
| Case | Top edge after scrolling 600px | Fix |
|---|---|---|
| Correct, sticks | 0px | ― |
No top written |
-480px | Write top: 0 or similar |
Ancestor with overflow-x: hidden |
-480px | Change to overflow-x: clip |
Ancestor with overflow: auto (no height set) |
-480px | Remove overflow, or set a height on that ancestor so it scrolls |
overflow-x: hidden only on body |
0px | ― |
overflow-x: hidden only on html |
0px | ― |
overflow-x: hidden on both html and body |
-480px | Keep it on only one of them |
| Parent wraps only the heading and the band | -480px | Place the element directly under a parent that extends to the end of the body |
| Flex child stretched to full height | -600px | Add align-self: start |
Ancestor with transform |
0px | ― |
The flex child row was measured on a different page where the heading was at the top, which is why its non-sticking value is -600px.
A function to paste into the console and narrow down the cause
The following function returns the values you need to estimate the cause for an element with sticky. Paste it into the browser developer tools console and call it with the element.
// Paste into the developer tools console to use. The argument is the element with sticky applied.
function checkSticky(el) {
const s = getComputedStyle(el);
const p = el.parentElement;
const result = {
position: s.position,
insets: { top: s.top, bottom: s.bottom, left: s.left, right: s.right },
selfHeight: el.getBoundingClientRect().height,
parentHeight: p.getBoundingClientRect().height,
alignSelf: s.alignSelf,
container: null,
};
const html = document.documentElement;
for (let a = p; a && a !== html; a = a.parentElement) {
const c = getComputedStyle(a);
const scrolls = (v) => v !== 'visible' && v !== 'clip';
if (!scrolls(c.overflowX) && !scrolls(c.overflowY)) continue;
// Skip body's overflow, since if html is visible it moves to the whole page.
if (a === document.body && getComputedStyle(html).overflowX === 'visible' && getComputedStyle(html).overflowY === 'visible') continue;
result.container = {
element: a.tagName.toLowerCase() + (a.className ? '.' + a.className : ''),
overflowX: c.overflowX,
overflowY: c.overflowY,
scrollableHeight: a.scrollHeight - a.clientHeight,
};
break;
}
if (!result.container) result.container = 'ページ全体(ビューポート)';
return result;
}
checkSticky(document.querySelector('.bar'));
Replace .bar with a selector for the element you want to pin. In Chrome DevTools, the element you last selected in the Elements panel can be referenced as $0, so you can also select the element and call checkSticky($0).
Read the returned values in this order:
- Check whether
topandbottomininsets(orleftandrightfor horizontal sticking) are allauto. - Check whether
containeris something other thanページ全体(ビューポート). If it is, that element'soverflowis the cause. IfscrollableHeightis 0, that element itself is not scrolling. - Check whether
selfHeightis close toparentHeight. If it is, the element has no room to stay stuck.
In the test environment, a page where .layout had overflow-x: hidden returned the following. overflow-y was never written, but it computed to auto, and scrollableHeight was 0.
{"position":"sticky","insets":{"top":"0px","bottom":"auto","left":"auto","right":"auto"},"selfHeight":40,"parentHeight":2160,"alignSelf":"auto","container":{"element":"div.layout","overflowX":"hidden","overflowY":"auto","scrollableHeight":0}}
No top or bottom written
If you write only position: sticky without top, the element does not stick. MDN's description of position says that if both top and bottom are auto for vertical sticking, sticky behaves the same as relative. To stick at the top, write a position such as top: 0, or top: 64px to leave room for a header of that height.
An ancestor's overflow changes the sticking boundary
According to MDN's description of position (English), a sticky element sticks relative to its nearest ancestor whose overflow is hidden, scroll, auto, or overlay. Whether that ancestor is actually scrolling does not matter.
So adding overflow: auto to an ancestor without setting a height also prevents sticking. In the test environment, the height of .layout matched its content at 2160px, so .layout itself did not scroll. Only the whole page scrolled, so the heading did not stick. Conversely, when I made a 300px-high frame with overflow: auto and gave the heading inside it top: 0, the heading's top edge stuck at the top edge of the frame (50px from the top of the screen) after scrolling the frame by 600px. If you want sticking within a frame, give the frame a fixed height so that it scrolls.
For example, suppose you add overflow-x: hidden to an ancestor to hide horizontal overflow. Changing it to overflow-x: clip makes sticking work. I explain the reason in the article on how to choose between clip and hidden for CSS overflow, and I verified a header case in the article on building a header that stays on top when scrolling in React.
It sticks if only one of html or body has it, but not if both do
To prevent horizontal scrolling of the page, you may add overflow-x: hidden to html or body. In the test environment, the heading stuck when the rule was on body alone or html alone. It did not stick when the rule was on both html and body.
The difference comes from the fact that the overflow value is propagated to the viewport (the whole page). The CSS Overflow specification says that the overflow of html is used for scrolling the whole page. When the overflow of html is visible in both axes, the overflow of body is used for the whole page instead, and body itself is treated as visible.
When the rule is only on body, its value is propagated to the whole page, so body does not act as a sticking boundary. When it is only on html, its value is used for the whole page and body stays visible. When both have it, the value of html is used for the whole page, and the overflow-x: hidden on body remains on body. Since body then acts as the sticking boundary and does not scroll itself, the heading does not stick.
If you have the rule on both html and body, keep it on only one of them. What makes this confusing is that the computed values of body from getComputedStyle() were the same in both cases: overflow-x: hidden and overflow-y: auto. What differed was the value of html. With the rule only on body, html was visible in both axes. With the rule on both, html was overflow-x: hidden and overflow-y: auto. Checking the value of body alone cannot tell you whether it is the cause, so the function above skips body when the overflow of html is visible in both axes.
The parent has no room left to stay stuck
According to MDN's description of position, a sticky element can only move within its containing block, which is its nearest block-level ancestor. An element stuck at the top with top is pushed up along with the containing block when the containing block's bottom edge arrives. If the parent is short, the element leaves the screen together with the parent before it can stay stuck. If parentHeight is small in the function's result, suspect this cause.
On a page where only the heading and the band were wrapped in a div, parentHeight was 160, and the heading scrolled up off the screen. Move the heading directly under an element that extends to the end of the body.
A flex child stretches to full height and stops sticking
Even when the parent is long enough, if the element itself stretches to the same height as its parent, there is no room to move. On a page where the sidebar was a child of display: flex and had sticky itself, the function returned selfHeight and parentHeight both equal to 2000, and alignSelf was auto.
According to MDN's description of align-self (English), auto follows the parent's align-items value. In the test environment, the parent's align-items was normal. According to MDN's description of align-items, normal behaves as stretch for flex children. When I added align-self: start to the sidebar, its height shrank to the height of its content (21px), and its top edge stayed at 0px after scrolling 600px.

For the approach of stretching the sidebar column while sticking only the table of contents inside it, see the article on making a sidebar follow the screen partway down the page in CSS.
bottom: 0 only sticks while the element is below the bottom of the screen
To pin an element to the bottom of the screen, write bottom: 0.
In the test environment, when I placed an element with bottom: 0 at the end of the page, it was stuck at the bottom edge of the screen (the bottom edge is at 600px) even before scrolling. When I placed the same element near the top of the page (120px from the top), it stayed in its normal position before scrolling, and after scrolling 600px it moved off the screen. A bottom sticky lifts an element that is below the bottom edge of the screen up to that edge, but it never moves an element downward from its normal position. So an element near the top of the page scrolls up and out of view. sticky is not suitable for keeping an element at the bottom of the screen when it starts near the top of the page. As an alternative, according to MDN's description of position, there is position: fixed, which is positioned relative to the viewport.
An ancestor's transform does not affect sticky
An element with position: fixed is positioned relative to an ancestor with transform, not the whole page. MDN's description of position states this. You might suspect the same for sticky, but in the test environment, adding transform: translateZ(0) to .layout still left the heading stuck at a top edge of 0px. When looking for the cause of sticking failures, check overflow and the parent's height before transform.





