Sticky elements have the best features of both relative and fixed positioning. Elements are initially treated as relative positioning, when the scrolling reaches the end of the boundary, it will become fixed positioning.
It’s very simple, we add position: sticky; to the element.
We also set either top and/or bottom positioning.
<div class="parent">
<div class="child"></div>
</div>
.parent{ height: 500px; width: 500px; } .child{ position: sticky; top: 100px; bottom: 100px; height: 100px; width: 500px; }
overflow
is set on any of the parents elements.height
is set on any of the parents elements.