I'm trying to create a sidebar which contains 2 sticky elements. The issue is when the screen size is changed, the elements start to overlapping each other. I wanted to find a way to prevent that overlapping and add some minimal 'margin between' them.
body {
height: 200vh;
}
.sidebar {
display: flex;
flex-direction: column;
justify-content: space-between;
height: 100vh;
}
.topSticky {
position: sticky;
top: 2rem;
padding-bottom: 2rem;
width: 100px;
height: 100px;
border: 1px solid black;
}
.bottomSticky {
position: sticky;
bottom: 2rem;
width: 100px;
height: 100px;
border: 1px solid black;
}
<div class="sidebar">
<aside class="topSticky">Top sticky</aside>
<aside class="bottomSticky">Bottom sticky</aside>
</div>
I also tried several ways to detect collisions but it was too complex to observe and cover all cases.
There are a few issues and limitations -
- The bottom element should be always pushed to the bottom, even when scrolling.
- The upper element has a dynamic height which I observe for any change.
- The margin between the elements should be reduced when scrolling down(that's naturally happens) but overlapping should be avoided. the margin between them should always be at lease 1rem.
How can I achieve that behaviour? In essence, I should find a way to avoid the overlapping.
Thanks.