While it may not be the most sleek solution, this method is far superior to using JavaScript:
<!DOCTYPE html>
<div style="position: relative; width: 100px; height: 100px; white-space: nowrap; border: solid">
<div style="width: 100%; height: 100%; overflow: auto">
looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong word
<div style="position: absolute; background: aqua; text-align: center; inset: 50% 0 auto; margin-top: -0.5lh">test</div>
</div>
</div>
The position: relative
outer element effectively traps the position: absolute
element just beyond the scrolling box, allowing it to overlay without interfering with the scroll, thus keeping it in place while the content moves.
The conventional CSS approach tends to outperform JavaScript in terms of scrolling and animations due to its declarative nature. This is because the browser engine can optimize and leverage hardware acceleration for layout tasks more efficiently compared to JavaScript's procedural approach.