I have a container div which has a border. I’m trying to make the top border sticky on content scroll but it’s not working.
.container{
border: 10px solid red;
position: sticky;
}
.content{
height: 500px;
overflow: scroll;
}
<div class="container">
<div class="content">
</div>
</div>
When I scroll the top border doesn’t stick.
3
Answers
I feel like you might be looking for this result, am not sure though.
I’ve placed
overflow: scroll
in .container instead and included height property for this element.I made some minor edits to your code, and now everything works correctly:
For sticky positioning to work, you must specify at least one of the top, right, bottom, or left properties.
The overflow property defines how content behaves when it exceeds the element’s padding—whether it extends horizontally, vertically, or both.
here is the code:
Please add some paragraphs within the .content class to observe the scrolling effect.
The .container element needs to have the position: sticky and the top property set to determine when it should become sticky. Also, the position: sticky element should not be the scrollable container itself, but rather a child element inside it.