I have an internally-scrollable element. Inside it, there's content that can be scrolled through, along with a position: fixed;
child forming something of a sidebar.
If I hover anywhere inside the parent and scroll, the parent's contents scroll just as expected. However, if I hover inside the fixed child, which is still inside the bounding box of the parent, and scroll, it does not:
<style>
body,
html {
margin: 0;
}
#container {
position: relative;
width: 100%;
height: 100vh;
overflow-y: auto;
}
#sidebar {
position: fixed;
right: 10px;
width: 100px;
top: 10px;
height: calc(100% - 20px);
background-color: red;
}
</style>
<div id='container'>
<div id='sidebar'>
<a href='https://google.com/'>Link to click</a>
When hovering here, scroll does NOT work.
</div>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
<p>Lorem ipsum.</p>
... and so on. When hovering here, scroll works.
</div>
I understand the use of this functionality, but I am interested in making the scroll event still propagate up from the position:fixed;
child to its overflow:auto;
parent, allowing the user to scroll the parent element, even while hovering the child. Is this possible?
I've already tried using JavaScript to watch for scroll events on the child, then pass them up to the parent, but no such scroll events are caught (I imagine as the element itself has nothing to scroll internally). I've also tried a variety of configurations of the parent element (position:relative;
, position:absolute;
, position:fixed;
), all to no avail. The issue only occurs when the child is fixed, but setting it as absolute isn't an option.
Preferably, I'd like to accomplish this without JavaScript, so a breakdown of possible solutions (HTML-only, HTML and CSS, and JavaScript-only) might be useful.
I'm aware of this question, but the first answer there does not work, as scrolling while hovering the fixed child does not trigger a scroll event (for either child or parent). The second answer isn't doable either, as it moves the child outside of the parent, which is not an option in my case.
Edit: Harsh Karanpuria suggested I add pointer-events: none;
to the sidebar. This does accomplish the desired result, but it also makes elements in the sidebar unclickable, which I'd rather not do! After their answer, I also experimented with making child elements of the sidebar pointer-events: all;
again, but found that, while that restored my ability to click them, once again removed the scrollable nature while hovering that grandchild.
position: fixed;
on the sidebar at all and instead make it go where I wanted some other way (in my case, withdisplay: inline-block;
, though grid and flex would usually be better choices, andfloat: right;
would be acceptable). – Limp