search ]

Troubleshooting position: sticky: Quick Fixes and Solutions

If you are experiencing issues with the position: sticky feature in CSS, meaning it’s not working for you, the cause is usually one of the following.

1. The feature is not supported by your browser

Before proceeding, make sure position: sticky is supported by your browser. You can check support here.

The stickily positioned element is an element whose computed position value is sticky. It is treated as relatively positioned until its containing block crosses a specified threshold (such as top: 0), at which point it is treated as stuck (MDN).

2. No placement defined

For elements defined as sticky to work, they must have at least one of these placement properties: top, bottom, right, or left.

.sticky-element {
  position: sticky;
  top: 0;
}

3. One of the parent elements has the overflow property

If a parent of the sticky element has overflow: hidden, overflow: auto, or overflow: scroll, position: sticky will not behave as expected. That parent creates a new scrolling context, so the sticky element sticks within that container instead of the viewport.

In most cases the culprit is a parent with overflow other than visible. Check ancestors first.

Two other gotchas: the sticky element’s parent needs enough height for the page to scroll (otherwise there’s nothing to stick within), and a low or missing z-index can make the sticky element sit behind other content.
Here’s a JavaScript snippet that quickly detects whether any ancestor has a non-visible overflow (source).

let parent = document.querySelector('.sticky-element').parentElement;

while (parent) {
    const hasOverflow = getComputedStyle(parent).overflow;
    if (hasOverflow !== 'visible') {
        console.log(hasOverflow, parent);
    }
    parent = parent.parentElement;
}

FAQs

Common questions about position: sticky not working:

Why is position: sticky not working?
Usually one of three things: your browser doesn't support it (check caniuse), you didn't set top, bottom, left, or right, or a parent has overflow other than visible. Parent overflow is the most common cause.
Do I need to set top or bottom for sticky?
Yes. A sticky element needs at least one of top, bottom, left, or right. Without it, the browser doesn't know where to stick. For example: position: sticky; top: 0;.
Can overflow: hidden on a parent break sticky?
Yes. Any parent with overflow: hidden, overflow: auto, or overflow: scroll creates a new scrolling context. The sticky element then sticks within that ancestor instead of the viewport, which often looks like it's not working. Use the script in this post to find which ancestor has overflow set.
Does position: sticky work in all browsers?
Modern browsers support it. Internet Explorer does not. Check caniuse.com/css-sticky for details. If you need to support IE, use a fallback layout or a polyfill.
My sticky element is behind other content. Why?
Stacking context: another element has a higher z-index or creates a new stacking context. Give your sticky element a z-index high enough to sit above the content it should stick over (e.g. z-index: 10 or higher, depending on your layout).

Summary

Most “sticky doesn’t work” cases come down to parent overflow, missing top/bottom/left/right, or browser support. Fix those first; the script above helps track down overflow. For more on layout and positioning, see the CSS beginners guide. For animating layout-related values, see the CSS @property guide.

Join the Discussion
2 Comments  ]
  • Fran 1 March 2024, 9:47

    Hello Roee!
    I would like to congratulate you for all your work, this post save me a hard time solving the issue within my project.
    Thanks a lot, the script really came handy.

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!

Savvy WordPress Development official logo