How to Fix Issues With CSS Position Sticky Not Working?

0 Shares
0
0
0
0
0
0
0

Checking for Browser Compatibility

Before we check for other issues, it might be a good idea to ensure that you’re using a browser that supports position: sticky.

Checking If a Threshold Has Been Specified

A sticky element requires a threshold to be specified by setting value (other than auto) for at least one of the top, right, bottom, or left properties. This threshold value makes the sticky element act as fixed positioned when it crosses the threshold, and a relatively positioned element otherwise. To illustrate this, consider the following example:

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

Checking Vendor Prefix for Safari

Make sure you add a vendor prefix for the property value to support Safari, like this:

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

Checking If an Ancestor Element Has overflow Property Set

If any parent/ancestor of the sticky element has any of the following overflow properties set, position: sticky won’t work:

  1. overflow: hidden
  2. overflow: scroll
  3. overflow: auto

Snippet to Check for Parents With overflow Property Set:

Simply copy/paste the following snippet in your browser’s web developer console to identify all parent elements with overflow property set to something other than visible:

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

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

How to Make position: sticky Work With the overflow Property?

By specifying a height on the overflowing container, you should be able to make position: sticky work whilst having the container element have the overflow property set.

Checking If height Property Is Not Set on Parent

If the parent element has no height set then the sticky element won’t have any area to stick to when scrolling. This happens because the sticky element is meant to stick/scroll within the height of a container.

Checking If a Parent Element Is a Flexbox

If sticky element’s parent is a flexbox, there are two scenarios to check for:

  1. The sticky element has align-self: auto set (which is the default);
  2. The sticky element has align-self: stretch set.

If the Sticky Element Has align-self: auto Set:

In this case the value of align-self would compute to the parent’s align-items value. So, if the parent has align-items: normal (which is the default) or align-items: stretch set, then it means the height of the sticky element would stretch to fill the entire available space. This would leave no room for the sticky element to scroll within the parent.

If the Sticky Element Has align-self: stretch Set:

In this case, the sticky element would stretch to the height of the parent, and would not have any area to scroll within.

How to Make Sticky Element Scrollable Within a Flexbox:

You could simply set the value of the align-self property to align-self: flex-start. This would put the sticky element at the start and won’t stretch it.

for more check
https://www.designcise.com/web/tutorial/how-to-fix-issues-with-css-position-sticky-not-working

You May Also Like