Possible solution: Adjusting Relative Position with Ancestors
When working with an element set to position: absolute
, it's important to note that its position is determined in relation to the closest ancestor with a non-static position. By setting the parent element (the div) to position: relative
instead of leaving the position of the ol
undefined, we can prevent the scrolling behavior typically associated with absolutely positioned elements like the ol tag. This allows us to dictate the top and bottom values for pseudo-elements based on the div/parent rather than the ol itself, necessitating an adjustment to the bottom value. For example, setting bottom to 40% would offset the height previously defined as 60% for the ol.
div {
height: 100vh;
width: 30%;
position: relative;
}
ol {
height: 60%;
width: 100%;
overflow-y: scroll;
}
ol::before,
ol::after {
content: '';
height: 5px;
background: red;
width: 100%;
position: absolute;
}
ol::after {
bottom: 40%;
}
li {
height: 300%;
}
<div>
<ol>
<li/>
</ol>
</div>
If this explanation doesn't fully meet your needs, I am open to clarifying any points and updating my response accordingly. Additionally, I will endeavor to include relevant references in future updates.