CSS position: sticky Stops Partway Through Scrolling: Causes and Fixes
A header or heading with position: sticky may stay at the top of the screen for a while and then scroll away as you keep scrolling. This happens when the element that wraps the whole page has height: 100vh or height: 100%. Replacing the parent's height with min-height keeps the element stuck until the end of the page.
The test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. The viewport was 800px wide and 600px high. Inside .page, which wraps the whole page, I placed a 120px-high band, a 40px-high heading (with position: sticky and top: 0), and a 2000px-high body. I measured the top edge of the heading with getBoundingClientRect() while scrolling the page. I did not test Firefox, Safari, or mobile browsers.
If the element never sticks in the first place (a missing top, an overflow on an ancestor, or a heading wrapped in a short element), see the article "Why position: sticky Doesn't Work in CSS and How to Fix It." This article covers the case where the parent box has a fixed height and its content overflows that box.
The release point is set by the parent's height
I changed only the CSS of .page and measured the top edge of the heading while scrolling. If the heading is stuck, its top edge is at 0px. If it has come off, the top edge becomes negative and moves off the screen.
.page CSS |
.page height |
scrollY 600 | scrollY 1200 | scrollY where it starts to release |
|---|---|---|---|---|
| Not set | 2160px | 0px | 0px | Never releases |
min-height: 100vh |
2160px | 0px | 0px | Never releases |
height: 100vh |
600px | -40px | -640px | 561 |
height: 100% on html, body, and .page |
600px | -40px | -640px | 561 |
max-height: 100vh |
600px | -40px | -640px | 561 |
height: 1000px |
1000px | 0px | -240px | 961 |
With height: 100vh, .page stayed 600px high, the same as the screen, even though its content was 2160px tall. The height was fixed, but the content was not clipped. It overflowed below the bottom of .page and was still displayed. This is the default visible behavior described in MDN's overflow documentation. Because the content looks like it continues normally, it is easy to miss that .page ends partway down the page.

According to MDN's position documentation, a sticky element stays in place until the opposite edge of its containing block (the nearest block-level ancestor) reaches it. In this example, the containing block is .page. Before scrolling, the bottom edge of .page is 600px from the top of the screen. When scrollY reaches 560, the bottom edge of .page rises to 40px from the top, which is the same position as the bottom edge of the heading. From then on, the heading is pushed up together with the bottom edge of .page.
The same calculation explains why the release point was scrollY 961 with height: 1000px. The heading starts to release when scrollY passes the parent's height minus the heading's height (40px), which is 960 here. When the parent's height depends on the screen height, as with 100vh or 100%, the release point changes with the screen size. When I changed the viewport height to 800px and measured height: 100vh, .page became 800px high, and the release point was scrollY 761.
Setting height: 100% on html, body, and .page gave the same result as height: 100vh. As MDN's height documentation explains, a percentage height is calculated from the containing block's height. The screen height is passed down from html, so .page also became 600px. max-height: 100vh gave the same result because, as MDN's max-height documentation explains, it limits the height to no more than 100vh.
Replace height with min-height
If you only want the parent to be at least as tall as the screen, use min-height instead of height.
/* Comes off partway */
.page {
height: 100vh;
}
/* Stays stuck to the end */
.page {
min-height: 100vh;
}
As MDN's min-height documentation describes, min-height only prevents the height from going below the given value. If the content is taller, the parent grows to fit it. In the test environment, .page became 2160px, the same as its content, and the top edge of the heading stayed at 0px all the way to the end of the page (scrollY 1560).
If you have html, body, .page { height: 100%; }, remove the height: 100% from .page and add min-height: 100vh to it. I kept height: 100% on html and body, made this change, and measured it. .page became 2160px, and the heading never came off.
Flexbox and grid layouts work the same way
To stack a header and body vertically, you may set .page to display: flex; flex-direction: column or display: grid and also give it height: 100vh. In this case, .page also stayed 600px high, and the heading started to release at scrollY 561. When I changed height: 100vh to min-height: 100vh, the heading stayed stuck to the end in both cases.
In the flexbox test, I added flex: none to the heading, band, and body elements so that the child elements would not shrink. When the content is short and you want the footer at the bottom of the screen, use min-height instead of height to set the screen height, as described in the article "How to Keep the Footer at the Bottom of the Screen in CSS Even When Content Is Short."
A function that compares the parent box height with the content height
This function helps you check whether the parent's height is causing the heading to come off partway. Paste it into the DevTools console, then call it with the element that has sticky applied.
// Paste into the DevTools console. The argument is the element with sticky applied
// Compare the parent box height with its content height
function checkParentHeight(el) {
const p = el.parentElement;
const c = getComputedStyle(p);
const box = p.clientHeight;
const content = p.scrollHeight;
return {
parent: p.tagName.toLowerCase() + (p.className ? '.' + p.className : ''),
boxHeight: box,
contentHeight: content,
overflowY: c.overflowY,
overflowing: c.overflowY === 'visible' && content > box,
};
}
checkParentHeight(document.querySelector('.bar'));
Replace .bar with a selector for the element you want to make stick. clientHeight is the height of the parent box, and scrollHeight is the height including its content. If overflowing is true, the content overflows the parent box, and the heading comes off at the end of the parent box.
In the test environment, this returned the following values for .page with height: 100vh.
{"parent":"div.page","boxHeight":600,"contentHeight":2160,"overflowY":"visible","overflowing":true}
After I changed it to min-height: 100vh, boxHeight became 2160 and overflowing became false.
Scrolling only inside the parent also works
You can keep .page at the height of the screen and add overflow: auto to it. Then only the inside of .page scrolls, and the whole page does not.
.page {
height: 100vh;
overflow: auto;
}
According to MDN's position documentation, a sticky element sticks relative to its nearest scrolling ancestor. In this case, that ancestor is .page. In the test environment, the top edge of the heading stayed at 0px when I set .page's scrollTop to 300, 600, 1200, and 1560.
However, the height of the whole page (document.documentElement.scrollHeight) was 600px, and window.scrollY stayed at 0 even after I called window.scrollTo(0, 600). When I scrolled 600px with the mouse wheel, only .page's scrollTop changed. Code that uses window.scrollTo or window.scrollY, such as a back-to-top button, will not work with scrolling inside .page. You would need to change that code to use .page's scrollTop. If your page has such code, replacing .page's height with min-height requires fewer changes.





