Within the code snippet provided, there is a sticky div
positioned inside a container. It adheres to the top of the scrolling panel while remaining within its designated container at all times. This mimics the behavior seen in iOS' UITableView
headers, where headers stay visible until the next header reaches the top.
In the second example, everything remains consistent except for the fact that the container has an overflow:hidden
CSS rule. This seemingly hinders the proper function of the position:sticky
property.
.parent {
position: relative;
background: #ccc;
width: 500px;
height: 150px;
overflow: auto;
margin-bottom: 20px;
}
.hidden-overflow {
overflow: hidden;
}
.sticky {
position: sticky;
background: #333;
text-align: center;
color: #fff;
top: 10px;
}
<div class="parent">
<div>
<div class = "sticky">
Hi, I am a sticky element inside the container which contains the first paragraph.
</div>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit...
(Extract taken from @Daniel's post on here)
What causes position:sticky
to malfunction when applied in a container with overflow:hidden
?