In web development, position:sticky allows an element to stay visible and positioned within its container while the user scrolls a parent container or viewport. However, when the container has overflow:hidden applied, it may prevent position:sticky from working as intended.
Original Problem:
Consider the following HTML code:
<div class="parent"> <div class="sticky"> ... </div> </div>
The sticky element will remain visible and positioned within the parent div as the user scrolls.
Overflow Issue:
If you add overflow:hidden to the parent div, the sticky element will no longer stick to the top of the container and will scroll out of view.
<div class="parent"> <div class="sticky"> ... </div> </div>
Cause:
overflow:hidden hides any content that extends beyond the boundaries of its container. This includes the sticky element when it is positioned outside of the container's visible area.
Resolution:
To prevent overflow:hidden from interfering with position:sticky, you have two options:
.parent { contain: paint; }
.parent { overflow: auto; /* or */ overflow: scroll; }
The above is the detailed content of Why Does `overflow: hidden` Break `position: sticky` Behavior?. For more information, please follow other related articles on the PHP Chinese website!